您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
Redis Zunionstore 命令的权重与聚合
发布时间:2021-11-08 23:46:58编辑:雪饮阅读()
使用 WEIGHTS 选项时,可以为各个有序集合输入指定一个乘法系数(Multiplication factor )。
这意味着在将每个有序集合输入中的每个元素的分值传递给聚合函数(Aggregation function)之前,会将该分值乘以对应的系数。当未给定 WEIGHTS 选项时,乘法系数默认为 1。
Using the WEIGHTS option, it is possible to specify a multiplication factor for each input sorted set. This means that the score of every element in every input sorted set is multiplied by this factor before being passed to the aggregation function.
When WEIGHTS is not given, the multiplication factors default to 1.
实例:
默认权重求并集:
127.0.0.1:6379> zadd key1 1 val1 2 val2 3 val3
(integer) 3
127.0.0.1:6379> zrange key1 0 -1 withscores
1) "val1"
2) "1"
3) "val2"
4) "2"
5) "val3"
6) "3"
127.0.0.1:6379> zunionstore key2 1 key1
(integer) 3
127.0.0.1:6379> zrange key2 0 -1 withscores
1) "val1"
2) "1"
3) "val2"
4) "2"
5) "val3"
6) "3"
指定权重求并集:
127.0.0.1:6379> zunionstore key2 1 key1 weights 2
(integer) 3
127.0.0.1:6379> zrange key2 0 -1 withscores
1) "val1"
2) "2"
3) "val2"
4) "4"
5) "val3"
6) "6"
指定权重可以同时指定多个权重,与参与并集的key一一对应:
127.0.0.1:6379> zunionstore key3 2 key1 key2 weights 2 4
(integer) 3
127.0.0.1:6379> zrange key3 0 -1 withscores
1) "val1"
2) "10"
3) "val2"
4) "20"
5) "val3"
6) "30"
使用 AGGREGATE 选项时,可以指定并集运算结果的聚合方式。该选项默认值为 SUM,即将输入中所有存在该元素的集合中对应的分值全部加一起。
当选项被设置为 MIN 或 MAX 任意值时,结果集合将保存输入中所有存在该元素的集合中对应的分值的最小或最大值。
With the AGGREGATE option, it is possible to specify how the results of the union are aggregated.
This option defaults to SUM, where the score of an element is summed across the inputs where it exists.
When this option is set to either MIN or MAX, the resulting set will contain the minimum or maximum score of an element across the inputs where it exists.
实例:
127.0.0.1:6379> zrange key1 0 -1 withscores
1) "val1"
2) "1"
3) "val2"
4) "2"
5) "val3"
6) "3"
127.0.0.1:6379> zrange key2 0 -1 withscores
1) "val1"
2) "1"
3) "val2"
4) "2"
5) "val3"
6) "3"
127.0.0.1:6379> zrange key3 0 -1 withscores
1) "val1"
2) "10"
3) "val2"
4) "20"
5) "val3"
6) "30"
127.0.0.1:6379> zunionstore key4 1 key1 weights 1 aggregate min
(integer) 3
127.0.0.1:6379> zrange key4 0 -1 withscores
1) "val1"
2) "1"
3) "val2"
4) "2"
5) "val3"
6) "3"
127.0.0.1:6379> zunionstore key4 2 key1 key3 weights 1 1 aggregate min
(integer) 3
127.0.0.1:6379> zrange key4 0 -1 withscores
1) "val1"
2) "1"
3) "val2"
4) "2"
5) "val3"
6) "3"
127.0.0.1:6379> zunionstore key4 2 key1 key3 weights 1 1 aggregate max
(integer) 3
127.0.0.1:6379> zrange key4 0 -1 withscores
1) "val1"
2) "10"
3) "val2"
4) "20"
5) "val3"
6) "30"
聚合的min和max同样支持权重操作
实例:
127.0.0.1:6379> zunionstore key4 2 key1 key3 weights 2 1 aggregate min
(integer) 3
127.0.0.1:6379> zrange key4 0 -1 withscores
1) "val1"
2) "2"
3) "val2"
4) "4"
5) "val3"
6) "6"
127.0.0.1:6379> zunionstore key4 2 key1 key3 weights 1 4 aggregate max
(integer) 3
127.0.0.1:6379> zrange key4 0 -1 withscores
1) "val1"
2) "40"
3) "val2"
4) "80"
5) "val3"
6) "120"
如果目标键 destination 已经存在,将会被覆盖。
If destination already exists, it is overwritten.
关于这个,上面的实例已经证实了。
关键字词:Redis,Zunionstore
上一篇:Redis Pubsub 命令
下一篇:Redis 发布订阅