Awesome indexing with RavenDB

time to read 18 min | 3561 words

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
   2: {
   3:     public string Name { get; set; }
   4:     public string Species { get; set; }
   5:     public string Breed { get; set; }
   6: }

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:

   1: SELECT Species, Breed, Count(*) FROM Animals
   2: 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:

   1: public class Animals_Stats : AbstractIndexCreationTask<Animal, Animals_Stats.ReduceResult>
   2: {
   3:     public class ReduceResult
   4:     {
   5:         public string Species { get; set; }
   6:         public int Count { get; set; }
   7:         public BreedStats[] Breeds { get; set; }
   8:  
   9:         public class BreedStats
  10:         {
  11:             public string Breed { get; set; }
  12:             public int Count { get; set; }
  13:         }
  14:     }
  15:  
  16:     public Animals_Stats()
  17:     {
  18:         Map = animals =>
  19:                 from animal in animals
  20:                 select new
  21:                     {
  22:                         animal.Species,
  23:                         Count = 1,
  24:                         Breeds = new [] {new {animal.Breed, Count = 1}}
  25:                     };
  26:         Reduce = animals =>
  27:                     from r in animals
  28:                     group r by r.Species
  29:                     into g
  30:                     select new
  31:                         {
  32:                             Species = g.Key,
  33:                             Count = g.Sum(x => x.Count),
  34:                             Breeds = from breed in g.SelectMany(x => x.Breeds)
  35:                                     group breed by breed.Breed
  36:                                     into gb
  37:                                     select new {Breed = gb.Key, Count = gb.Sum(x => x.Count)}
  38:                         };
  39:  
  40:     }
  41: }

And the result of this beauty?

image

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