RavenDBIndex Boosting

time to read 6 min | 1084 words

Recently we added a really nice feature, boosting the results while indexing.

Boosting is a way to give documents or attributes in a document weights. Attribute level boosting is a way to tell RavenDB that a certain  attribute in a document is more important than the others, so it will show up higher in queries when other properties are involved in a query. A document level boosting means that a certain document is more important than another (when using multi maps).

Let us see a few examples where this is happening. The simplest scenario is when we have a multi field search, and we want one of the fields to be the more important one. For example, we decided that when you make a search for first name and last name, a match on the first name has higher relevance than a match on the last name. We can define this requirement with the following index:

public class Users_ByName : AbstractIndexCreationTask<User>
    public Users_ByName()
        Map = users => from user in users
                       select new
                           FirstName = user.FirstName.Boost(3),

And we can query the index using:

var matches = session.Query<User,UsersByName>()
      .Where(x=>x.FirstName == "Ayende" || x.LastName == "Eini")

Assuming that we have a user with the first name “Ayende” and another user with the last name “Eini”, this will find both of them, but will rank the user with the name “Ayende” first.

Let us see another variant, we have a multi map index for users and accounts, both are searchable by name, but we want to ensure that accounts are more important than users. We can do that using the following index:

public class UsersAndAccounts : AbstractMultiMapIndexCreationTask
    public UsersAndAccounts()
        AddMap<User>(users =>
                     from user in users
                     select new {Name = user.FirstName}
        AddMap<Account>(accounts =>
                        from account in accounts
                        select new {account.Name}.Boost(3)

If we have query that has matches for users and accounts, this will make sure that the account comes first.

And finally, a really interesting use case is that based on the entity itself, you decide to rank it higher. For example, we want to rank customers that ordered a lot from us higher than other customers. We can do that using the following index:

public class Accounts_Search : AbstractIndexCreationTask<Account>
    public Accounts_Search()
        Map = accounts =>
              from account in accounts
              select new
              }.Boost(account.TotalIncome > 10000 ? 3 : 1);

This way, we get the more important customers first. And this is really one of those things that brings up the polish in the system, the things that makes the users sit up and take notice.

More posts in "RavenDB" series:

  1. (13 Oct 2017) Interlocked distributed operations
  2. (12 Oct 2017) Node.JS client is now in beta
  3. (11 Sep 2017) Support options
  4. (14 Aug 2017) Maintaining transaction boundary integrity in a distributed cluster
  5. (03 Aug 2017) Raven Query Language
  6. (13 Jul 2017) The admin’s backdoor is piping hot
  7. (10 Jul 2017) Securing the keys to the kingdom
  8. (04 Jul 2017) Unbounded results sets
  9. (13 Jun 2017) The etag simplification
  10. (12 Jun 2017) Data subscriptions, Part II
  11. (09 Jun 2017) Data subscriptions, Part I
  12. (19 May 2017) Managing encrypted databases
  13. (11 May 2017) Working with attachments
  14. (10 May 2017) Attachments
  15. (08 May 2017) Full database encryption