Ayende @ Rahien

My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:


+972 52-548-6969

, @ Q c

Posts: 6,007 | Comments: 44,762

filter by tags archive

RavenDB – Defining indexes

time to read 3 min | 557 words

One of the more powerful features of RavenDB is the notion of indexes. Indexes allow you to query RavenDB efficiently and they stand at the core of RavenDB’s philosophy of “we shall never make you wait” and they are the express way to a lot of RavenDB’s capabilities (spatial searches and full text searches, to name just a few).

Unfortunately, indexes require that you pre-define them before you can use them. That led to a problem. A big one. It meant that in addition for simply deploying RavenDB, you had to manage additional assets, the index definitions. In essence, that is very similar to having to worry about the database schema.  That, in turn, made deploying that bit more complex, and that was unacceptable.

So we implemented the Code Only Indexes, which allow you to define your indexes as part of your project, and have them automatically created the first time the application starts.

Defining the indexes is pretty simple:

public class Movies_ByActor : AbstractIndexCreationTask<Movie>
    public Movies_ByActor()
        Map = movies => from movie in movies
                        select new {movie.Name};
        Index(x=>x.Name, FieldIndexing.Analyzed);

public class Users_CountByCountry : AbstractIndexCreationTask<User>
    public Users_CountByCountry()
        Map = users => from user in users
              select new {user.Country, Count = 1};
        Reduce= results => from result in results
                           group result by result.Country into g
                           select new { Country = g.Key, Count = g.Sum(x=>x.Count)}


But this is just the first step, the next is to tell RavenDB about them:

IndexCreation.CreateIndexes(typeof(Movies_ByActor).Assembly, store);

If you put this in your Global.asax, you would never have to think about indexing or deployment issues again.



[disclaimer]I've not looked at ravendb so maybe this is already available or not a good idea.[/disclaimer]

Something that springs to mind reading this is that as index definitions implement AbstractIndexCreationTask <t maybe there could be a method IndexCreation.AutomaticDiscovery()? This could then scan your assembly for any types implementing AbstractIndexCreationTask and include them?

Ayende Rahien


Which assemblies should it scan?


What about upgrade scenarios for this? If i alter an index or remove one?

Ayende Rahien


If you remove an index, you need to explicitly remove it.

If you alter it, it will update


This code doesnt compile. ;-)

Ayende Rahien


This is happening on the unstable fork.


What sort of syntax is this?

Ayende Rahien


You need to explain a bit more on that


I'm referring to this:

public Movies_ByActor()




-- What is that construct? It doesn't seem to be valid C# code. What is the type of 'Movies_ByActor'? It can't be a method because there's no return type; it can't be an anonymous object because it's not local to any methods; it can't be a property because it doesn't have getters or setters; it can't be a class or struct itself because... well, there's no class or struct keywords.

Basically the syntax is confusing me - it's also confusing Visual Studio. Perhaps I'm showing my ignorance. =P


Okay - I'm a little less confused now - I forgot about constructor syntax. Lol

But the second example - 'public Users()' - that still stands; was that a typo, and should have been 'public Users_CountByCountry' ?

Ayende Rahien


Yes, the second one was a typo, fixed


Okay, cool, thanks - I'm not going insane. =)


How about IndexCreation.AutomaticDiscovery(Assembly ass); ?

Ayende Rahien


That is what this does, doesn't it?

IndexCreation.CreateIndexes(typeof(Movies_ByActor).Assembly, store);


I missed the .Assembly there! I thought it was just for that specific type...


Ayende i m confident u have the ressources to bring this from an experiment to a major product.

Any chance RavenDB will get more coverage and grow as first class storage ?


cool, you found why it didnt compile.

btw. love your work on these projects man.

I've been looking for this...

Thanks for doing this, I've been rolling my own index creator. I can't seem to find it in the assembly though. I'm using Raven.Client-3.5. Is this only available on the lightweight assembly?

Ayende Rahien


Yes, it is available in 4.0 only (relies on the MEF stuff)

Comment preview

Comments have been closed on this topic.


No future posts left, oh my!


  1. Speaking (3):
    23 Sep 2015 - Build Stuff 2015 (Lithuania & Ukraine), Nov 18 - 24
  2. Production postmortem (11):
    22 Sep 2015 - The case of the Unicode Poo
  3. Technical observations from my wife (2):
    15 Sep 2015 - Disk speeds
  4. Find the bug (5):
    11 Sep 2015 - The concurrent memory buster
  5. Buffer allocation strategies (3):
    09 Sep 2015 - Bad usage patterns
View all series



Main feed Feed Stats
Comments feed   Comments Feed Stats