searchSelections = new HashMap<Product, Boolean>();
}
private FullTextQuery searchQuery(String textQuery) {
QueryBuilder queryBuilder = entityManager.getSearchFactory()
.buildQueryBuilder().forEntity(Product.class).get();
//Hibernate Search fulltext query example:
//query to match exact terms occurrence, using custom boosts:
Query queryToFindExactTerms = queryBuilder.keyword()
.onFields("title").boostedTo(4f)
.andField("description").boostedTo(2f)
.andField("actors.name").boostedTo(2f)
.andField("categories.name").boostedTo(0.5f)
.matching(textQuery)
.createQuery();
//Similar query, but using NGram matching instead of exact terms:
Query queryToFindMathingNGrams = queryBuilder.keyword()
.onFields("title:ngrams").boostedTo(2f)
.andField("description:ngrams")
.andField("actors.name:ngrams")
.andField("categories.name:ngrams").boostedTo(0.2f)
.matching(textQuery)
.createQuery();
//Combine them for best results, note exact uses an higher boost:
Query fullTextQuery = queryBuilder.bool()
.should(queryToFindMathingNGrams)
.should(queryToFindExactTerms)
.createQuery();
log.info("Executing fulltext query {0}", fullTextQuery);