# Returning Distinct Values in a query

**URL:** <https://discuss.elastic.co/t/returning-distinct-values-in-a-query/197361>\
**Category:** Elasticsearch\
**Created:** [August 29, 2019, 2:25pm UTC](https://discuss.elastic.co/t/returning-distinct-values-in-a-query/197361 "2019-08-29T14:25:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsariasgeek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jsariasgeek/32/53301_2.png) [@jsariasgeek](https://discuss.elastic.co/u/jsariasgeek)\
**Post date:** [August 29, 2019, 2:25pm UTC](https://discuss.elastic.co/t/returning-distinct-values-in-a-query/197361/1 "2019-08-29T14:25:59Z")

</div>

Hi there,  
I have the next elasticsearch query and I need to know how to get only distinct results for certain fields. (Like an sql distinct: SELECT DISTINCT _column1_ , _column2, ..._  
FROM _table\_name_ 😉

Here is my query

{  
"\_source": ["part", "manufacturer", "shortdesc"],  
"query": {  
"match": {  
"part": "2n2222"  
}  
}  
}

And here my results:

{  
"\_index" : "my\_index",  
"\_type" : "\_doc",  
"\_id" : "13921",  
"\_score" : 207.16005,  
"\_source" : {  
"part" : "2N2222A",  
"manufacturer" : "Microsemi Corporation"  
}  
},  
{  
"\_index" : "my\_index",  
"\_type" : "\_doc",  
"\_id" : "13923",  
"\_score" : 207.16005,  
"\_source" : {  
"part" : "2N2222A",  
"manufacturer" : "Microsemi Corporation"  
}  
},  
{  
"\_index" : "my\_index",  
"\_type" : "\_doc",  
"\_id" : "811202",  
"\_score" : 202.03964,  
"\_source" : {  
"part" : "2N2222A",  
"manufacturer" : "Microsemi Corporation"  
}  
},  
{  
"\_index" : "my\_index",  
"\_type" : "\_doc",  
"\_id" : "534059",  
"\_score" : 202.03964,  
"\_source" : {  
"part" : "2N2222A",  
"manufacturer" : "Microsemi Corporation"  
}  
},  
{  
"\_index" : "my\_index",  
"\_type" : "\_doc",  
"\_id" : "534062",  
"\_score" : 202.03964,  
"\_source" : {  
"part" : "2N2222A",  
"manufacturer" : "Microsemi Corporation"  
}  
},  
{  
"\_index" : "my\_index",  
"\_type" : "\_doc",  
"\_id" : "144303",  
"\_score" : 202.03964,  
"\_source" : {  
"part" : "2N2222A",  
"manufacturer" : "Microsemi Corporation"  
}  
},  
{  
"\_index" : "my\_index",  
"\_type" : "\_doc",  
"\_id" : "557240",  
"\_score" : 202.03964,  
"\_source" : {  
"part" : "2N2222A",  
"manufacturer" : "Infineon"  
}  
},  
{  
"\_index" : "my\_index",  
"\_type" : "\_doc",  
"\_id" : "13924",  
"\_score" : 201.24086,  
"\_source" : {  
"part" : "2N2222A",  
"manufacturer" : "Microsemi Corporation"  
}  
},  
{  
"\_index" : "my\_index",  
"\_type" : "\_doc",  
"\_id" : "557235",  
"\_score" : 201.24086,  
"\_source" : {  
"part" : "2N2222A",  
"manufacturer" : "Microsemi Corporation"  
}  
},  
{  
"\_index" : "my\_index",  
"\_type" : "\_doc",  
"\_id" : "55566",  
"\_score" : 201.24086,  
"\_source" : {  
"part" : "2N2222A",  
"manufacturer" : "Microsemi Corporation"  
}  
},  
{  
"\_index" : "my\_index",  
"\_type" : "\_doc",  
"\_id" : "50873",  
"\_score" : 201.24086,  
"\_source" : {  
"part" : "2N2222A",  
"manufacturer" : "Microsemi Corporation"  
}  
},  
{  
"\_index" : "my\_index",  
"\_type" : "\_doc",  
"\_id" : "13915",  
"\_score" : 199.76857,  
"\_source" : {  
"part" : "2N2222A",  
"manufacturer" : "Microsemi Corporation"  
}  
},  
{  
"\_index" : "my\_index",  
"\_type" : "\_doc",  
"\_id" : "591924",  
"\_score" : 199.76857,  
"\_source" : {  
"part" : "2N2222A",  
"manufacturer" : "Microsemi Corporation"  
}  
},  
{  
"\_index" : "my\_index",  
"\_type" : "\_doc",  
"\_id" : "526043",  
"\_score" : 199.76857,  
"\_source" : {  
"part" : "2N2222A",  
"manufacturer" : "Microsemi Corporation"  
}  
},  
{  
"\_index" : "my\_index",  
"\_type" : "\_doc",  
"\_id" : "423282",  
"\_score" : 198.89282,  
"\_source" : {  
"part" : "2N2222A",  
"manufacturer" : "Microsemi Corporation"  
}  
},  
{  
"\_index" : "my\_index",  
"\_type" : "\_doc",  
"\_id" : "565951",  
"\_score" : 193.51782,  
"\_source" : {  
"part" : "P2N2222A",  
"manufacturer" : "ON Semiconductor"  
}  
},  
{  
"\_index" : "my\_index",  
"\_type" : "\_doc",  
"\_id" : "13920",  
"\_score" : 192.1505,  
"\_source" : {  
"part" : "P2N2222A",  
"manufacturer" : "ON Semiconductor"  
}  
},  
{  
"\_index" : "my\_index",  
"\_type" : "\_doc",  
"\_id" : "2885944",  
"\_score" : 191.28773,  
"\_source" : {  
"part" : "Q2N2222A",  
"manufacturer" : "Freescale Semiconductor"  
}  
},  
{  
"\_index" : "my\_index",  
"\_type" : "\_doc",  
"\_id" : "765656",  
"\_score" : 191.28773,  
"\_source" : {  
"part" : "2N2222AL",  
"manufacturer" : "Microsemi"  
}  
},  
{  
"\_index" : "my\_index",  
"\_type" : "\_doc",  
"\_id" : "491090",  
"\_score" : 190.78474,  
"\_source" : {  
"part" : "2N2222AUB",  
"manufacturer" : "Microsemi Corporation"  
}  
}

and I need to return distinct values for part and manufacturer

Thank you so much for your help.

---

<div class="post-metadata">

**Author:** ![wangqinghuan](https://avatars.discourse-cdn.com/v4/letter/w/d26b3c/32.png) [@wangqinghuan](https://discuss.elastic.co/u/wangqinghuan)\
**Post date:** [August 30, 2019, 1:34am UTC](https://discuss.elastic.co/t/returning-distinct-values-in-a-query/197361/2 "2019-08-30T01:34:42Z")

</div>

Terms Aggregation can get distinct values.

```
{
  "aggs": {
    "distinct_part": {
      "terms": {
        "field": "part",
        "size": 1000
      }
    },
      "aggs": {
    "distinct_manufacturer": {
      "terms": {
        "field": "manufacturer",
        "size": 1000
      }
    }
  }
}

```

Note the size parameter determines the number of results and make sure the field you aggregate on is not analyzed.

---

<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:** [September 27, 2019, 10:18am UTC](https://discuss.elastic.co/t/returning-distinct-values-in-a-query/197361/4 "2019-09-27T10:18:04Z")

</div>

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