Ayende @ Rahien

Refunds available at head office

RavenDB Index Management

When I wrote RavenDB, I started from the server, and built the client last. That had some interesting affects on RavenDB, for example, you can see detailed docs about the HTTP API, because that is what I had when I wrote most of the docs.

In the context of indexes, that meant that I thought a lot more about defining and working with indexes from the WebUI perspective, rather than the client perspective. Now that Raven have users that actually put it through its paces, I found that most people want to be able to define their indexes completely in code, and want to be able to re-create those indexes from code.

And that calls for a integral solution from Raven for this issue. Here is how you do this.

  • You define your index creation as a class, such as this one:
    public class Movies_ByActor : AbstractIndexCreationTask
    {
        public override IndexDefinition CreateIndexDefinition()
        {
            return new IndexDefinition<Movie>
            {
                Map = movies => from movie in movies
                                select new {movie.Name}
            }
            .ToIndexDefinition(DocumentStore.Conventions);
        }
    }
  • Somewhere in your startup routine, you include the following line of code:
    IndexCreation.CreateIndexes(typeof(Movies_ByActor).Assembly, store);

And that is it, Raven will scan the provided assembly (you can also provide a MEF catalog, for more complex scenarios) and create all those indexes for you, skipping the creation if the new index definition matches the index definition in the database.

This also provide a small bit of convention, as you can see, the class name is Movies_ByActor, but the index name will be Movies/ByActor. You can override that by overriding the IndexName property

Comments

Szymon Kulec
07/26/2010 06:55 PM by
Szymon Kulec

Nice. I do like this scanning type of configuration!

josh
07/26/2010 08:06 PM by
josh

YAY! this will help with a demo I'm working on which shows mongodb, ravendb, and sql side by side (by side).

Comments have been closed on this topic.