本文介绍了如何在排序集上获得 DIFF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从已排序的集合中获取加权最多的元素,但不包括在另一个集合(或列表或散列)中找到的元素.

How do I get most weighted elements from a sorted set, but excluding those found 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

我唯一的选择是从排序集中获取元素并与禁用"项目列表进行比较吗?由于到服务器的事务太多,这会不会很慢?

Is my only option is to get elements from the sorted set one-by-one and compare to the list of "disabled" items? Wouldn't that be very slow because of so many transactions to a server?

这里的方法是什么?

推荐答案

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

Note: I assume you've meant sadd disabled two

正如您所发现的,SDIFF 不会对排序集进行操作 - 那是因为定义排序集之间的差异并非易事.

As you've found out, SDIFF does not operate on sorted sets - that is because defining the difference between sorted sets isn't trivial.

你可以做的是先用 ZUNIONSTORE 创建一个临时集合,并将相交的分数设置为 0.然后做一个不包括 0 的范围,例如:

What you could do is first create a temporary set with ZUNIONSTORE and set the intersect's scores to 0. Then do a range excluding the 0, e.g.:

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"

这篇关于如何在排序集上获得 DIFF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 03:31