w3resource

Redis Sorted Sets: ZUNIONSTORE

Redis ZUNIONSTORE Command

Redis ZUNIONSTORE command calculates the union of a number of input keys  sorted sets given by the specified keys, and stores the result in a specified key.

The WEIGHTS option along with ZUNIONSTORE 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.

Here are some common uses and scenarios for the "ZUNIONSTORE" command:

  • Union of Sets: Combine multiple sorted sets into a single sorted set.
  • Aggregating Scores: Aggregate scores of members from different sorted sets using SUM, MIN, or MAX aggregation functions.
  • Creating Combined Leaderboards: Merge multiple leaderboards into a single, unified leaderboard.
  • Data Consolidation: Consolidate data from various sorted sets for unified analysis or reporting.

Syntax:

ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]

Available since

2.0.0.

Return Value

Integer reply, the number of elements in the resulting sorted set at the destination.

Return Value Type

Integer

Example: Redis ZUNIONSTORE: a new set of union

Redis zinterstore example with union of three sets
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"

Example: Redis ZUNIONSTORE: Using weights

Redis zinterstore example with union of three sets
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"

Example: Redis ZUNIONSTORE: Using aggregate

Redis zinterstore example with union of three sets
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"

Example: Redis ZUNIONSTORE: union of three sets

Redis zinterstore example with union of three sets
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"

Previous: ZSCORE
Next: ZSCAN



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/redis/redis-zunionstore-destination-numkeys-key.php