How to get DIFF on a sorted set

How to get the most weighted items from a sorted set, but excluding those that are in another set (or list or hash).

>zadd all 1 one >zadd all 2 two >zadd all 3 three >sadd disabled 2 >sdiff all disabled (error) WRONGTYPE Operation against a key holding the wrong kind of value 

Am I the only option - to get items from a sorted set one by one and compare with a list of "disabled" items? Won't it be very slow due to the large number of transactions on the server?

What is the approach here?

+5
source share
1 answer

Note. I assume you meant sadd disabled two

As you found out, SDIFF does not work on sorted sets, because determining the difference between sorted sets is not trivial.

What you can do is first create a temporary set with ZUNIONSTORE and set the intersection values ​​to 0. Then do a range excluding 0, for example:

 127.0.0.1:6379> ZADD all 1 one 2 two 3 three (integer) 3 127.0.0.1:6379> SADD disabled two (integer) 1 127.0.0.1:6379> ZUNIONSTORE tmp 2 all disabled WEIGHTS 1 0 AGGREGATE MIN (integer) 3 127.0.0.1:6379> ZREVRANGEBYSCORE tmp +inf 1 WITHSCORES 1) "three" 2) "3" 3) "one" 4) "1" 
+17
source

Source: https://habr.com/ru/post/1206045/


All Articles