O'Reilly logo

Hibernate Search by Example by Steve Perkins

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Query-time boosting

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(); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required