# Bucketing elasticsearch results?

**URL:** https://discuss.elastic.co/t/bucketing-elasticsearch-results/5602
**Category:** Elasticsearch
**Created:** [October 14, 2011, 2:38pm UTC](https://discuss.elastic.co/t/bucketing-elasticsearch-results/5602 "2011-10-14T14:38:08Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Jon\_Paul\_Lussier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jon_paul_lussier/32/1696_2.png) [@Jon\_Paul\_Lussier](https://discuss.elastic.co/u/Jon_Paul_Lussier)
#### Post date: [October 14, 2011, 2:38pm UTC](https://discuss.elastic.co/t/bucketing-elasticsearch-results/5602/1 "2011-10-14T14:38:08Z")

</div>

Hey, first of all, I'd like to preface this by saying I'm fairly  
inexperienced with Elasticsearch or implementing search in general;  
I'm an intermediate ruby/rails developer using the Tire gem; and this  
question has been a headache for at least 8 hours.

I need to essentially, query the 'most relevant N(where N is a small  
number, 3-7) 'listing' documents for each 'organization'' -- ideally,  
this would be some kind of bucketed result set(e.g. hits =\>  
{:organization\_1 =\> {:matching\_listings =\> '...' }, :organization\_2 =\>  
{:etc =\> '...'}} )

I'm not sure if elasticsearch /can/ do this, it seems like I might  
need to get facet counts for my query over a global scope, and run the  
same query to return a limited number of documents for /each/ facet  
term.

Am I totally off base here? Is there something more/less built in to  
Elasticsearch to enable this?

Appreciate any help you guys can extend; hopefully I stumble on to a  
reasonable solution soon. Thanks.

---

<div class="post-metadata">

### Author: ![vineeth\_mohan](https://avatars.discourse-cdn.com/v4/letter/v/bc79bd/32.png) [@vineeth\_mohan](https://discuss.elastic.co/u/vineeth_mohan)
#### Post date: [October 14, 2011, 2:54pm UTC](https://discuss.elastic.co/t/bucketing-elasticsearch-results/5602/2 "2011-10-14T14:54:10Z")

</div>

What is your criteria for "most relevant N" documents.  
As in what qualifies a document to be relevent ?

Thanks  
Vineeth

On Fri, Oct 14, 2011 at 8:08 PM, Jon-Paul Lussier \<[jonpaul.lussier@gmail.com](mailto:jonpaul.lussier@gmail.com)

> wrote:

> Hey, first of all, I'd like to preface this by saying I'm fairly  
> inexperienced with Elasticsearch or implementing search in general;  
> I'm an intermediate ruby/rails developer using the Tire gem; and this  
> question has been a headache for at least 8 hours.
> 
> I need to essentially, query the 'most relevant N(where N is a small  
> number, 3-7) 'listing' documents for each 'organization'' -- ideally,  
> this would be some kind of bucketed result set(e.g. hits =\>  
> {:organization\_1 =\> {:matching\_listings =\> '...' }, :organization\_2 =\>  
> {:etc =\> '...'}} )
> 
> I'm not sure if elasticsearch /can/ do this, it seems like I might  
> need to get facet counts for my query over a global scope, and run the  
> same query to return a limited number of documents for /each/ facet  
> term.
> 
> Am I totally off base here? Is there something more/less built in to  
> Elasticsearch to enable this?
> 
> Appreciate any help you guys can extend; hopefully I stumble on to a  
> reasonable solution soon. Thanks.

---

<div class="post-metadata">

### Author: ![vineeth\_mohan](https://avatars.discourse-cdn.com/v4/letter/v/bc79bd/32.png) [@vineeth\_mohan](https://discuss.elastic.co/u/vineeth_mohan)
#### Post date: [October 14, 2011, 2:57pm UTC](https://discuss.elastic.co/t/bucketing-elasticsearch-results/5602/3 "2011-10-14T14:57:14Z")

</div>

Are you looking for something like "group by" in mysql.

On Fri, Oct 14, 2011 at 8:24 PM, Vineeth Mohan [vineethmohan@algotree.com](mailto:vineethmohan@algotree.com)wrote:

