DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Databases
  4. Awesome Indexing with RavenDB

Awesome Indexing with RavenDB

Oren Eini user avatar by
Oren Eini
·
Dec. 06, 12 · Interview
Like (0)
Save
Tweet
Share
3.14K Views

Join the DZone community and get the full member experience.

Join For Free

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.




Awesome (window manager) Express Merge (version control) push sql

Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • ChatGPT: The Unexpected API Test Automation Help
  • Express Hibernate Queries as Type-Safe Java Streams
  • How to Secure Your CI/CD Pipeline
  • How To Check Docker Images for Vulnerabilities

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: