ZUNION
ZUNION numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM | MIN | MAX] [WITHSCORES]
- Group
- Sorted set
- Complexity
- O(N)+O(M*log(M)) where N is the total size of the input sets and M is the size of the result
- Arity
- -3 (at least 3 words, counting the command name)
- Flags
- read-only
- HotKV Cloud
- All plans
- On-Premise
- no license needed
ZUNION returns every member that appears in at least one of the given sorted
sets, ordered by its combined score. A member’s combined score is built from
its score in each input that contains it, by default by adding them up.
Inputs can also be plain sets; their members count as having score 1.
Missing keys count as empty. To store the result instead, use
ZUNIONSTORE.
Options
Section titled “Options”| Option | Effect |
|---|---|
numkeys |
How many keys follow. Must be at least 1. |
WEIGHTS weight ... |
One multiplier per input key, applied to that input’s scores before they are combined. Default 1 each. |
AGGREGATE SUM |
Add up the (weighted) scores. The default. |
AGGREGATE MIN |
Keep the lowest (weighted) score. |
AGGREGATE MAX |
Keep the highest (weighted) score. |
WITHSCORES |
Return each member with its combined score. Must be the last argument. |
Return value
Section titled “Return value”| Case | RESP2 | RESP3 |
|---|---|---|
| Success | Array of members | Same |
With WITHSCORES |
Flat array: member, score, … (scores as bulk strings) | Array of [member, score] pairs, the score a double |
Errors:
ERR value is not an integer or out of rangeifnumkeysis not an integer, andERR at least 1 input key is needed for 'zunion' commandif it is below 1.ERR syntax errorif fewer keys follow thannumkeyssays,WEIGHTShas too few values, theAGGREGATEvalue is unknown, or anything else follows.ERR weight value is not a floatfor a weight that is not a number.WRONGTYPEif a key holds something other than a sorted set or a set. Key types are checked before the options.
Examples
Section titled “Examples”127.0.0.1:6379> ZADD q1 10 "alice" 5 "bob"(integer) 2127.0.0.1:6379> ZADD q2 3 "bob" 7 "carol"(integer) 2127.0.0.1:6379> ZUNION 2 q1 q2 WITHSCORES1) "carol"2) "7"3) "bob"4) "8"5) "alice"6) "10"127.0.0.1:6379> ZUNION 2 q1 q2 WEIGHTS 1 2 AGGREGATE MAX1) "bob"2) "alice"3) "carol"Compatibility
Section titled “Compatibility”Same replies as Redis 7.4 and Valkey 8. HotKV requires WITHSCORES to be the
last argument; Redis and Valkey also accept it before WEIGHTS or
AGGREGATE.
See also
Section titled “See also”Was this page helpful?
Thanks for the feedback.