# Select first document in each group

**URL:** <https://discuss.elastic.co/t/select-first-document-in-each-group/13279>\
**Category:** Elasticsearch\
**Created:** [August 21, 2013, 6:24am UTC](https://discuss.elastic.co/t/select-first-document-in-each-group/13279 "2013-08-21T06:24:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniel\_koller](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daniel_koller/32/2149_2.png) [@daniel\_koller](https://discuss.elastic.co/u/daniel_koller)\
**Post date:** [August 21, 2013, 6:24am UTC](https://discuss.elastic.co/t/select-first-document-in-each-group/13279/1 "2013-08-21T06:24:37Z")

</div>

Hi all,

is there a "query" where I can get only the first document of a group of  
documents (e.g. all docs have the same value for field "customer")?

in SQL this is something like

id | customer | total  
1 | Joe | 5  
2 | Sally | 3  
3 | Joe | 2  
4 | Sally | 1

a query like

SELECT DISTINCT ON (customer)  
id, customer, totalFROM purchasesORDER BY customer, total DESC, id

then the result would be

1 | Joe | 5  
2 | Sally | 3

So only one document for each "customer" is returned.

Best regards,  
Daniel

--  
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:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [August 21, 2013, 9:09pm UTC](https://discuss.elastic.co/t/select-first-document-in-each-group/13279/2 "2013-08-21T21:09:08Z")

</div>

You would probably need to implement a custom facet to support your  
request. Identical to a terms facet, but all the values after the first  
will be disregarded.

--  
Ivan

--  
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:** ![daniel\_koller](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daniel_koller/32/2149_2.png) [@daniel\_koller](https://discuss.elastic.co/u/daniel_koller)\
**Post date:** [August 22, 2013, 4:37am UTC](https://discuss.elastic.co/t/select-first-document-in-each-group/13279/3 "2013-08-22T04:37:06Z")

</div>

Hi Ivan,

thank you for your reply, I suspected something like this.

Best regards,  
Daniel

Am Mittwoch, 21. August 2013 23:09:08 UTC+2 schrieb Ivan Brusic:

> You would probably need to implement a custom facet to support your  
> request. Identical to a terms facet, but all the values after the first  
> will be disregarded.
> 
> --  
> Ivan

--  
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:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [August 22, 2013, 4:02pm UTC](https://discuss.elastic.co/t/select-first-document-in-each-group/13279/4 "2013-08-22T16:02:48Z")

</div>

I was hoping someone else would have a suggestion, but I think that might  
be your best bet.

I needed something similar, but I was waiting for the facet framework  
refactor to occur before creating another facet type (lots of boilerplate  
code is involved). The facet refactor is actually a whole new aggregation  
framework which has been detailed but not yet released:  
[Aggregation Module - Phase 1 - Functional Design · Issue #3300 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/3300). Part of what  
you are looking for is field collapsing, which is not implement in  
elasticsearch. The new aggregation framework does not seem to address it  
either.

Cheers,

Ivan

On Wed, Aug 21, 2013 at 9:37 PM, daniel koller [danielkoller82@gmail.com](mailto:danielkoller82@gmail.com)wrote:

> Hi Ivan,
> 
> thank you for your reply, I suspected something like this.
> 
> Best regards,  
> Daniel
> 
> Am Mittwoch, 21. August 2013 23:09:08 UTC+2 schrieb Ivan Brusic:
> 
> > You would probably need to implement a custom facet to support your  
> > request. Identical to a terms facet, but all the values after the first  
> > will be disregarded.
> > 
> > --  
> > Ivan
> 
> --  
> 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).

--  
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:20am UTC](https://discuss.elastic.co/t/select-first-document-in-each-group/13279/5 "2017-07-06T02:20:08Z")

</div>


