Boost Document Scores for Hits in a Certain Property

A search query with a constraint to only show documents containing some text may look like this:

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

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. To boost the weight of a hit in a certain property (such as title) and get a better score for documents that match the query in their title, add an additional contains constraint for the title property and combine the two constraints using OR as follows:

// Build an HstQuery with OR-ed "contains" constraints for the title property
// and the entire document. Documents that have a hit in the title (and/or content) 
// will now score higher than documents that only have a hit in the content.
final HstQuery hstQuery = HstQueryBuilder.create(scope)
        .ofTypes(BaseDocument.class)
        .where(
                or(
                        constraint("myproject:title").contains(parsedQuery), 
                        constraint(".").contains(parsedQuery)
                )
        )
        .build();

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?