w3resource

Redis Sorted Sets: ZINTERSTORE

Redis ZINTERSTORE Command

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.

Here are some common uses and scenarios for the "ZINTERSTORE" command:

  • Data Intersection: Compute the intersection of sorted sets to find common elements across multiple datasets.
  • Data Analysis: Perform set-based operations for analytical purposes, such as finding common elements with specific scores.
  • Aggregate Scores: Calculate aggregated scores based on intersections of scores from multiple sorted sets.
  • Data Integration: Integrate and consolidate data by computing intersections of sorted sets representing different data segments.
  • Efficient Data Processing: Efficiently process and derive insights from datasets by leveraging set operations on sorted sets.

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



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/redis/redis-zinterstore-destination-numkeys-key.php