Hello with the last Hibernate version we implemented the autocomplete method as follows:
public List<String> getAllSuggestions(final String searchString) {
IndexReader reader = null;
try {
final FullTextEntityManager fullTextEntityManager = Search
.getFullTextEntityManager(emf.createEntityManager());
reader = fullTextEntityManager.getSearchFactory().getIndexReaderAccessor().open(Entity.class);
final Terms firstTerms= SlowCompositeReaderWrapper.wrap(reader)
.terms("firstEntityField");
final Terms secondTerms= SlowCompositeReaderWrapper.wrap(reader)
.terms("secondEntityField");
final Terms thirdTerms= SlowCompositeReaderWrapper.wrap(reader)
.terms("thirdEntityField");
//All words are written in a set so that there are no duplicate entries (Helper-Method below)
final Set<String> allTerms = new HashSet<>();
addTermsToSet(firstTerms, allTerms);
addTermsToSet(secondTerms, allTerms);
addTermsToSet(thirdTerms, allTerms);
final List<String> suggestions = allTerms.stream().sorted(Comparator.naturalOrder())
.collect(Collectors.toList());
return suggestions.stream().filter(s -> s.startsWith(searchString) && !s.equalsIgnoreCase(searchString))
.limit(SUGGESTION_LIMIT)
.collect(Collectors.toList());
} catch (final Exception e) {
LOG.warn("Terms for autocomplete function couldn't be loaded.");
} finally {
if (reader != null) {
try {
reader.close();
} catch (final Exception readerException) {
"..."
}
}
}
return new ArrayList<>();
}
// Helper Method for Set
private void addTermsToSet(final Terms terms, final Set<String> set) throws IOException {
final BytesRefIterator iterator;
iterator = terms.iterator();
BytesRef byteRef = null;
while ((byteRef = iterator.next()) != null) {
set.add(byteRef.utf8ToString());
}
}
Unfortunately I couldn’t find any further information on how I could do something like this with
Hibernate-Search 6.
Do you have any idea?
Thank you!