Warm tip: This article is reproduced from serverfault.com, please click

Is there a keyword in redis that acts similarly to the WHERE clause in SQL?

发布于 2020-11-29 21:42:09

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).

Questioner
Highboi
Viewed
0
Maruthi Adithya 2020-11-30 12:04:31

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 ZSCOREcommand to get the current score and update it to whatever you want using the ZADD command.

When you are querying, use the ZRANGEBYSCORE command to get the entire list and then choose the entry based on your need.