# Grouping search results in Elasticsearch

**URL:** <https://discuss.elastic.co/t/grouping-search-results-in-elasticsearch/8836>\
**Category:** Elasticsearch\
**Created:** [August 23, 2012, 6:05pm UTC](https://discuss.elastic.co/t/grouping-search-results-in-elasticsearch/8836 "2012-08-23T18:05:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vibin](https://avatars.discourse-cdn.com/v4/letter/v/2bfe46/32.png) [@vibin](https://discuss.elastic.co/u/vibin)\
**Post date:** [August 23, 2012, 6:05pm UTC](https://discuss.elastic.co/t/grouping-search-results-in-elasticsearch/8836/1 "2012-08-23T18:05:36Z")

</div>

Hi All,

Is it possible to group the search results in elastic search by using the  
value in particular field (as in DB Query)?

Thanks,  
-Vibin

--

---

<div class="post-metadata">

**Author:** ![Keith\_Webster](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/keith_webster/32/1532_2.png) [@Keith\_Webster](https://discuss.elastic.co/u/Keith_Webster)\
**Post date:** [August 23, 2012, 7:33pm UTC](https://discuss.elastic.co/t/grouping-search-results-in-elasticsearch/8836/2 "2012-08-23T19:33:25Z")

</div>

Hey Vibin,

It's not currently possible as far as I know. Take a look at this thread:  
[https://groups.google.com/forum/?fromgroups=#!searchin/elasticsearch/group$20results/elasticsearch/ADD0aaKRM-c/lb\_LUweod20J](https://groups.google.com/forum/?fromgroups=#!searchin/elasticsearch/group$20results/elasticsearch/ADD0aaKRM-c/lb_LUweod20J)

On Thu, Aug 23, 2012 at 1:05 PM, vibin [vibindhas@gmail.com](mailto:vibindhas@gmail.com) wrote:

> Hi All,
> 
> Is it possible to group the search results in Elasticsearch by using the  
> value in particular field (as in DB Query)?
> 
> Thanks,  
> -Vibin
> 
> --

--

---

<div class="post-metadata">

**Author:** ![phill](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@phill](https://discuss.elastic.co/u/phill)\
**Post date:** [August 30, 2012, 3:30pm UTC](https://discuss.elastic.co/t/grouping-search-results-in-elasticsearch/8836/3 "2012-08-30T15:30:37Z")

</div>

Why doesn't sorting on several fields work?

If you really want one "row" representing a group, keep in mind that a  
group of documents is not a document, document retrieval is about find  
matching documents, not making up an ad hoc set of values based on  
interesting queries. One way around that is to design a document that  
has repeated fields, or children doc, or nested objects and then you can  
ask for hits on this larger doc that represents the group.

-Paul  
On 8/23/2012 11:05 AM, vibin wrote:

> Hi All,
> 
> Is it possible to group the search results in Elasticsearch by using  
> the value in particular field (as in DB Query)?
> 
> ## Thanks, -Vibin

--

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [August 31, 2012, 6:58am UTC](https://discuss.elastic.co/t/grouping-search-results-in-elasticsearch/8836/4 "2012-08-31T06:58:26Z")

</div>

Agreed. It is essential to prepare the documents beforehand when aggregated  
data must be represented. Note that parent/children docs, though organized  
as relational, is an expensive solution since top\_children/has\_children  
queries have to keep the doc ID loaded in memory or traverse the  
structures by query sequences. In short, it is not recommended to throw row  
data into in inverted index for aggregation because there is no such thing  
like SQL's "group by", "count", "sum" or "avg". You can't model  
Elasticsearch inverted indexes as a relational database, and it is strongly  
recommended to redesign the data model that should represent aggregated  
data.

Best regards,

Jörg

On Thursday, August 30, 2012 5:30:45 PM UTC+2, P Hill wrote:

> Why doesn't sorting on several fields work?
> 
> If you really want one "row" representing a group, keep in mind that a  
> group of documents is not a document, document retrieval is about find  
> matching documents, not making up an ad hoc set of values based on  
> interesting queries. One way around that is to design a document that  
> has repeated fields, or children doc, or nested objects and then you can  
> ask for hits on this larger doc that represents the group.
> 
> -Paul  
> On 8/23/2012 11:05 AM, vibin wrote:
> 
> > Hi All,
> > 
> > Is it possible to group the search results in Elasticsearch by using  
> > the value in particular field (as in DB Query)?
> > 
> > ## Thanks, -Vibin

--

---

<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, 3:14am UTC](https://discuss.elastic.co/t/grouping-search-results-in-elasticsearch/8836/5 "2017-07-06T03:14:28Z")

</div>


