I am trying to see how I can select keys in redis based off of the value of these keys. I am using redis in order to store a "word score" for each word in the english language to give search recommendations on my site. The larger the word score, the more common the word is on the site and therefore the more it will be recommended as the user types something into the search bar. I cannot implement this feature without having something similar to the WHERE clause in SQL, so is there anyway to have this kind of behavior in redis? Should I implement a different kind of database for this problem? (I chose redis because it is fast).
2
Answers
Redis doesn’t have a built in solution for secondary index but you can build your own searchable index using the built in data structures see: Secondary indexing with Redis.
The other option is to add the RediSearch module which extends Redis to provide a simple indexing mechanism and extended query language.
Redis is primarily an in-memory data store. This means that at one point in time, certain keys may get evicted. The design decision is simple – if you want all your data to be persisted forever, Redis would definitely be a wrong choice here. However, if this is temporary and you can handle a few faults, then you can go with Redis.
Even though it is not possible to directly simulate the
WHERE
clause behaviour, it is possible to do it.For this, you can use the
ZRANGEBYSCORE
command in Redis.So, what you can do is as and when the words come, use the
ZSCORE
command to get the current score and update it to whatever you want using theZADD
command.When you are querying, use the
ZRANGEBYSCORE
command to get the entire list and then choose the entry based on your need.