This question on Stack Overflow is a fairly common one. Here is the data:

And the question was about how to get RavenDB to create an index that would have the following results:

{ CarId: "cars/1", PersonId: "people/1235", UnitId: "units/4321", Make: "Toyota", Model: "Prius" FirstName: "Ayende", LastName: "Rahien" Address: "Komba 10, Hadera" } { CarId: "cars/2", PersonId: "people/1236", UnitId: "units/4321", Make: "Toyota", Model: "4runner" FirstName: "test", LastName: "test" Address: "blah blah" } same unit different person owns a different car

Now, if you try really hard, you can probably try to get something like that, but that is the wrong way to go about this in RavenDB.

Instead, we can write the following index:

Note that this index is a simple multi map index, it isn’t a multi map/reduce index. There is no need.

This index can return one of three types.

Car – just show the car to the user



Person – now that we have a person, we have the id, and we can query for that:



Unit – now that we have a unit, we have the id, and we can query for that:



This method means that we have to generate an additional query for some cases, but it has a lot of advantages. It is simple. It requires very little work from both client and server and it doesn’t suffer from the usual issues that you run into when you attempt to query over multiple disjointed data sets.

Now, the bad thing about this is that this won’t allow me to query for cross entity values, so it would be hard for me to query for the cars in Hadera owned by Ayende. But in most cases, that isn’t really a requirement. We just want to be able to search by either one of those, not all of them.