NoSQL Zone is brought to you in partnership with:

Ayende Rahien is working for Hibernating Rhinos LTD, a Israeli based company producing developer productivity tools for OLTP applications such as NHibernate Profiler (nhprof.com), Linq to SQL Profiler(l2sprof.com), Entity Framework Profiler (efprof.com) and more. Ayende is a DZone MVB and is not an employee of DZone and has posted 461 posts at DZone. You can read more from them at their website. View Full User Profile

Awesome Indexing with RavenDB

12.06.2012
| 2201 views |
  • submit to reddit

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
                     {
                         animal.Species,
                         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?

image

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




Published at DZone with permission of Ayende Rahien, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)