ZUNION

Introduction and Use Case(s)

The ZUNION command is used to perform the union of multiple sorted sets in Redis. It aggregates the scores of elements from the provided sets and adds them into a new sorted set. This command is typically used when you need to combine sorted sets for leaderboard applications or to aggregate data from different sources.

Syntax

  1. ZUNION numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]

Parameter Explanations

  • numkeys: The number of keys to be combined.
  • key [key …]: The list of keys representing the sorted sets to be united.
  • WEIGHTS weight [weight …]: Optional. Modifies the score of each element in the sorted sets by multiplying it with this factor.
  • AGGREGATE SUM|MIN|MAX: Optional. Specifies how the results should be aggregated. By default, it’s SUM.
    • SUM: Adds up all the scores (default).
    • MIN: Takes the minimum score.
    • MAX: Takes the maximum score.

Return Values

The ZUNION command returns an array of elements with their scores from the resulting union of the input sorted sets.

Example outputs:

  1. 1) "member1"
  2. 2) "2.0"
  3. 3) "member2"
  4. 4) "3.5"

Code Examples

  1. dragonfly> ZADD zset1 1 "one"
  2. (integer) 1
  3. dragonfly> ZADD zset1 2 "two"
  4. (integer) 1
  5. dragonfly> ZADD zset2 1 "one"
  6. (integer) 1
  7. dragonfly> ZADD zset2 3 "three"
  8. (integer) 1
  9. dragonfly> ZUNION 2 zset1 zset2
  10. 1) "one"
  11. 2) "2.0"
  12. 3) "two"
  13. 4) "2.0"
  14. 5) "three"
  15. 6) "3.0"
  16. dragonfly> ZUNION 2 zset1 zset2 WEIGHTS 2 3
  17. 1) "one"
  18. 2) "5.0"
  19. 3) "two"
  20. 4) "4.0"
  21. 5) "three"
  22. 6) "9.0"
  23. dragonfly> ZUNION 2 zset1 zset2 AGGREGATE MAX
  24. 1) "one"
  25. 2) "1.0"
  26. 3) "two"
  27. 4) "2.0"
  28. 5) "three"
  29. 6) "3.0"

Best Practices

  • Ensure the numkeys parameter accurately matches the number of keys provided.
  • Use the WEIGHTS option to adjust the significance of each sorted set based on your application requirements.
  • Choose the appropriate AGGREGATE option (SUM, MIN, MAX) to get the desired result based on how you want to combine the scores.

Common Mistakes

  • Miscounting the numkeys parameter, leading to incorrect command execution.
  • Forgetting that WEIGHTS and AGGREGATE options are optional but can significantly change the output if not properly understood.

FAQs

What happens if the same member exists in multiple sets?

If the same member exists in multiple sets, its score will be aggregated based on the specified AGGREGATE option. By default, scores are summed.

Can I use ZUNION with only one sorted set?

Yes, but it would be equivalent to just copying the sorted set since there’s no other set to union with.

How does WEIGHTS affect the result?

The WEIGHTS option multiplies the score of each element in the sorted sets by the provided weight, allowing for weighted unions.