zinterstore
ZINTERSTORE
Compute the intersection and store it in a new set
Usage: zinterstore dest_key 2 src_key1 src_key2.
The result set is stored in dest_key.
Each member score is added.
| src_key1 = | { (1,A), | (2,B), | (3,C) | } | |
| src_key2 = | { | (4,B), | (5,C) | (6,D) | } |
| dest_key = | { | (6,B), | (8,C) | } | |
Example
| Command> | zinterstore dest_key 2 src_key1 src_key2 |
| Result> | 4 |
| Command> | zrange dest_key 0 -1 withscores
|
| Result> |
0) 6 -> B
1) 8 -> C
|
View animation
Using weights
Usage: zinterstore dest_key 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) } | | -> | { (2*2,B) | (3*2,C) } |
| src_key2 = | { | (4,B), | (5,C), | (6,D) } | -> | { (4*3,B) | (5*3,C) } |
| dest_key | | | | | = | { (4+12,B) | (6+15,C) } |
| | | | | | = | { (16,B) | (21,C) } |
Example
| Command> | zinterstore dest_key 2 src_key1 src_key2 weights 2 3 |
| Result> | 2 |
| Command> | zrange dest_key 0 -1 withscores
|
| Result> |
0) 16 -> B
1) 21 -> C
|
View animation
Using aggregate
Usage: zinterstore dest_key 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) } |
| dest_key aggregate min = | { | (2,B), | (3,C) } | |
| dest_key aggregate max = | { | (4,B), | (5,C) } | |
Example
| Command> | zinterstore dest_key 2 src_key1 src_key2 aggregate min |
| Result> | 4 |
| Command> | zrange dest_key 0 -1 withscores
|
| Result> |
0) 2 -> B
1) 3 -> C
|
| Command> | zinterstore dest_key 2 src_key1 src_key2 aggregate max |
| Result> | 4 |
| Command> | zrange dest_key 0 -1 withscores
|
| Result> |
0) 4 -> B
1) 5 -> C
|
View animation
Compute the intersection of three sets and store it
Usage: zinterstore dest_key 3 src_key1 src_key2 src_key3.
| src_key1 = | { (1,A), | (2,B), | (3,C) | } | |
| src_key2 = | { | (4,B), | (5,C) | (6,D) | } |
| src_key3 = | { | | (7,C) | (8,D) | (9,E) } |
| dest_key = | { | | (15,C) | } | |
There is no limit on the number of source keys.
A source key can also be used as the destination key
ZINTERSTORE src_key1 src_key1 src_key2
The previous contents of src_key1 are overwritten with the result.
Syntax
ZINTERSTORE dest_key numkeys src_key [src_key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
- Available since version 2.0.0.
- Time complexity is O(N)+O(M log(M)).
N is the total number of members in the input sets, and M is the number of members in the result set.