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,124 | Comments: 45,474

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.


  1. RavenDB 3.5 whirl wind tour: You want all the data, you can’t handle all the data - about one day from now
  2. The design of RavenDB 4.0: Making Lucene reliable - 2 days from now
  3. RavenDB 3.5 whirl wind tour: I’ll find who is taking my I/O bandwidth and they SHALL pay - 3 days from now
  4. The design of RavenDB 4.0: Physically segregating collections - 4 days from now
  5. RavenDB 3.5 Whirlwind tour: I need to be free to explore my data - 5 days from now

And 14 more posts are pending...

There are posts all the way to May 30, 2016


  1. RavenDB 3.5 whirl wind tour (14):
    29 Apr 2016 - A large cluster goes into a bar and order N^2 drinks
  2. The design of RavenDB 4.0 (13):
    28 Apr 2016 - The implications of the blittable format
  3. Tasks for the new comer (2):
    15 Apr 2016 - Quartz.NET with RavenDB
  4. Code through the looking glass (5):
    18 Mar 2016 - And a linear search to rule them
  5. Find the bug (8):
    29 Feb 2016 - When you can't rely on your own identity
View all series



Main feed Feed Stats
Comments feed   Comments Feed Stats