Get distinct values from a field in ElasticSearch


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


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.


You can user terms aggregation to get distinct values from your _source.
In your case
GET persons/_search
 "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.)



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


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