Get distinct values from a field in ElasticSearch

Hello,

I have the following URI which should retrieve all values from the Gender field.

http://localhost:9200/persons/_search?_source=Gender

However, it does not return me all the data in the index of the Gender field. I want to get the data distinct also.

I am consuming this REST API in AngularJS. Can someone help me to achieve this type of URI query ?

Thank you.

2 Likes

You can user terms aggregation to get distinct values from your _source.
In your case
GET persons/_search
{
 "size":"0",
 "aggs" : {
  "uniq_gender" : {
   "terms" : { "field" : "Gender" }
   }
  }
}
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.
(field data should be enabled for your field, in case it isn't then you can user multi field to create a sub field and use that one.)

5 Likes

Thanks.

But how can I get this in form of a REST URI to consume with AngularJS ?

2 Likes

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