2014-11-02 67 views
5

如何从排序集中获取大多数加权元素,但排除在另一个集(或列表或散列)中找到的元素。如何获得排序集上的DIFF

>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 

我唯一的选择是从有序集合中逐个获取元素,并与“禁用”项目列表进行比较?由于服务器的交易太多,会不会很慢?

这里有什么方法?

回答

17

注:我假设你的意思sadd disabled two

正如你已经找到了,SDIFF不上的有序集合操作 - 这是因为定义排序组之间的差异是不平凡的。

你可以做的是首先要创建一个临时组与ZUNIONSTORE并设置相交的得分为0,然后做一个范围不包括0,例如:

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" 
+1

酷招,伊塔马尔! – djanowski 2016-03-10 19:58:21

+0

@djanowski谢谢:)希望再次见面#RedisConf 2016! – 2016-03-11 11:17:23