# Get distinct records from a search query

**URL:** <https://discuss.elastic.co/t/get-distinct-records-from-a-search-query/46183>\
**Category:** Elasticsearch\
**Created:** [April 3, 2016, 9:19pm UTC](https://discuss.elastic.co/t/get-distinct-records-from-a-search-query/46183 "2016-04-03T21:19:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anurag\_srivastava](https://avatars.discourse-cdn.com/v4/letter/a/90db22/32.png) [@Anurag\_srivastava](https://discuss.elastic.co/u/Anurag_srivastava)\
**Post date:** [April 3, 2016, 9:19pm UTC](https://discuss.elastic.co/t/get-distinct-records-from-a-search-query/46183/1 "2016-04-03T21:19:13Z")

</div>

Hi,  
How can i get a distinct record set from a query in elastic search :My query is as follows 🙂{  
"\_source": [  
"dealname",  
"deal\_key"  
],  
"query": {  
"match": {  
"dealname": {  
"query": "Babson 2004",  
"operator": "and"  
}  
}  
}  
}

and the result that i am getting back is:

"\_source": {  
"dealname": "Babson CLO Ltd. 2004-I",  
"deal\_key": "500029871"  
}

"\_source": {  
"dealname": "Babson CLO Ltd. 2004-I",  
"deal\_key": "500029871"  
}

"\_source": {  
"dealname": "Babson CLO Ltd. 2004-I2",  
"deal\_key": "500029871"  
}

But as you see there are only 2 distinct records in the output  
1:deal\_key 500029871 and dealname Babson CLO Ltd. 2004-I  
2:deal\_key 500029871 and dealname Babson CLO Ltd. 2004-I2

But the output gives me 3 result set .How can i refine this?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 4, 2016, 5:57am UTC](https://discuss.elastic.co/t/get-distinct-records-from-a-search-query/46183/2 "2016-04-04T05:57:08Z")

</div>

You have to use a Terms aggregation

---

<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:** [July 5, 2017, 11:02pm UTC](https://discuss.elastic.co/t/get-distinct-records-from-a-search-query/46183/3 "2017-07-05T23:02:46Z")

</div>


