Redis zunionstore 命令用于求多个排序集合的并集,并将结果存储在指定key中。
weights 选项为乘法因子。在新的结果集中,每个元素的分数都乘以该因子为新的分数。如果未指定 weights ,则乘法因子默认为 1。
语法:
zunionstore destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
可用版本
>=2.0.0.
返回值
在目的地的结果排序集中的元素数。
返回值类型
整数
示例1:没有乘法因子,默认为1
127.0.0.1:6379> zadd srcset1 5 M 6 N 7 O
(integer) 3
127.0.0.1:6379> zadd srcset2 3 N 2 O 4 P
(integer) 3
127.0.0.1:6379> zunionstore desset 2 srcset1 srcset2
(integer) 4
127.0.0.1:6379> zrange desset 0 -1 WITHSCORES
1) "P"
2) "4"
3) "M"
4) "5"
5) "N"
6) "9"
7) "O"
8) "9"
示例2:有乘法因子,每个元素的score为该score乘于乘法因子
127.0.0.1:6379> zadd srcset1 5 M 6 N 7 O
(integer) 3
127.0.0.1:6379> zadd srcset2 3 N 2 O 4 P
(integer) 3
127.0.0.1:6379> zunionstore desset 2 srcset1 srcset2 WEIGHTS 2 3
(integer) 4
127.0.0.1:6379> zrange desset 0 -1 WITHSCORES
1) "M"
2) "10"
3) "P"
4) "12"
5) "O"
6) "20"
7) "N"
8) "21"
示例3:
127.0.0.1:6379> zadd srcset1 5 M 6 N 7 O
(integer) 3
127.0.0.1:6379> zadd srcset2 3 N 2 O 4 P
(integer) 3
127.0.0.1:6379> zunionstore desset 2 srcset1 srcset2 AGGREGATE MIN
(integer) 4
127.0.0.1:6379> zrange desset 0 -1 WITHSCORES
1) "O"
2) "2"
3) "N"
4) "3"
5) "P"
6) "4"
7) "M"
8) "5"
127.0.0.1:6379> zunionstore desset 2 srcset1 srcset2 AGGREGATE MAX
(integer) 4
127.0.0.1:6379> zrange desset 0 -1 WITHSCORES
1) "P"
2) "4"
3) "M"
4) "5"
5) "N"
6) "6"
7) "O"
8) "7"
示例4:
127.0.0.1:6379> zadd srcset1 5 M 6 N 7 O
(integer) 3
127.0.0.1:6379> zadd srcset2 3 N 2 O 4 P
(integer) 3
127.0.0.1:6379> zadd srcset3 1 O 2 P 3 Q
(integer) 3
127.0.0.1:6379> zunionstore desset 3 srcset1 srcset2 srcset3
(integer) 5
127.0.0.1:6379> zrange desset 0 -1 WITHSCORES
1) "Q"
2) "3"
3) "M"
4) "5"
5) "P"
6) "6"
7) "N"
8) "9"
9) "O"
10) "10"