Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch: I want to get count with group by

I have the following type on ElasticSearch:

{   bundle_id:   11141   bundle_name: bla_bla   country:     India } 

I want to perform the following query (I'll describe it in SQL):

SELECT COUNT(DISTINCT bundle_id) FROM my_type WHERE county IN ('india', 'usa') 
like image 573
Avi K. Avatar asked May 29 '13 12:05

Avi K.


People also ask

How do I get more than 10 results in Elasticsearch?

If a search request results in more than ten hits, ElasticSearch will, by default, only return the first ten hits. To override that default value in order to retrieve more or fewer hits, we can add a size parameter to the search request body.

How do I get distinct values in Elasticsearch?

You can user terms aggregation to get distinct values from your _source. As you have mentioned you don't want any other data from _source hence you can give size=0. This will give you all unique Gender values with their count in the response.

How do Elasticsearch aggregations work?

Elasticsearch Aggregations provide you with the ability to group and perform calculations and statistics (such as sums and averages) on your data by using a simple search query. An aggregation can be viewed as a working unit that builds analytical information across a set of documents.


1 Answers

Have a look at Terms Aggregation. This is what you are looking for.

like image 197
dadoonet Avatar answered Sep 29 '22 08:09

dadoonet