Ayende @ Rahien

Hi!
My name is Ayende Rahien
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:

ayende@ayende.com

+972 52-548-6969

, @ Q c

Posts: 5,949 | Comments: 44,547

filter by tags archive

RavenDBIndex 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.

More posts in "RavenDB" series:

  1. (22 May 2015) Adding a new shard to an existing cluster, splitting the shard
  2. (21 May 2015) Adding a new shard to an existing cluster, the easy way
  3. (18 May 2015) Enabling shards for existing database

Comments

Lam Chan

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

Lam Chan

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

Jonty

How is this better than order 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

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

dotnetchris

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

Karep

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

njy
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

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

Njy, I don't understand the question

njy
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

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
njy

Ok, thanks

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.

Comment preview

Comments have been closed on this topic.

FUTURE POSTS

No future posts left, oh my!

RECENT SERIES

  1. The RavenDB Comic Strip (3):
    28 May 2015 - Part III – High availability & sleeping soundly
  2. Special Offer (2):
    27 May 2015 - 29% discount for all our products
  3. RavenDB Sharding (3):
    22 May 2015 - Adding a new shard to an existing cluster, splitting the shard
  4. Challenge (45):
    28 Apr 2015 - What is the meaning of this change?
  5. Interview question (2):
    30 Mar 2015 - fix the index
View all series

RECENT COMMENTS

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats