I’m using an n-gram filter to add a suggestions/autocomplete feature. I’m coming across the issue where, since field values can be duplicated, the same value is being suggested multiple times.
For instance, if my db contained aB, aB, aB, aB, aB, aC, aD and I retrieved the first 5 from the n-gram search, I would get aB 5 times.
I want to retrieve the first fiveunique values (aB, aC, aD). Is this possible?
The current search code is very simple; just retrieving the first five:
SearchResult<Entity> result = searchSession.search(Entity.class).where(entity -> entity.match().field("autocomplete_field").matching(fieldValue)).fetch(5);
List<Entity> results = result.hits();
I don’t think there is a select distinct field from ... alternative in full-text search.
If you just need to get the unique values of a single field, you could try using the terms aggregation: Hibernate Search 7.1.1.Final: Reference Documentation :