Ayende @ Rahien

It's a girl

RavenDB: Index Boosting

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),
                           user.LastName
                       };
    }
}

And we can query the index using:

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

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
              {
                  account.Name
              }.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.

Tags:

Posted By: Ayende Rahien

Published at

Originally posted at

Comments

Lam Chan
01/06/2012 04:11 PM by
Lam Chan

That is very awesome. I'm looking forward to your feature set expansions!

Lam Chan
01/06/2012 04:12 PM by
Lam Chan

That is very awesome. I'm looking forward to your feature set expansions!

Jonty
02/17/2012 12:48 PM by
Jonty

How is this better than order by?

smartcaveman
02/17/2012 01:24 PM by
smartcaveman

Where is the source for the AbstractIndexCreationTask and AbstractMultiMapIndexCreationTask?

From this angle, your method for isolating query logic looks similar to the specification pattern implementation that you were less than enthusiastic about (see here: http://ayende.com/blog/4784/architecting-in-the-pit-of-doom-the-evils-of-the-repository-abstraction-layer). But, I'm guessing it just seems that way because I don't know what the base classes are actually doing.

smartcaveman
02/17/2012 01:27 PM by
smartcaveman

Nevermind (https://github.com/ravendb/ravendb/blob/master/Raven.Client.Lightweight/Indexes/AbstractIndexCreationTask.cs)

dotnetchris
02/17/2012 04:04 PM by
dotnetchris

How do you handle ordering when using boosting? Never use ordering? Will Skip/Take blow up for not being ordered?

Karep
02/17/2012 07:23 PM by
Karep

Same question as Jonty. Also what if You decide that Firstname is more important then AccountName?

njy
02/17/2012 07:42 PM by
njy

@Oren: is the value of the Boost available as a field in the result, or at least usable to specify a different ordering? Something like "order by boost desc" or something like that.

Ayende Rahien
02/17/2012 08:22 PM by
Ayende Rahien

Jonty, It is important because it makes the values not dependent on strict ordering, but their basic importance as it relates to your actual query.

Ayende Rahien
02/17/2012 08:23 PM by
Ayende Rahien

Njy, I don't understand the question

njy
02/17/2012 08:44 PM by
njy

Using Boost gives more "importance" to some results, which actually means doing a (logical) additional "order by", is that correct?

If it is like so, i was wondering if it could be possible - for example - to search news articles by a search term and get the results ordered by the date (to get the most recent firsts) and then, if the date is the same, by "boost value". It's just an example, i hope to have clarified the hypothetical scenario.

Ayende Rahien
02/17/2012 09:58 PM by
Ayende Rahien

Njy, I actually think that this would work, yes. Itamar can probably answer this better, but I am pretty sure that the boost value would only be a factor inside the same orderby value.

njy
02/17/2012 11:50 PM by
njy

Ok, thanks

Itamar Syn-Hershko
02/18/2012 05:25 PM by
Itamar Syn-Hershko

njy, the "score" will be computed using the sort by and the boost will be factored in. So yes - this is essentially what will happen.

Comments have been closed on this topic.