Ayende @ Rahien

Hi!
My name is Oren Eini
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: 18 | Comments: 65

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.


Comments

GeeBee

[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

GeeBee.

Which assemblies should it scan?

Dennis

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

Ayende Rahien

Dennis,

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

If you alter it, it will update

wouzer

This code doesnt compile. ;-)

Ayende Rahien

Wouzer,

This is happening on the unstable fork.

Erik

What sort of syntax is this?

Ayende Rahien

Erik,

You need to explain a bit more on that

Erik

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

Erik

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

Erik,

Yes, the second one was a typo, fixed

Erik

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

configurator

How about IndexCreation.AutomaticDiscovery(Assembly ass); ?

Ayende Rahien

Configurator,

That is what this does, doesn't it?

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

configurator

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

JimY

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 ?

wouzer

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

Miguel,

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

Comment preview

Comments have been closed on this topic.

FUTURE POSTS

  1. RavenDB 3.0 New Stable Release - 9 hours from now
  2. Production postmortem: The case of the lying configuration file - about one day from now
  3. Production postmortem: The industry at large - 2 days from now
  4. The insidious cost of allocations - 3 days from now
  5. Buffer allocation strategies: A possible solution - 6 days from now

And 4 more posts are pending...

There are posts all the way to Sep 11, 2015

RECENT SERIES

  1. Find the bug (5):
    20 Apr 2011 - Why do I get a Null Reference Exception?
  2. Production postmortem (10):
    31 Aug 2015 - The case of the memory eater and high load
  3. What is new in RavenDB 3.5 (7):
    12 Aug 2015 - Monitoring support
  4. Career planning (6):
    24 Jul 2015 - The immortal choices aren't
View all series

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats