w3resource

Redis Sorted Sets: ZINTERSTORE

ZINTERSTORE destination numkeys key [key ...]

Redis ZINTERSTORE command is used to compute the intersection of specified number of input keys sorted sets given by the specified keys, and stores the result in a specified key.

Syntax:

ZINTERSTORE KEY INCREMENT MEMBER

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 ZINTERSTORE

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> ZINTERSTORE desset 2 srcset1 srcset2
(integer) 2
127.0.0.1:6379> ZRANGE desset 0 -1
1) "N"
2) "O"
127.0.0.1:6379> ZRANGE desset 0 -1 WITHSCORE
(error) ERR syntax error
127.0.0.1:6379> ZRANGE desset 0 -1 WITHSCORES
1) "N"
2) "9"
3) "O"
4) "9"

Example: Redis ZINTERSTORE: 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> ZINTERSTORE desset 2 srcset1 srcset2 WEIGHTS 2 3
(integer) 2
127.0.0.1:6379> ZRANGE desset 0 -1 WITHSCORES
1) "O"
2) "20"
3) "N"
4) "21"

Example: Redis ZINTERSTORE: 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> ZINTERSTORE desset 2 srcset1 srcset2 AGGREGATE MIN
(integer) 2
127.0.0.1:6379> ZRANGE desset 0 -1 WITHSCORES
1) "O"
2) "2"
3) "N"
4) "3"
127.0.0.1:6379> ZINTERSTORE desset 2 srcset1 srcset2 AGGREGATE MAX
(integer) 2
127.0.0.1:6379> ZRANGE desset 0 -1 WITHSCORES
1) "N"
2) "6"
3) "O"
4) "7"

Example: Redis ZINTERSTORE: 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> ZINTERSTORE desset 3 srcset1 srcset2 srcset3
(integer) 1
127.0.0.1:6379> ZRANGE desset 0 -1 WITHSCORES
1) "O"
2) "10"

Previous: ZINCRBY
Next: ZLEXCOUNT



Follow us on Facebook and Twitter for latest update.