> What is your criteria for "most relevant N" documents.  
> As in what qualifies a document to be relevent ?
> 
> Thanks  
> Vineeth
> 
> On Fri, Oct 14, 2011 at 8:08 PM, Jon-Paul Lussier \<  
> [jonpaul.lussier@gmail.com](mailto:jonpaul.lussier@gmail.com)\> wrote:
> 
> > Hey, first of all, I'd like to preface this by saying I'm fairly  
> > inexperienced with Elasticsearch or implementing search in general;  
> > I'm an intermediate ruby/rails developer using the Tire gem; and this  
> > question has been a headache for at least 8 hours.
> > 
> > I need to essentially, query the 'most relevant N(where N is a small  
> > number, 3-7) 'listing' documents for each 'organization'' -- ideally,  
> > this would be some kind of bucketed result set(e.g. hits =\>  
> > {:organization\_1 =\> {:matching\_listings =\> '...' }, :organization\_2 =\>  
> > {:etc =\> '...'}} )
> > 
> > I'm not sure if elasticsearch /can/ do this, it seems like I might  
> > need to get facet counts for my query over a global scope, and run the  
> > same query to return a limited number of documents for /each/ facet  
> > term.
> > 
> > Am I totally off base here? Is there something more/less built in to  
> > Elasticsearch to enable this?
> > 
> > Appreciate any help you guys can extend; hopefully I stumble on to a  
> > reasonable solution soon. Thanks.

---

<div class="post-metadata">

### Author: ![Jon\_Paul\_Lussier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jon_paul_lussier/32/1696_2.png) [@Jon\_Paul\_Lussier](https://discuss.elastic.co/u/Jon_Paul_Lussier)
#### Post date: [October 14, 2011, 3:16pm UTC](https://discuss.elastic.co/t/bucketing-elasticsearch-results/5602/4 "2011-10-14T15:16:01Z")

</div>

The query parameters from a user, in this case. The 'most' qualified  
would be the highest scoring.

On Oct 14, 10:57 am, Vineeth Mohan [vineethmo...@algotree.com](mailto:vineethmo...@algotree.com) wrote:

> Are you looking for something like "group by" in mysql.
> 
> On Fri, Oct 14, 2011 at 8:24 PM, Vineeth Mohan [vineethmo...@algotree.com](mailto:vineethmo...@algotree.com)wrote:
> 
> > What is your criteria for "most relevant N" documents.  
> > As in what qualifies a document to be relevent ?
> 
> > Thanks  
> > Vineeth
> 
> > On Fri, Oct 14, 2011 at 8:08 PM, Jon-Paul Lussier \<  
> > [jonpaul.luss...@gmail.com](mailto:jonpaul.luss...@gmail.com)\> wrote:
> 
> > > Hey, first of all, I'd like to preface this by saying I'm fairly  
> > > inexperienced with Elasticsearch or implementing search in general;  
> > > I'm an intermediate ruby/rails developer using the Tire gem; and this  
> > > question has been a headache for at least 8 hours.
> 
> > > I need to essentially, query the 'most relevant N(where N is a small  
> > > number, 3-7) 'listing' documents for each 'organization'' -- ideally,  
> > > this would be some kind of bucketed result set(e.g. hits =\>  
> > > {:organization\_1 =\> {:matching\_listings =\> '...' }, :organization\_2 =\>  
> > > {:etc =\> '...'}} )
> 
> > > I'm not sure if elasticsearch /can/ do this, it seems like I might  
> > > need to get facet counts for my query over a global scope, and run the  
> > > same query to return a limited number of documents for /each/ facet  
> > > term.
> 
> > > Am I totally off base here? Is there something more/less built in to  
> > > Elasticsearch to enable this?
> 
> > > Appreciate any help you guys can extend; hopefully I stumble on to a  
> > > reasonable solution soon. Thanks.

---

<div class="post-metadata">

### Author: ![Jon\_Paul\_Lussier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jon_paul_lussier/32/1696_2.png) [@Jon\_Paul\_Lussier](https://discuss.elastic.co/u/Jon_Paul_Lussier)
#### Post date: [October 14, 2011, 3:23pm UTC](https://discuss.elastic.co/t/bucketing-elasticsearch-results/5602/5 "2011-10-14T15:23:36Z")

</div>

I believe I'm looking for something that would give me the same result  
as group by (more or less, I'm not particuarly adept with data  
stores); though I'm honestly not exactly sure.

On Oct 14, 10:57 am, Vineeth Mohan [vineethmo...@algotree.com](mailto:vineethmo...@algotree.com) wrote:

