Relational searching sucks, don’t try to replicate it
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:
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.