# Get distinct values from a field in ElasticSearch

**URL:** https://discuss.elastic.co/t/get-distinct-values-from-a-field-in-elasticsearch/99783
**Category:** Elasticsearch
**Created:** [September 8, 2017, 4:25am UTC](https://discuss.elastic.co/t/get-distinct-values-from-a-field-in-elasticsearch/99783 "2017-09-08T04:25:47Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![stackf01](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@stackf01](https://discuss.elastic.co/u/stackf01)
#### Post date: [September 8, 2017, 4:25am UTC](https://discuss.elastic.co/t/get-distinct-values-from-a-field-in-elasticsearch/99783/1 "2017-09-08T04:25:47Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![vivek7mehta](https://avatars.discourse-cdn.com/v4/letter/v/51bf81/32.png) [@vivek7mehta](https://discuss.elastic.co/u/vivek7mehta)
#### Post date: [September 8, 2017, 6:36am UTC](https://discuss.elastic.co/t/get-distinct-values-from-a-field-in-elasticsearch/99783/2 "2017-09-08T06:36:38Z")

</div>

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.)

---

<div class="post-metadata">

### Author: ![stackf01](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@stackf01](https://discuss.elastic.co/u/stackf01)
#### Post date: [September 8, 2017, 6:44am UTC](https://discuss.elastic.co/t/get-distinct-values-from-a-field-in-elasticsearch/99783/3 "2017-09-08T06:44:13Z")

</div>

Thanks.

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

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [October 6, 2017, 6:44am UTC](https://discuss.elastic.co/t/get-distinct-values-from-a-field-in-elasticsearch/99783/4 "2017-10-06T06:44:33Z")

</div>

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