Get distinct values from a field in ElasticSearch

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