Indexes: Boosting

A feature that RavenDB leverages from Lucene is called Boosting. This feature gives you the ability to manually tune the relevance level of matching documents when performing a query.

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 extension method from the Raven.Client.Documents.Linq.Indexing namespace.

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 : AbstractIndexCreationTask<Employee>
{
    public Employees_ByFirstAndLastName()
    {
        Map =
            employees =>
            from employee in employees
            select new
            {
                FirstName = employee.FirstName.Boost(10),
                LastName = employee.LastName
            };
    }
}
store
    .Maintenance
    .Send(new PutIndexesOperation(new IndexDefinition
    {
        Name = "Employees/ByFirstAndLastName",
        Maps =
        {
            @"from employee in docs.Employees
              select new
              {
                  FirstName = employee.FirstName.Boost(10),
                  LastName = employee.LastName
              }"
        }
    }));

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
IList<Employee> results = session
    .Query<Employee, Employees_ByFirstAndLastName>()
    .Where(x => x.FirstName == "Bob" || x.LastName == "Bob")
    .ToList();

Remarks

Information

Boosting is also available at the query level. You can read more about it here.