Boost document scores for hits in some property - BloomReach Experience - Open Source CMS

This article covers a Hippo CMS version 10. There's an updated version available that covers our most recent release.

15-05-2015

Boost document scores for hits in some property

When you have a query with a constraint to only show documents containing some text, most likely, you'll have something like this:

// create a new Filter and set it on the HstQuery
Filter filter = hstQuery.createFilter();
hstQuery.setFilter(filter);

// add to the filter a 'query' constraint. "."
// means to search in the entire document
filter.addContains(".", query);

This will return all documents that contain query in one of its properties or one of the properties of descendant nodes (for compound type fields). Every hit in every property will be treated with equal weight. Thus, a hit in say the title property has a weight equal to a hit in the document's body, which may contain a large amount of text. If you want to boost the weight of a hit in a certain property (such as title) to get a better scoring for documents that match the query in their title, you can achieve this by OR-ing two filters:

// create a new Filter and set it on the HstQuery
Filter filter = hstQuery.createFilter();
hstQuery.setFilter(filter);

// a filter specifically for the title property that contains query
Filter titleFilter = hstQuery.createFilter();
titleFilter.addContains("example:title", query);

// a filter for the entire content that contains query
Filter contentFilter = hstQuery.createFilter();
contentFilter.addContains(".", query);

// add titleFilter and contentFilter as OR-ed filters
// documents that have a hit in the title (and or content) will now score
// much higher than documents that only have a hit in the content
filter.addOrFilter(titleFilter).addOrFilter(contentFilter);

HstQueryResult result = hstQuery.execute();

Now, the result will have documents that have a hit in the title ranked higher than documents that only have a hit in the content.

Did you find this page helpful?
How could this documentation serve you better?
On this page
    Did you find this page helpful?
    How could this documentation serve you better?