# Distinct results by field for a given query

**URL:** https://discuss.elastic.co/t/distinct-results-by-field-for-a-given-query/12469
**Category:** Elasticsearch
**Created:** [June 18, 2013, 8:17pm UTC](https://discuss.elastic.co/t/distinct-results-by-field-for-a-given-query/12469 "2013-06-18T20:17:35Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![David\_MZ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david_mz/32/2215_2.png) [@David\_MZ](https://discuss.elastic.co/u/David_MZ)
#### Post date: [June 18, 2013, 8:17pm UTC](https://discuss.elastic.co/t/distinct-results-by-field-for-a-given-query/12469/1 "2013-06-18T20:17:35Z")

</div>

I have the following problem, I have a document that has a field 'xxx'  
which may have duplicate values across the entire index,  
I want to do a very simple thing, I want to be able to query the index  
using a bool query on all my other fields,

but the _results_ of the query should return only distinct results based on  
xxx, my index simulates people, and people who live in the same house are  
duplicates. I would like only to have distinct houses in my results but the  
search is done across all houses

I know the duplication in advance as this is a one time index job, Is there  
a trick I can do to enable this feature in elasticsearch, I was reading  
around and I know that distinct is not present in elastic or lucene out of  
the box  
I am asking for some advanced ideas on how to make this happen, including  
some clever indexing as I have full control and I also know the duplicates  
in advance

I have two scenarios:

1. I want to count the results of a given query- needs to be very fast
2. I want to retrieve the actual documents - performance does not matter

--  
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: ![Jorge\_T](https://avatars.discourse-cdn.com/v4/letter/j/13edae/32.png) [@Jorge\_T](https://discuss.elastic.co/u/Jorge_T)
#### Post date: [June 18, 2013, 8:39pm UTC](https://discuss.elastic.co/t/distinct-results-by-field-for-a-given-query/12469/2 "2013-06-18T20:39:38Z")

</div>

Hi David,

As far as I know, there is no grouping functionality in ES (like Solr for  
instance). However, they do have a nifty parent/child feature you may want  
to take a look at. You could index the people as children of house objects,  
and then make use of the "has\_child" query. This will return only parent  
items that have matching properties in their children. I hope that helps.

Best,

Jorge

On Tuesday, June 18, 2013 4:17:35 PM UTC-4, David MZ wrote:

> I have the following problem, I have a document that has a field 'xxx'  
> which may have duplicate values across the entire index,  
> I want to do a very simple thing, I want to be able to query the index  
> using a bool query on all my other fields,
> 
> but the _results_ of the query should return only distinct results based  
> on xxx, my index simulates people, and people who live in the same house  
> are duplicates. I would like only to have distinct houses in my results but  
> the search is done across all houses
> 
> I know the duplication in advance as this is a one time index job, Is  
> there a trick I can do to enable this feature in elasticsearch, I was  
> reading around and I know that distinct is not present in elastic or lucene  
> out of the box  
> I am asking for some advanced ideas on how to make this happen, including  
> some clever indexing as I have full control and I also know the duplicates  
> in advance
> 
> I have two scenarios:
> 
> 1. I want to count the results of a given query- needs to be very fast
> 2. I want to retrieve the actual documents - performance does not matter

--  
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: ![David\_MZ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david_mz/32/2215_2.png) [@David\_MZ](https://discuss.elastic.co/u/David_MZ)
#### Post date: [June 18, 2013, 9:10pm UTC](https://discuss.elastic.co/t/distinct-results-by-field-for-a-given-query/12469/3 "2013-06-18T21:10:03Z")

</div>

Thank I think I can see the light at the end of the tunnel.

Would the has\_child query return also the child document?

On Tue, Jun 18, 2013 at 11:39 PM, Jorge T  
[jorge.alberto.trujillo@gmail.com](mailto:jorge.alberto.trujillo@gmail.com)wrote:

> Hi David,
> 
> As far as I know, there is no grouping functionality in ES (like Solr for  
> instance). However, they do have a nifty parent/child feature you may want  
> to take a look at. You could index the people as children of house objects,  
> and then make use of the "has\_child" query. This will return only parent  
> items that have matching properties in their children. I hope that helps.
> 
> Best,
> 
> Jorge
> 
> On Tuesday, June 18, 2013 4:17:35 PM UTC-4, David MZ wrote:
> 
> > I have the following problem, I have a document that has a field 'xxx'  
> > which may have duplicate values across the entire index,  
> > I want to do a very simple thing, I want to be able to query the index  
> > using a bool query on all my other fields,
> > 
> > but the _results_ of the query should return only distinct results based  
> > on xxx, my index simulates people, and people who live in the same house  
> > are duplicates. I would like only to have distinct houses in my results but  
> > the search is done across all houses
> > 
> > I know the duplication in advance as this is a one time index job, Is  
> > there a trick I can do to enable this feature in elasticsearch, I was  
> > reading around and I know that distinct is not present in elastic or lucene  
> > out of the box  
> > I am asking for some advanced ideas on how to make this happen, including  
> > some clever indexing as I have full control and I also know the duplicates  
> > in advance
> > 
> > I have two scenarios:
> > 
> > 1. I want to count the results of a given query- needs to be very fast
> > 2. I want to retrieve the actual documents - performance does not matter
> > 
> > --  
> > 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/\_3HVEt9\_\_aQ/unsubscribe](https://groups.google.com/d/topic/elasticsearch/_3HVEt9__aQ/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).

--  
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: ![Jorge\_T](https://avatars.discourse-cdn.com/v4/letter/j/13edae/32.png) [@Jorge\_T](https://discuss.elastic.co/u/Jorge_T)
#### Post date: [June 18, 2013, 10:03pm UTC](https://discuss.elastic.co/t/distinct-results-by-field-for-a-given-query/12469/4 "2013-06-18T22:03:20Z")

</div>

Hi,

It does not, it only returns parents. There is a "has\_parent" query which  
is the mirror of this, or you could just simply gather up the IDs and  
retrieve the children directly.

Also check out "top\_children", which seems like it is similar to has\_child,  
but may be more performant as it seems it does not need to traverse the  
entire index.

Best of luck,

Jorge

On Tuesday, June 18, 2013 4:17:35 PM UTC-4, David MZ wrote:

> I have the following problem, I have a document that has a field 'xxx'  
> which may have duplicate values across the entire index,  
> I want to do a very simple thing, I want to be able to query the index  
> using a bool query on all my other fields,
> 
> but the _results_ of the query should return only distinct results based  
> on xxx, my index simulates people, and people who live in the same house  
> are duplicates. I would like only to have distinct houses in my results but  
> the search is done across all houses
> 
> I know the duplication in advance as this is a one time index job, Is  
> there a trick I can do to enable this feature in elasticsearch, I was  
> reading around and I know that distinct is not present in elastic or lucene  
> out of the box  
> I am asking for some advanced ideas on how to make this happen, including  
> some clever indexing as I have full control and I also know the duplicates  
> in advance
> 
> I have two scenarios:
> 
> 1. I want to count the results of a given query- needs to be very fast
> 2. I want to retrieve the actual documents - performance does not matter

--  
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: ![David\_MZ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david_mz/32/2215_2.png) [@David\_MZ](https://discuss.elastic.co/u/David_MZ)
#### Post date: [June 19, 2013, 5:13am UTC](https://discuss.elastic.co/t/distinct-results-by-field-for-a-given-query/12469/5 "2013-06-19T05:13:02Z")

</div>

My issue is that I need to simulate distinct, so I "need" to know which of  
the children has triggered the parent, so I can include it into the results  
as two children may have the same parent  
the has\_parent query won't give me a distinct answer

I did not understand the retrieve ids approach

On Wed, Jun 19, 2013 at 1:03 AM, Jorge T  
[jorge.alberto.trujillo@gmail.com](mailto:jorge.alberto.trujillo@gmail.com)wrote:

> Hi,
> 
> It does not, it only returns parents. There is a "has\_parent" query which  
> is the mirror of this, or you could just simply gather up the IDs and  
> retrieve the children directly.
> 
> Also check out "top\_children", which seems like it is similar to  
> has\_child, but may be more performant as it seems it does not need to  
> traverse the entire index.
> 
> Best of luck,
> 
> Jorge
> 
> On Tuesday, June 18, 2013 4:17:35 PM UTC-4, David MZ wrote:
> 
> > I have the following problem, I have a document that has a field 'xxx'  
> > which may have duplicate values across the entire index,  
> > I want to do a very simple thing, I want to be able to query the index  
> > using a bool query on all my other fields,
> > 
> > but the _results_ of the query should return only distinct results based  
> > on xxx, my index simulates people, and people who live in the same house  
> > are duplicates. I would like only to have distinct houses in my results but  
> > the search is done across all houses
> > 
> > I know the duplication in advance as this is a one time index job, Is  
> > there a trick I can do to enable this feature in elasticsearch, I was  
> > reading around and I know that distinct is not present in elastic or lucene  
> > out of the box  
> > I am asking for some advanced ideas on how to make this happen, including  
> > some clever indexing as I have full control and I also know the duplicates  
> > in advance
> > 
> > I have two scenarios:
> > 
> > 1. I want to count the results of a given query- needs to be very fast
> > 2. I want to retrieve the actual documents - performance does not matter
> > 
> > --  
> > 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/\_3HVEt9\_\_aQ/unsubscribe](https://groups.google.com/d/topic/elasticsearch/_3HVEt9__aQ/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).

--  
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:30am UTC](https://discuss.elastic.co/t/distinct-results-by-field-for-a-given-query/12469/6 "2017-07-06T02:30:30Z")

</div>


