1

I'm trying to get the value of the best key in a sorted set. This is my query at the moment:

ZREVRANGEBYSCORE genre1 +inf -inf WITHSCORES LIMIT 0 1 

This is an example of an add in my set:

ZADD "genre1|genre2|genre3" 3.25153 "film"

I'd like to use the query in a way like this

ZREVRANGEBYSCORE *genre1* +inf -inf WITHSCORES LIMIT 0 1 

to match keys containing "...|genre1|..." and not only keys like "genre1". Any help will be appreciated

1 Answers1

0

This can be accomplished in two or three steps:

1) Use SCAN or KEYS to find the keys matching your pattern.

SCAN 0 MATCH "*genre1*"
1) "9"
2) 1) "genre1|genre2|genre3"
   2) "genre1|genre4"

2) For each key, use TYPE to test if it is a Sorted Set. This is only important if you may have other genre1 keys on the db

TYPE "genre1|genre4"
zset

3) Run your ZREVRANGEBYSCORE <key> +inf -inf WITHSCORES LIMIT 0 1 for each key.

See this answer on how you can SCAN for a given type. You can modify the Lua script to include the ZREVRANGEBYSCORE and get your results atomically on a single call.

Finally, consider reviewing if storing the genre combinations is optimal in your case. You may use a sorted set per genre, and then use ZUNIONSTORE or ZINTERSTORE to get scored combinations.

LeoMurillo
  • 6,048
  • 1
  • 19
  • 34