> Are you looking for something like "group by" in mysql.
> 
> On Fri, Oct 14, 2011 at 8:24 PM, Vineeth Mohan [vineethmo...@algotree.com](mailto:vineethmo...@algotree.com)wrote:
> 
> > What is your criteria for "most relevant N" documents.  
> > As in what qualifies a document to be relevent ?
> 
> > Thanks  
> > Vineeth
> 
> > On Fri, Oct 14, 2011 at 8:08 PM, Jon-Paul Lussier \<  
> > [jonpaul.luss...@gmail.com](mailto:jonpaul.luss...@gmail.com)\> wrote:
> 
> > > Hey, first of all, I'd like to preface this by saying I'm fairly  
> > > inexperienced with Elasticsearch or implementing search in general;  
> > > I'm an intermediate ruby/rails developer using the Tire gem; and this  
> > > question has been a headache for at least 8 hours.
> 
> > > I need to essentially, query the 'most relevant N(where N is a small  
> > > number, 3-7) 'listing' documents for each 'organization'' -- ideally,  
> > > this would be some kind of bucketed result set(e.g. hits =\>  
> > > {:organization\_1 =\> {:matching\_listings =\> '...' }, :organization\_2 =\>  
> > > {:etc =\> '...'}} )
> 
> > > I'm not sure if elasticsearch /can/ do this, it seems like I might  
> > > need to get facet counts for my query over a global scope, and run the  
> > > same query to return a limited number of documents for /each/ facet  
> > > term.
> 
> > > Am I totally off base here? Is there something more/less built in to  
> > > Elasticsearch to enable this?
> 
> > > Appreciate any help you guys can extend; hopefully I stumble on to a  
> > > reasonable solution soon. Thanks.

---

<div class="post-metadata">

### Author: ![vineeth\_mohan](https://avatars.discourse-cdn.com/v4/letter/v/bc79bd/32.png) [@vineeth\_mohan](https://discuss.elastic.co/u/vineeth_mohan)
#### Post date: [October 14, 2011, 4:49pm UTC](https://discuss.elastic.co/t/bucketing-elasticsearch-results/5602/6 "2011-10-14T16:49:01Z")

</div>

I am not sure if we can get this using a single hit to ES.

one way wud b using faceted search.  
Using faceted search you will get the companies which are included in the  
results.  
Once you get this , you can again first request for each company with the  
same search query. HTH

Thanks  
Vineeth

On Fri, Oct 14, 2011 at 8:53 PM, Jon-Paul Lussier \<[jonpaul.lussier@gmail.com](mailto:jonpaul.lussier@gmail.com)

> wrote:

> I believe I'm looking for something that would give me the same result  
> as group by (more or less, I'm not particuarly adept with data  
> stores); though I'm honestly not exactly sure.
> 
> On Oct 14, 10:57 am, Vineeth Mohan [vineethmo...@algotree.com](mailto:vineethmo...@algotree.com) wrote:
> 
> > Are you looking for something like "group by" in mysql.
> > 
> > On Fri, Oct 14, 2011 at 8:24 PM, Vineeth Mohan \<  
> > [vineethmo...@algotree.com](mailto:vineethmo...@algotree.com)\>wrote:
> > 
> > > What is your criteria for "most relevant N" documents.  
> > > As in what qualifies a document to be relevent ?
> > 
> > > Thanks  
> > > Vineeth
> > 
> > > On Fri, Oct 14, 2011 at 8:08 PM, Jon-Paul Lussier \<  
> > > [jonpaul.luss...@gmail.com](mailto:jonpaul.luss...@gmail.com)\> wrote:
> > 
> > > > Hey, first of all, I'd like to preface this by saying I'm fairly  
> > > > inexperienced with Elasticsearch or implementing search in general;  
> > > > I'm an intermediate ruby/rails developer using the Tire gem; and this  
> > > > question has been a headache for at least 8 hours.
> > 
> > > > I need to essentially, query the 'most relevant N(where N is a small  
> > > > number, 3-7) 'listing' documents for each 'organization'' -- ideally,  
> > > > this would be some kind of bucketed result set(e.g. hits =\>  
> > > > {:organization\_1 =\> {:matching\_listings =\> '...' }, :organization\_2 =\>  
> > > > {:etc =\> '...'}} )
> > 
> > > > I'm not sure if elasticsearch /can/ do this, it seems like I might  
> > > > need to get facet counts for my query over a global scope, and run the  
> > > > same query to return a limited number of documents for /each/ facet  
> > > > term.
> > 
> > > > Am I totally off base here? Is there something more/less built in to  
> > > > Elasticsearch to enable this?
> > 
> > > > Appreciate any help you guys can extend; hopefully I stumble on to a  
> > > > reasonable solution soon. Thanks.

