This is probably not the right way to do it. Maybe look into that instead:
- Elasticsearch: Hibernate Search 6 suggesters + completion type
- Lucene (old but maybe still a bit relevant): [HSEARCH-1823] - Hibernate JIRA
There is not in Hibernate Search at the moment, but there could be in the future: [HSEARCH-868] - Hibernate JIRA
If you use Elasticsearch, you can probably leverage the relevant feature through the Elasticsearch extension in Hibernate Search, but that involves dealing with JSON.
It’s a good solution if:
- the field is a keyword field; aggregations are not available on full-text fields.
- the orderings available in terms aggregations are what you’re after when you say “first five”.
Somehow I doubt it.