# How we implemented Group By in ES

**URL:** <https://discuss.elastic.co/t/how-we-implemented-group-by-in-es/9125>\
**Category:** Elasticsearch\
**Created:** [September 25, 2012, 5:18am UTC](https://discuss.elastic.co/t/how-we-implemented-group-by-in-es/9125 "2012-09-25T05:18:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sumit\_Guptaa](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@Sumit\_Guptaa](https://discuss.elastic.co/u/Sumit_Guptaa)\
**Post date:** [September 25, 2012, 5:18am UTC](https://discuss.elastic.co/t/how-we-implemented-group-by-in-es/9125/1 "2012-09-25T05:18:22Z")

</div>

Hi All,

I have the following document like:

"document\_id" : "abcdef"  
"domain" : "[sumitgupta.org](http://sumitgupta.org)"  
"url" : "[http://www.sumitgupta.org/](http://www.sumitgupta.org/)"  
"document\_title" : "sumitgupta - -tring to implement sql group by"  
"document\_text" : "Hello guys how r you"

When searching, I would like to display the results 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.

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

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

Regards,  
Sumit Gupta

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [September 25, 2012, 5:26am UTC](https://discuss.elastic.co/t/how-we-implemented-group-by-in-es/9125/2 "2012-09-25T05:26:14Z")

</div>

When you Group by in SQL, you count values by type.  
You can use Terms Facet to get the same kind of result.

For your use case, you will have to modify the default analyzer for domain field.

HTH

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 25 sept. 2012 à 07:18, Sumit Guptaa [sumit.gupta.ngi@gmail.com](mailto:sumit.gupta.ngi@gmail.com) a écrit :

Hi All,

I have the following document like:

"document\_id" : "abcdef"  
"domain" : "sumitgupta.org"  
"url" : "[http://www.sumitgupta.org/](http://www.sumitgupta.org/)"  
"document\_title" : "sumitgupta - -tring to implement sql group by"  
"document\_text" : "Hello guys how r you"

When searching, I would like to display the results 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.

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

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

Regards,  
Sumit Gupta

--  
View this message in context: [http://elasticsearch-users.115913.n3.nabble.com/How-we-implemented-Group-By-in-ES-tp4023084.html](http://elasticsearch-users.115913.n3.nabble.com/How-we-implemented-Group-By-in-ES-tp4023084.html)  
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, 3:11am UTC](https://discuss.elastic.co/t/how-we-implemented-group-by-in-es/9125/3 "2017-07-06T03:11:36Z")

</div>


