Indexes: Boosting

When querying with some filtering conditions,
a basic score is calculated for each document in the results by the underlying engine.

Providing a boost value to some fields allows you to prioritize the resulting documents.
The boost value is integrated with the basic score, making the document rank higher.
Automatic ordering of the results by the score is configurable.

From the index perspective we can associate to an index entry a boosting factor.
The higher value it has, the more relevant term will be. To do this, we must use the Boost method.

Let's jump straight into the example.
To perform the query that will return employees where either FirstName or LastName is equal to Bob,
and to promote employees (move them to the top of the results) where FirstName matches the phrase, we must first create an index with boosted entry.

public class Employees_ByFirstAndLastName extends AbstractIndexCreationTask {
    public Employees_ByFirstAndLastName() {
        map = "docs.Employees.Select(employee => new {" +
            "    FirstName = employee.FirstName.Boost(10)," +
            "    LastName = employee.LastName" +
            "})";
    }
}
IndexDefinition indexDefinition = new IndexDefinition();
indexDefinition.setName("Employees/ByFirstAndLastName");
indexDefinition.setMaps(Collections.singleton(
    "docs.Employees.Select(employee => new {" +
    "    FirstName = employee.FirstName.Boost(10)," +
    "    LastName = employee.LastName" +
    "})"));

store.maintenance().send(new PutIndexesOperation(indexDefinition));

The next step is to perform a query against that index:

// employees with 'firstName' equal to 'Bob'
// will be higher in results
// than the ones with 'lastName' match
List<Employee> results = session.query(Employee.class, Employees_ByFirstAndLastName.class)
    .whereEquals("FirstName", "Bob")
    .whereEquals("LastName", "Bob")
    .toList();

Remarks

Information

Boosting is also available at the query level.

Automatic score-based ordering

  • By default, whenever boosting is involved, either via a dynamic query or when querying an index that has a boosting factor in its definition, the results will be automatically ordered by the score.

  • This behavior can be modified using the OrderByScoreAutomaticallyWhenBoostingIsInvolved
    configuration key.