Guess What? Relational Searching Sucks
Join the DZone community and get the full member experience.
Join For Freethis 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.
Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments