Reputation:
What is the widely used technique in indexing a JSON
or XML
string when storing it on a NoSQL database like Redis
?
I know database like MongoDB
provides this already, but I want to understand how to implement this when I will be using a Key-value store, like Redis
or Voldemort
.
Such that:
Upvotes: 1
Views: 538
Reputation: 73246
With a pure key/value store, you are supposed to maintain an additional set of keys to simulate a secondary index.
For instance you can store:
user:1 -> { id:1, firstname:Bilbo, lastname:Baggins, race:hobbit }
user:2 -> { id:2, firstname:Peregrin, lastname:Took, race:hobbit }
and then:
firstname:Bilbo - > [1]
lastname:Baggins -> [1]
firstname:Peregrin -> [2]
lastname:Took -> [2]
race:hobbit -> [1,2]
To find hobbit users, get the value of race:hobbit, and then for each returned id, get user:id.
Of course with a pure key/value store (memcached for instance), it is difficult to manage indexes with low cardinalities (ie. lots of entries for a given value).
With Redis, it is easier due to its support of set and hash datatypes. Also, intersection/union of redis sets is a convenient way to implement queries with logical AND/OR expressions.
See also the following questions:
how to have relations many to many in redis
Project Voldemort also supports list storage that can be used for a similar purpose (provided there are not too many entries per value).
Upvotes: 1