Grouping the search result by index field (field collapsing)
Activity
Show:
Details
Details
Assignee
Unassigned
UnassignedReporter
Sascha Grebe
Sascha GrebeComponents
Priority
Created February 5, 2015 at 10:05 PM
Updated September 25, 2023 at 2:48 PM
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.
Requirements
Group the search result by at least one field
Support paging
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
Implementation
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.