In Chapter 3, Performing Queries, we saw how to boost a field's relevance at index time, on either a fixed or a dynamic basis. It is also possible to dynamically change the weight at query time.
All query types in the Hibernate Search DSL include the onField
and andField
methods. For each query type, these two clauses also support a
boostedTo
method, taking a weight
factor as a float
parameter. Whatever the index-time weight of that field might be, adding a boostedTo
clause multiplies it by the indicated number:
... luceneQuery = queryBuilder .phrase() .onField("name").boostedTo(2) .andField("description").boostedTo(2) .andField("supportedDevices.name") .andField("customerReviews.comments") .sentence(unquotedSearchString) .createQuery(); ...
In this chapter's version of the VAPORware Marketplace application, query-time boosting is now added to the "exact phrase" use case. When users wraps their search string in double quotes to search by phrase rather than by keywords, we want to give the App
entity's name and description field even more weight than normal. The highlighted changes double the index-time weight of those two fields, but only for the exact phrase query rather than for all query types.