---

<div class="post-metadata">

### Author: ![Jon\_Paul\_Lussier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jon_paul_lussier/32/1696_2.png) [@Jon\_Paul\_Lussier](https://discuss.elastic.co/u/Jon_Paul_Lussier)
#### Post date: [October 14, 2011, 4:56pm UTC](https://discuss.elastic.co/t/bucketing-elasticsearch-results/5602/7 "2011-10-14T16:56:17Z")

</div>

Fair enough, this is what I'm thinking will have to happen. It's a  
little unlucky that I have to run the query twice, but so far  
Elasticsearch /seems/ fast enough that it won't matter at all.

Thanks a lot for your time and input; if anyone else has some ideas,  
feel free to share them. Appreciated.

On Oct 14, 12:49 pm, Vineeth Mohan [vineethmo...@algotree.com](mailto:vineethmo...@algotree.com) wrote:

> I am not sure if we can get this using a single hit to ES.
> 
> one way wud b using faceted search.  
> Using faceted search you will get the companies which are included in the  
> results.  
> Once you get this , you can again first request for each company with the  
> same search query. HTH
> 
> Thanks  
> Vineeth
> 
> On Fri, Oct 14, 2011 at 8:53 PM, Jon-Paul Lussier \<[jonpaul.luss...@gmail.com](mailto:jonpaul.luss...@gmail.com)
> 
> > wrote:  
> > I believe I'm looking for something that would give me the same result  
> > as group by (more or less, I'm not particuarly adept with data  
> > stores); though I'm honestly not exactly sure.
> 
> > On Oct 14, 10:57 am, Vineeth Mohan [vineethmo...@algotree.com](mailto:vineethmo...@algotree.com) wrote:
> > 
> > > Are you looking for something like "group by" in mysql.
> 
> > > On Fri, Oct 14, 2011 at 8:24 PM, Vineeth Mohan \<  
> > > [vineethmo...@algotree.com](mailto:vineethmo...@algotree.com)\>wrote:
> 
> > > > What is your criteria for "most relevant N" documents.  
> > > > As in what qualifies a document to be relevent ?
> 
> > > > Thanks  
> > > > Vineeth
> 
> > > > On Fri, Oct 14, 2011 at 8:08 PM, Jon-Paul Lussier \<  
> > > > [jonpaul.luss...@gmail.com](mailto:jonpaul.luss...@gmail.com)\> wrote:
> 
> > > > > Hey, first of all, I'd like to preface this by saying I'm fairly  
> > > > > inexperienced with Elasticsearch or implementing search in general;  
> > > > > I'm an intermediate ruby/rails developer using the Tire gem; and this  
> > > > > question has been a headache for at least 8 hours.
> 
> > > > > I need to essentially, query the 'most relevant N(where N is a small  
> > > > > number, 3-7) 'listing' documents for each 'organization'' -- ideally,  
> > > > > this would be some kind of bucketed result set(e.g. hits =\>  
> > > > > {:organization\_1 =\> {:matching\_listings =\> '...' }, :organization\_2 =\>  
> > > > > {:etc =\> '...'}} )
> 
> > > > > I'm not sure if elasticsearch /can/ do this, it seems like I might  
> > > > > need to get facet counts for my query over a global scope, and run the  
> > > > > same query to return a limited number of documents for /each/ facet  
> > > > > term.
> 
> > > > > Am I totally off base here? Is there something more/less built in to  
> > > > > Elasticsearch to enable this?
> 
> > > > > Appreciate any help you guys can extend; hopefully I stumble on to a  
> > > > > reasonable solution soon. Thanks.

---

<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: [October 17, 2011, 3:19pm UTC](https://discuss.elastic.co/t/bucketing-elasticsearch-results/5602/8 "2011-10-17T15:19:44Z")

</div>

this needs to be implemented in an efficient manner (but can be simply  
implemented on the client side):

> <https://github.com/elastic/elasticsearch/issues/256>

---

<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:51am UTC](https://discuss.elastic.co/t/bucketing-elasticsearch-results/5602/9 "2017-07-06T03:51:46Z")

</div>


