Elastic search - Get each conditions count along with result result


(Ketan Broctagon) #1

Hello, My requirement is to fetch the count of each creteria as well for each conditions along with the result.

For example :

If the query is to search documents having firstname equal to ‘Charles’ or city equal to ‘New York’. And in total there are 5 documents with ‘Charles’ as firstname, 10 documents with ‘New York’ as city and 2 documents which have both ‘Charles’ as firstname and ‘New York’ as city.

So, along with 17 results I need count as well like first name/Charles = 6 City/New York = 11

Could you please let me know what can be done here to implement this ?

EXPECTED SCRIPT :

var query = {
                query: {
                        "bool": {
                        "should" : [
                                { match: { firstname : ‘Charles’ }},
                                { match: { city : ‘New York’ }}
                        ]
                }
            }
        };

        client.search({
                index: 'blacklists',
                type: 'place',
                body: query

        }).then(function (resp) {
                res.status(200).json({status: 'success', data: resp});
        }, function (err) {
                console.trace(err.message);
                res.status(200).json({status: 'fail', data: err.message});
        });

CURRENT RESULT :

{
    "status": "success",
    "data": {
        "took": 20,
        "timed_out": false,
        "_shards": {
            "total": 3,
            "successful": 3,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": 3,
            "max_score": 0.6931472,
            "hits": [
                {
                    "_index": "blacklists",
                    "_type": "place",
                    "_id": "5a3498cccaccfbc3b0c7eabd",
                    "_score": 0.6931472,
                    "_source": {
                        "id": "5a3498cccaccfbc3b0c7eabd",
                         “firstname”: "Charles",
                         “city”: “Munich”,
                    }
                },
                {
                    "_index": "blacklists",
                    "_type": "place",
                    "_id": "5a1a61ce069ac1e0b8ffb1ee",
                    "_score": 0.6099695,
                    "_source": {
                        "id": "5a1a61ce069ac1e0b8ffb1ee",
                         “firstname”: "Charles",
                         “city”: “Manchester”,
                    }
                },
                {
                    "_index": "blacklists",
                    "_type": "place",
                    "_id": "5a1a607a069ac1e0b8ffb1ed",
                    "_score": 0.6099695,
                    "_source": {
                        "id": "5a1a607a069ac1e0b8ffb1ed”,
                        “firstname”: “Fernando",
                        “city”: “New York”,
                    }
                }
            ]
        }
    }
}

EXPECTED RESULT :

If I could get documents count as well in result for Charles and New York


(system) #2

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.