ZUNION

Apply for Redis Developer Course Redis Regular Inspection / Technical Support Redis Enterprise Server

Compute the union

Usage: zunion 2 src_key1 src_key2.
Each member score is added by default if not specified.

src_key1 ={ (1,A),(2,B),(3,C)}
src_key2 = {(4,B),(5,C)(6,D)}
Result ={ (1,A),(6,B),(8,C),(6,D)}

Example

Command>zunion 2 src_key1 src_key2 withscores
Result> 1) "A"
2) "1"
3) "B"
4) "6"
5) "D"
6) "6"
7) "C"
8) "8"

Using weights

Usage: zunion 2 src_key1 src_key2 weights 2 3.
The first weight 2 is multiplied by each score of src_key1, and the second weight 3 is multiplied by each score of src_key2.

src_key1 ={ (1,A),(2,B),(3,C) } ->{ (1*2,A) (2*2,B)(3*2,C) }
src_key2 ={ (4,B),(5,C),(6,D) }->{ (4*3,B)(5*3,C) (6*3,D) }
Result ={ (2,A) (4+12,B)(6+15,C) (18,D) }
={ (2,A) (16,B)(21,C) (18,D) }

Example

Command>zunion 2 src_key1 src_key2 weights 2 3 withscores
Result> 1) "A"
2) "2"
3) "B"
4) "16"
5) "D"
6) "18"
7) "C"
8) "21"

Using aggregate

Usage: zunion 2 src_key1 src_key2 aggregate sum|min|max.
sum adds the scores. If aggregate is omitted, sum is the default.
min takes the smaller score of each member, and max takes the larger score.

src_key1 ={ (1,A),(2,B),(3,C) }
src_key2 ={ (4,B),(5,C),(6,D) }
aggregate min = { (1,A) (2,B),(3,C), (6,D) }
aggregate max = { (1,A) (4,B),(5,C), (6,D) }

Example

Command>zunion 2 src_key1 src_key2 aggregate min withscores
Result> 1) "A"
2) "1"
3) "B"
4) "2"
5) "C"
6) "3"
7) "D"
8) "6"
Command>zunion 2 src_key1 src_key2 aggregate max withscores
Result> 1) "A"
2) "1"
3) "B"
4) "4"
5) "C"
6) "5"
7) "D"
8) "6"


Syntax

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

  • Available since version 6.2.0.
  • Time complexity is O(N)+O(M*log(M)).
Related commands ZINTER, ZDIFF

<< ZREMRANGEBYLEX ZUNION ZINTER >>

Email 답글이 올라오면 이메일로 알려드리겠습니다.