Boost Document Scores for Hits in a Certain Property

By default, a search query with a constraint that matches documents containing specific text assigns equal weight to all properties. For example, the following query searches all properties of each document:

// Build a HstQuery with a "contains" constraint (the "." selector searches all properties) final HstQuery hstQuery = HstQueryBuilder.create(scope) .ofTypes(BaseDocument.class) .where(constraint(".").contains(query)) .build();

This query returns all documents where query appears in any property or in any property of descendant nodes (such as compound fields). Each match, regardless of property, contributes equally to the document's score. For example, a match in the title property is weighted the same as a match in the body property, even if the body contains much more text.

To increase the score of documents that match the query in a specific property—such as title—add an additional contains constraint targeting that property. Combine this constraint with the general constraint using an OR operator. This approach boosts the score for documents where the query matches the title property.

// Build an HstQuery with OR-ed "contains" constraints for the title property // and all properties. Documents with a match in the title will score higher // than those with matches only in other properties. final HstQuery hstQuery = HstQueryBuilder.create(scope) .ofTypes(BaseDocument.class) .where( or( constraint("myproject:title").contains(parsedQuery), constraint(".").contains(parsedQuery) ) ) .build();

With this configuration, documents that match the query in the title property are ranked higher than documents that match only in other properties. This allows you to prioritize matches in specific fields when scoring search results.

Share Feedback
Page: /build/search/jcr-search/boost-document-scores-for-hits-in-some-property
Section: Build
Category *
Boost Document Scores for Hits in a Certain Property | Bloomreach Content Documentation