# How Can I Perform a Distinct Query?

**URL:** https://discuss.elastic.co/t/how-can-i-perform-a-distinct-query/12539
**Category:** Elasticsearch
**Created:** [June 25, 2013, 9:12am UTC](https://discuss.elastic.co/t/how-can-i-perform-a-distinct-query/12539 "2013-06-25T09:12:51Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Randall\_Degges](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/randall_degges/32/2249_2.png) [@Randall\_Degges](https://discuss.elastic.co/u/Randall_Degges)
#### Post date: [June 25, 2013, 9:12am UTC](https://discuss.elastic.co/t/how-can-i-perform-a-distinct-query/12539/1 "2013-06-25T09:12:51Z")

</div>

Hi Guys,

I'm a new ElasticSearch user, and need some help selecting only documents  
with a distinct field. I've got an index of documents which look something  
like the following:

{  
"last\_name": "Degges",  
"first\_name": "Randall",  
"state": "CA",  
}

I'm trying to return a list of de-duplicated documents which contain the  
same last name. For instance -- if I had three documents, two with a  
"last\_name" field of "Degges" and one with a "last\_name" field of "Perez",  
I'd want to return only two documents: one document where "Degges" is the  
last name (I don't care which), and one where "Perez" is the last name.

I realize that this question has been asked before on this mailing list,  
but even after reading through the documentation on facets, asking on the  
IRC channel, and doing lots of trial-and-error testing, I can't figure out  
how to make it work.

I'm hoping some of you can give me specific example queries I can use to do  
the de-duplication.

The reason I'm attempting to do this is that my application needs to return  
a list of unique last names for people in a given state (e.g. CA). Since I  
have so many people in my index, it would be incredibly slow for me to  
select all of the people at once (with duplicates), and de-duplicate things  
on my end ☹

Any help would be greatly appreciated.

Thank you.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Justin\_Treher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/justin_treher/32/45243_2.png) [@Justin\_Treher](https://discuss.elastic.co/u/Justin_Treher)
#### Post date: [June 25, 2013, 12:04pm UTC](https://discuss.elastic.co/t/how-can-i-perform-a-distinct-query/12539/2 "2013-06-25T12:04:18Z")

</div>

Basically, you want to send in a query with some specific filters and then  
only return a list of facets based on last\_name? This would give you a list  
of unique last names along with their counts, which is sounds like you can  
ignore. You can't return "unique documents" as every document is already  
unique.

{"query":"match\_all":{},  
"facets": {  
"filters": {  
"terms": {  
"field": "last\_name",  
"size": 10000  
}  
}}}

On Tuesday, June 25, 2013 5:12:51 AM UTC-4, Randall Degges wrote:

> Hi Guys,
> 
> I'm a new Elasticsearch user, and need some help selecting only documents  
> with a distinct field. I've got an index of documents which look something  
> like the following:
> 
> {  
> "last\_name": "Degges",  
> "first\_name": "Randall",  
> "state": "CA",  
> }
> 
> I'm trying to return a list of de-duplicated documents which contain the  
> same last name. For instance -- if I had three documents, two with a  
> "last\_name" field of "Degges" and one with a "last\_name" field of "Perez",  
> I'd want to return only two documents: one document where "Degges" is the  
> last name (I don't care which), and one where "Perez" is the last name.
> 
> I realize that this question has been asked before on this mailing list,  
> but even after reading through the documentation on facets, asking on the  
> IRC channel, and doing lots of trial-and-error testing, I can't figure out  
> how to make it work.
> 
> I'm hoping some of you can give me specific example queries I can use to  
> do the de-duplication.
> 
> The reason I'm attempting to do this is that my application needs to  
> return a list of unique last names for people in a given state (e.g. CA).  
> Since I have so many people in my index, it would be incredibly slow for  
> me to select all of the people at once (with duplicates), and de-duplicate  
> things on my end ☹
> 
> Any help would be greatly appreciated.
> 
> Thank you.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Randall\_Degges](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/randall_degges/32/2249_2.png) [@Randall\_Degges](https://discuss.elastic.co/u/Randall_Degges)
#### Post date: [June 26, 2013, 6:36am UTC](https://discuss.elastic.co/t/how-can-i-perform-a-distinct-query/12539/3 "2013-06-26T06:36:54Z")

</div>

Thanks Justin,

That actually solved my problem perfectly!

-Randall

On Tue, Jun 25, 2013 at 5:04 AM, Justin Treher [jtreher@gmail.com](mailto:jtreher@gmail.com) wrote:

> Basically, you want to send in a query with some specific filters and then  
> only return a list of facets based on last\_name? This would give you a list  
> of unique last names along with their counts, which is sounds like you can  
> ignore. You can't return "unique documents" as every document is already  
> unique.
> 
> {"query":"match\_all":{},  
> "facets": {  
> "filters": {  
> "terms": {  
> "field": "last\_name",  
> "size": 10000  
> }  
> }}}
> 
> On Tuesday, June 25, 2013 5:12:51 AM UTC-4, Randall Degges wrote:
> 
> > Hi Guys,
> > 
> > I'm a new Elasticsearch user, and need some help selecting only documents  
> > with a distinct field. I've got an index of documents which look something  
> > like the following:
> > 
> > {  
> > "last\_name": "Degges",  
> > "first\_name": "Randall",  
> > "state": "CA",  
> > }
> > 
> > I'm trying to return a list of de-duplicated documents which contain the  
> > same last name. For instance -- if I had three documents, two with a  
> > "last\_name" field of "Degges" and one with a "last\_name" field of "Perez",  
> > I'd want to return only two documents: one document where "Degges" is the  
> > last name (I don't care which), and one where "Perez" is the last name.
> > 
> > I realize that this question has been asked before on this mailing list,  
> > but even after reading through the documentation on facets, asking on the  
> > IRC channel, and doing lots of trial-and-error testing, I can't figure out  
> > how to make it work.
> > 
> > I'm hoping some of you can give me specific example queries I can use to  
> > do the de-duplication.
> > 
> > The reason I'm attempting to do this is that my application needs to  
> > return a list of unique last names for people in a given state (e.g. CA).  
> > Since I have so many people in my index, it would be incredibly slow for  
> > me to select all of the people at once (with duplicates), and de-duplicate  
> > things on my end ☹
> > 
> > Any help would be greatly appreciated.
> > 
> > Thank you.
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/-C2mdiKfcVk/unsubscribe](https://groups.google.com/d/topic/elasticsearch/-C2mdiKfcVk/unsubscribe).  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
Randall Degges  
_[http://rdegges.com/](http://rdegges.com/)_

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Thomas\_Bolis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thomas_bolis/32/700_2.png) [@Thomas\_Bolis](https://discuss.elastic.co/u/Thomas_Bolis)
#### Post date: [June 27, 2013, 5:58am UTC](https://discuss.elastic.co/t/how-can-i-perform-a-distinct-query/12539/4 "2013-06-27T05:58:46Z")

</div>

Hi,

I have a similar problem.. but can we actually count the distinct terms  
without fetching them from ES? There might be some million of unique terms  
that we only need to know is how many they are in a specific time period  
for instance (range)

Thanks

On Wednesday, 26 June 2013 09:36:54 UTC+3, Randall Degges wrote:

> Thanks Justin,
> 
> That actually solved my problem perfectly!
> 
> -Randall
> 
> On Tue, Jun 25, 2013 at 5:04 AM, Justin Treher \<[jtr...@gmail.com](mailto:jtr...@gmail.com)\<javascript:\>
> 
> > wrote:
> 
> > Basically, you want to send in a query with some specific filters and  
> > then only return a list of facets based on last\_name? This would give you a  
> > list of unique last names along with their counts, which is sounds like you  
> > can ignore. You can't return "unique documents" as every document is  
> > already unique.
> > 
> > {"query":"match\_all":{},  
> > "facets": {  
> > "filters": {  
> > "terms": {  
> > "field": "last\_name",  
> > "size": 10000  
> > }  
> > }}}
> > 
> > On Tuesday, June 25, 2013 5:12:51 AM UTC-4, Randall Degges wrote:
> > 
> > > Hi Guys,
> > > 
> > > I'm a new Elasticsearch user, and need some help selecting only  
> > > documents with a distinct field. I've got an index of documents which look  
> > > something like the following:
> > > 
> > > {  
> > > "last\_name": "Degges",  
> > > "first\_name": "Randall",  
> > > "state": "CA",  
> > > }
> > > 
> > > I'm trying to return a list of de-duplicated documents which contain the  
> > > same last name. For instance -- if I had three documents, two with a  
> > > "last\_name" field of "Degges" and one with a "last\_name" field of "Perez",  
> > > I'd want to return only two documents: one document where "Degges" is the  
> > > last name (I don't care which), and one where "Perez" is the last name.
> > > 
> > > I realize that this question has been asked before on this mailing list,  
> > > but even after reading through the documentation on facets, asking on the  
> > > IRC channel, and doing lots of trial-and-error testing, I can't figure out  
> > > how to make it work.
> > > 
> > > I'm hoping some of you can give me specific example queries I can use to  
> > > do the de-duplication.
> > > 
> > > The reason I'm attempting to do this is that my application needs to  
> > > return a list of unique last names for people in a given state (e.g. CA).  
> > > Since I have so many people in my index, it would be incredibly slow for  
> > > me to select all of the people at once (with duplicates), and de-duplicate  
> > > things on my end ☹
> > > 
> > > Any help would be greatly appreciated.
> > > 
> > > Thank you.
> > 
> > --  
> > You received this message because you are subscribed to a topic in the  
> > Google Groups "elasticsearch" group.  
> > To unsubscribe from this topic, visit  
> > [https://groups.google.com/d/topic/elasticsearch/-C2mdiKfcVk/unsubscribe](https://groups.google.com/d/topic/elasticsearch/-C2mdiKfcVk/unsubscribe).  
> > To unsubscribe from this group and all its topics, send an email to  
> > [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> Randall Degges  
> _[http://rdegges.com/](http://rdegges.com/)_

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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 6, 2017, 2:29am UTC](https://discuss.elastic.co/t/how-can-i-perform-a-distinct-query/12539/5 "2017-07-06T02:29:18Z")

</div>


