My use case is grouping the search result by a field. We are developing an web application with hibernate search as the search engine. The application is some kind of a customer relationship management system where the user can search through the customer contacts. The contacts are displayed in a table. The results can be grouped by a contract number. At first only the groups shall be displayed and within the row some of the entity fields of the first entity. The elements within the group are lazy loaded on user request.
Group the search result by at least one field
Calculate the hit count for every group
Calculate the total hit count
Calculate the total group count
Load the data of every group within the search request (without firing another search)
Limit the loaded entites per group
Limit the count of extracted groups
Sort the groups on a field which is not the the grouping field
Sort within the group by different field
In solr this feature is known as field collapsing http://wiki.apache.org/solr/FieldCollapsing and has been discused in the hibernate forum for example here https://forum.hibernate.org/viewtopic.php?f=9&t=1005522&view=next.
Lucene provides the grouping function by several collectors and a utility class (which uses the collector itself). My implementation makes use of the provided collectors and integrates them in the QueryHits class of hibernate search. It provides the most of the requirements.
For me the most challenging part was the API for requesting the results. For now I could manage to extract the EntityInfo from the search index. That would fit my needs because in my use case I'm not interested in loading the full entity.