elasticsearch/elasticsearch — pull request #2326 by artemredkin (2012-10-14 12:48:21+00:00)
Title: Grouping prototype implementation

Hi!

I am trying to implement grouping feature and want to contribute to es. This is a proof-of-concept prototype to start conversation :), it is not meant to be pulled.

What do i mean by grouping?
Lets say i have 4 Documents:
{ "id" : 1, "name": "Doc1", "field" : "A" }
{ "id" : 2, "name": "Doc2", "field" : "A" }
{ "id" : 3, "name": "Doc3", "field" : "B" }
{ "id" : 4, "name": "Doc4", "field" : "B" }
I want to execute search that is something like this:
{ "query": { "match_all" : {}, "group", "field" }
With result:
{ "field" : {
"A" : { "hits" : { "id" : 1, "_source" : { "id" : 1, "name": "Doc1", "field" : "A" } },
{ "id" : 2, "_source" : { "id" : 2, "name": "Doc2", "field" : "A" } }
},
"B" : { "hits" : { "id" : 3, "_source" : { "id" : 3, "name": "Doc3", "field" : "B" } },
{ "id" : 4, "_source" : { "id" : 4, "name": "Doc4", "field" : "B" } }
}
}

Implementation.
First i looked at lucene-grouping module, but later decided to make a simple (slower?) one-pass solution based on Facets. Basically, in aggregator i collect docs in maps, and later - sort them and merge into one map.

The code is a work-in-progress for now, because i was unable to fit nicely it into current hierarchy (SearchPhaseController operates only with flat ScoreDocs array, or i have to reimplement whole Search stack from sratch).
Any thought?

Thank!