Redis排序集联合收割机

Redis排序集联合收割机,redis,Redis,Redis太棒了 如果我有两个排序集,比如“set1=key1:100,key2:200,key3:300” 还有一个类似“set2=key1:1,key2:2,key3:3” 那么有可能得到这样的结果吗?set3=key1:100\u 1,key2:200\u 2,key3:300\u 3 我不想添加相同键的值,也不想获取相同键的最小值/最大值。 是否有可能将这些值组合在一起? 我想为同一个键同时获取这两个值。排序集中的分数必须是数值,因此不可能在交集/并集运算中返回分数的简单串联 但是,如果

Redis太棒了

如果我有两个排序集,比如“set1=key1:100,key2:200,key3:300”

还有一个类似“set2=key1:1,key2:2,key3:3”

那么有可能得到这样的结果吗?set3=key1:100\u 1,key2:200\u 2,key3:300\u 3

我不想添加相同键的值,也不想获取相同键的最小值/最大值。 是否有可能将这些值组合在一起?
我想为同一个键同时获取这两个值。

排序集中的分数必须是数值,因此不可能在交集/并集运算中返回分数的简单串联

但是,如果可以将每个集合的分数值绑定到给定的范围,则可以使用求和运算将两个值编码为一个值,并使用简单的算术运算

例如:

# For s1, we assume the score is between 0 and 999
> zadd s1 100 key1 200 key2 300 key3
(integer) 3
# For s2, we assume the score is between 0 and 99
> zadd s2 1 key1 2 key2 3 key3
(integer) 3

# Get the intersection, dividing the s2 score by 100
> zinterstore s3 2 s1 s2 WEIGHTS 1 0.01
(integer) 3

# Get the result with scores
> zrange s3 0 -1 withscores
1) "key1"
2) "100.01000000000001"
3) "key2"
4) "200.02000000000001"
5) "key3"
6) "300.02999999999997"
在结果分数中,整数部分包含来自s1的分数,小数部分包含来自s2的分数

请注意分数是一个双浮点数。它有一些后果:

  • 如果只处理整数(即0.029999…实际上是0.03),则可能必须正确地对其进行四舍五入

  • 将两个数值编码为一表示精度除以2(仅16位到8位)。它可能适合也可能不适合你的需要


排序集中的分数必须是一个数值,因此不可能在交集/并集运算中返回分数的简单串联

但是,如果可以将每个集合的分数值绑定到给定的范围,则可以使用求和运算将两个值编码为一个值,并使用简单的算术运算

例如:

# For s1, we assume the score is between 0 and 999
> zadd s1 100 key1 200 key2 300 key3
(integer) 3
# For s2, we assume the score is between 0 and 99
> zadd s2 1 key1 2 key2 3 key3
(integer) 3

# Get the intersection, dividing the s2 score by 100
> zinterstore s3 2 s1 s2 WEIGHTS 1 0.01
(integer) 3

# Get the result with scores
> zrange s3 0 -1 withscores
1) "key1"
2) "100.01000000000001"
3) "key2"
4) "200.02000000000001"
5) "key3"
6) "300.02999999999997"
在结果分数中,整数部分包含来自s1的分数,小数部分包含来自s2的分数

请注意分数是一个双浮点数。它有一些后果:

  • 如果只处理整数(即0.029999…实际上是0.03),则可能必须正确地对其进行四舍五入

  • 将两个数值编码为一表示精度除以2(仅16位到8位)。它可能适合也可能不适合你的需要