# ElasticSearch and grouping

**URL:** <https://discuss.elastic.co/t/elasticsearch-and-grouping/9428>\
**Category:** Elasticsearch\
**Created:** [October 20, 2012, 9:59am UTC](https://discuss.elastic.co/t/elasticsearch-and-grouping/9428 "2012-10-20T09:59:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicolas\_Blanc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nicolas_blanc/32/2030_2.png) [@Nicolas\_Blanc](https://discuss.elastic.co/u/Nicolas_Blanc)\
**Post date:** [October 20, 2012, 9:59am UTC](https://discuss.elastic.co/t/elasticsearch-and-grouping/9428/1 "2012-10-20T09:59:33Z")

</div>

Hi the list!

Some days ago, i started to work on ES, and grouping function. Today i have  
something which work, thx to Martijn and his experimental grouping code.

I adapt Martijn code to the 0.19.4 version (thx also to [lusini.de](http://lusini.de)).

Based on that code, i added more facets to manage grouping (for now  
date\_histogram, histogram in normal mode (i.e. without script ou key/value  
or bound), and range). And it works. I will add more facets soon, as my  
company want to use more of them.

After that, i worked on a version which will not be tied to 1 shard. First  
i tried a full global grouping. I have a branch with that, and it works.  
But it's a nightmare to have valid facets. So i won't work on this feature  
for some times.

My second great adventure was to test my patched martijn's code on multiple  
shards, with all items to group in the same shard (i use the parent/child  
functionnality to achieve this goal). The grouping works, and facets are  
good too. But i had a problem as soon as i want the result to be sorted.  
So my question is :  
Is a developper in the group, who can explain me how i can Merge and sort  
docs from all shards ? It's the last problem which can prevent me using ES  
in production 🙂

Thx in advance!

--  
Nicolas BLANC.

P.S.: If Some of you want it, i can share my code which add grouping to  
Range, Histogram and DateHistogram facets.

--

---

<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:** [October 22, 2012, 10:28am UTC](https://discuss.elastic.co/t/elasticsearch-and-grouping/9428/2 "2012-10-22T10:28:52Z")

</div>

Hi Nicolas,

well, I would love to have a look at your code!

Without knowing much in detail about the grouping patch state as it is now,  
the merge/sort step to build groups out of shards is not easy.

You have to introduce a group function, which shouldn't be necessarily a  
sort function, more a collapse function that could allow round-robin group  
building, that is, look at each shard, apply the group feature extraction,  
compare to current group, collapse or not, and proceed to iterate over the  
shards until last document is grouped. This is expensive. Sorting groups is  
soon going to be even more expensive because the whole result set would  
have to be examined. ☹

But, I think, a sort should be similar to facet sorting implementation.  
Sorting facets works that each shard performs faceting on its own and  
pushes the result up to the node level, transporting the required sort  
parameters, and on that level, the collected facets are sorted again.

Cheers,

Jörg

On Saturday, October 20, 2012 11:59:33 AM UTC+2, Nicolas Blanc wrote:

> Hi the list!
> 
> Some days ago, i started to work on ES, and grouping function. Today i  
> have something which work, thx to Martijn and his experimental grouping  
> code.
> 
> I adapt Martijn code to the 0.19.4 version (thx also to [lusini.de](http://lusini.de)).
> 
> Based on that code, i added more facets to manage grouping (for now  
> date\_histogram, histogram in normal mode (i.e. without script ou key/value  
> or bound), and range). And it works. I will add more facets soon, as my  
> company want to use more of them.
> 
> After that, i worked on a version which will not be tied to 1 shard. First  
> i tried a full global grouping. I have a branch with that, and it works.  
> But it's a nightmare to have valid facets. So i won't work on this feature  
> for some times.
> 
> My second great adventure was to test my patched martijn's code on  
> multiple shards, with all items to group in the same shard (i use the  
> parent/child functionnality to achieve this goal). The grouping works, and  
> facets are good too. But i had a problem as soon as i want the result to be  
> sorted.  
> So my question is :  
> Is a developper in the group, who can explain me how i can Merge and sort  
> docs from all shards ? It's the last problem which can prevent me using ES  
> in production 🙂
> 
> Thx in advance!
> 
> --  
> Nicolas BLANC.
> 
> P.S.: If Some of you want it, i can share my code which add grouping to  
> Range, Histogram and DateHistogram facets.

--

---

<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:07am UTC](https://discuss.elastic.co/t/elasticsearch-and-grouping/9428/3 "2017-07-06T03:07:36Z")

</div>


