Over a million developers have joined DZone.
Platinum Partner

Awesome Indexing with RavenDB

· Database Zone

Speed up your development and test cycles with fully automated data delivery, view a 10 minute demo, brought to you in partnership with Delphix.

I am currently teaching a course in RavenDB, and as usual during a course, we keep doing a lot of work that pushes what we do with RavenDB. Usually because we try to come up with new scenarios on the fly and adapting to the questions from the students.

In this case, we were going over the map/reduce stack and we kept coming more and more complex example and how to handle them, and then we got to this scenario.

Given the following class structure:

   1: public class Animal
     public string Name { get; set; }
     public string Species { get; set; }
     public string Breed { get; set; }

Give me the count of all the species and all the breeds.  That is pretty easy to do, right?  In SQL, you would write it like this:

 SELECT Species, Breed, Count(*) FROM Animals
 GROUP BY Species, Breed

And that is nice, but it still means that you have to do some work on the client side to merge things up to get the final result, since we want something like this:

  • Dogs: 6
    • German Shepherd: 3
    • Labrador: 1
    • Mixed: 2
  • Cats: 3
    • Street: 2
    • Long Haired: 1

In RavenDB, we can express the whole thing in a simple succinct index:

 public class Animals_Stats : AbstractIndexCreationTask<Animal, Animals_Stats.ReduceResult>
     public class ReduceResult
         public string Species { get; set; }
         public int Count { get; set; }
         public BreedStats[] Breeds { get; set; }
         public class BreedStats
             public string Breed { get; set; }
             public int Count { get; set; }

     public Animals_Stats()
         Map = animals =>
                 from animal in animals
                 select new
                         Count = 1,
                         Breeds = new [] {new {animal.Breed, Count = 1}}
         Reduce = animals =>
                     from r in animals
                     group r by r.Species
                     into g
                     select new
                             Species = g.Key,
                             Count = g.Sum(x => x.Count),
                             Breeds = from breed in g.SelectMany(x => x.Breeds)
                                     group breed by breed.Breed
                                     into gb
                                     select new {Breed = gb.Key, Count = gb.Sum(x => x.Count)}

And the result of this beauty?


And that is quite pretty, even if I say so myself.

The Database Zone is brought to you in partnership with Delphix. Learn how you can easily virtualize and clone your database, read the white paper.


Published at DZone with permission of Ayende Rahien , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}