HTTP API - Map Reduce Indexes

Indexes enable advanced fast and complex queries of JSON documents already in RavenDB. Indexes are expressed using LINQ and are composed of up to two parts, a map and a reduce function.

This section of the documentation focuses on map reduce indexes. Map reduce indexes allow for complex aggregation of data by first selecting records (map) and then applying the specified reduce function to these records to produce a smaller set of results.

For a more in-depth look at how map reduce works, read this post: Map / Reduce a Visual Explanation.

Creating a Map Reduce Index

Map reduce indexes are created in the same way as simple indexes, just with a reduce function specified as well:

curl -X PUT http://localhost:8080/indexes/byEmail -d "{ Map: 'from doc in docs where != null select new {, count = 1 };' ,
     Reduce: 'from result in results group result by into g select new { email = g.Key, count = g.Sum(x=>x.count)  } '}"

On a successful index create, RavenDB will respond with a HTTP 201 Created response code, and a JSON acknowledgment of the index just created:

HTTP/1.1 201 Created

Querying a Map Reduce Index

Querying a Map Reduce is done the same way as with a simple index. Assuming we've previously added a document such as:

curl http://localhost:8080/docs/ayende1 -X PUT "{ _id: 'ayende',email: '', projects: [ 'rhino mocks','nhibernate','rhino service bus','raven db' ] }"
curl http://localhost:8080/docs/ayende2 -X PUT "{ _id: 'ayende',email: '', projects: [ 'rhino mocks','nhibernate','rhino service bus','raven db' ] }"

Then a query of the "byEmail" map reduce index would look like:

curl -X GET http://localhost:8080/indexes/byEmail?

Paging Results & Deleting

Paging results and deleting a map reduce index are accomplished the same way as for a simple index.