# Group By Field Value

**URL:** <https://discuss.elastic.co/t/group-by-field-value/4286>\
**Category:** Elasticsearch\
**Created:** [April 25, 2011, 6:45am UTC](https://discuss.elastic.co/t/group-by-field-value/4286 "2011-04-25T06:45:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![poncho](https://avatars.discourse-cdn.com/v4/letter/p/59ef9b/32.png) [@poncho](https://discuss.elastic.co/u/poncho)\
**Post date:** [April 25, 2011, 6:45am UTC](https://discuss.elastic.co/t/group-by-field-value/4286/1 "2011-04-25T06:45:44Z")

</div>

Hi All,

I'm moving from MySQL to ElasticSearch. I'm loving ElasticSearch's speed and ease of setup, well done!

Using ElasticSearch, I'm creating a database of websites that have the following fields:  
document\_id (i.e. abc123)  
domain (i.e. [elasticsearch.org](http://elasticsearch.org) )  
url (i.e. [http://www.elasticsearch.org/](http://www.elasticsearch.org/) )  
document\_title (i.e. elasticsearch - - Open Source, Distributed, RESTful, Search Engine)  
document\_text (i.e. You know, for Search. So, we build a web site or an app....)

When searching, I would like to display the results (sorted by score) and group the results by domain. So i'm trying to be fair and show 10 different domains in the result rather than showing 10 results from the same domain. If you search on Google you'll know what I mean.

In SQL I could achieve this using the following query:  
SELECT \* FROM table WHERE document\_title LIKE '%elasticsearch%' GROUP BY domain

How could I get a similar GROUP BY results with elasticsearch.

Cheers Poncho

---

<div class="post-metadata">

**Author:** ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)\
**Post date:** [April 26, 2011, 5:25pm UTC](https://discuss.elastic.co/t/group-by-field-value/4286/2 "2011-04-26T17:25:31Z")

</div>

That is not possible today:

> <https://github.com/elastic/elasticsearch/issues/256>
>
> Ability to collapse on a field. For example, I want the most relevant result fro…m all different report types. Or similarly, the most recent result of each report type. Or maybe, I want to de-dup on headline. 
> 
> So, the sort order would dictate which one from the group is returned. Similar to what is discussed here:
> http://blog.jteam.nl/2009/10/20/result-grouping-field-collapsing-with-solr/
> 
> From my understanding, it seems that in order for field collapsing to be efficient, the result set must be relatively small.
> 
> This is also referred to as "Combine" on some other search products.

but you can do it on the client side (fetch up some more docs and  
collapse by yourself)

Another way would be to index domains not documents ... but that is  
probably not what you want.

Regards,  
Peter.

On Apr 25, 8:45 am, poncho [nat...@noisefm.com.au](mailto:nat...@noisefm.com.au) wrote:

> Hi All,
> 
> I'm moving from MySQL to Elasticsearch. I'm loving Elasticsearch's speed and  
> ease of setup, well done!
> 
> Using Elasticsearch, I'm creating a database of websites that have the  
> following fields:  
> document\_id (i.e. abc123)  
> domain (i.e. [elasticsearch.org](http://elasticsearch.org) )  
> url (i.e.[http://www.elasticsearch.org/](http://www.elasticsearch.org/))  
> document\_title (i.e. elasticsearch - - Open Source, Distributed, RESTful,  
> Search Engine)  
> document\_text (i.e. You know, for Search. So, we build a web site or an  
> app....)
> 
> When searching, I would like to display the results (sorted by score) and  
> group the results by domain. So i'm trying to be fair and show 10 different  
> domains in the result rather than showing 10 results from the same domain.  
> If you search on Google you'll know what I mean.
> 
> In SQL I could achieve this using the following query:  
> SELECT \* FROM table WHERE document\_title LIKE '%elasticsearch%' GROUP BY  
> domain
> 
> How could I get a similar GROUP BY results with elasticsearch.
> 
> Cheers Poncho
> 
> --  
> View this message in context:[http://elasticsearch-users.115913.n3.nabble.com/Group-By-Field-Value-](http://elasticsearch-users.115913.n3.nabble.com/Group-By-Field-Value-)...  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![poncho](https://avatars.discourse-cdn.com/v4/letter/p/59ef9b/32.png) [@poncho](https://discuss.elastic.co/u/poncho)\
**Post date:** [April 30, 2011, 2:15pm UTC](https://discuss.elastic.co/t/group-by-field-value/4286/3 "2011-04-30T14:15:17Z")

</div>

Thanks Peter, I am looking into storing domains so this might be a solution.

Alternatively, after reading up on the parent-child feature of ES, could this be something I could use.

The way I look at this is I have a domain (i.e. [blahblah.com](http://blahblah.com)), then under this [blahblah.com](http://blahblah.com) domain I could have many documents (i.e. [blahblah.com/somepage.html](http://blahblah.com/somepage.html), [blahblah.com/index.html](http://blahblah.com/index.html), [blahblah.com/someotherpage.php](http://blahblah.com/someotherpage.php)).

Would making these document children work?

Therefore, could I search on children documents to return the parent document?

Thanks again!

---

<div class="post-metadata">

**Author:** ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)\
**Post date:** [May 1, 2011, 6:37pm UTC](https://discuss.elastic.co/t/group-by-field-value/4286/4 "2011-05-01T18:37:50Z")

</div>

Sorry, I'm not familiar with the child parent things and API. Maybe  
you could ask on freenode?

On Apr 30, 4:15 pm, poncho [nat...@noisefm.com.au](mailto:nat...@noisefm.com.au) wrote:

> Thanks Peter, I am looking into storing domains so this might be a solution.
> 
> Alternatively, after reading up on the parent-child feature of ES, could  
> this be something I could use.
> 
> The way I look at this is I have a domain (i.e. [blahblah.com](http://blahblah.com)), then under  
> this [blahblah.com](http://blahblah.com) domain I could have many documents (i.e.  
> [blahblah.com/somepage.html](http://blahblah.com/somepage.html), [blahblah.com/index.html](http://blahblah.com/index.html),  
> [blahblah.com/someotherpage.php](http://blahblah.com/someotherpage.php)).
> 
> Would making these document children work?
> 
> Therefore, could I search on children documents to return the parent  
> document?
> 
> Thanks again!--  
> View this message in context:[http://elasticsearch-users.115913.n3.nabble.com/Group-By-Field-Value-](http://elasticsearch-users.115913.n3.nabble.com/Group-By-Field-Value-)...  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<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, 4:07am UTC](https://discuss.elastic.co/t/group-by-field-value/4286/5 "2017-07-06T04:07:13Z")

</div>


