In Redis, the ZINTER
command returns the intersection of the specified sorted sets. In other words, it returns only those members that are present in all sorted sets.
Syntax
The syntax goes like this:
ZINTER numkeys key [key ...] [WEIGHTS weight [weight ...]]
[AGGREGATE <SUM | MIN | MAX>] [WITHSCORES]
The following examples demonstrate how the arguments are used.
Example
Suppose we create the following sorted sets:
ZADD cats 1 meow 2 fluffy 3 scratch
And:
ZADD dogs 1 bark 2 woof 3 fluffy 4 scratch
Let’s use the ZINTER
command to return the intersection of those two sorted sets:
ZINTER 2 cats dogs
Result:
1) "fluffy" 2) "scratch"
As expected, only those members that are in both sorted sets are returned.
Include the Scores
We can use the WITHSCORES
argument to include the scores:
ZINTER 2 cats dogs WITHSCORES
Result:
1) "fluffy" 2) "5" 3) "scratch" 4) "7"
By default, the scores for each member are added together from their respective sorted sets. In this example, fluffy
has a score of 5
(2
from cats
plus 3
from dogs
) and scratch has a score of 7
(3
from cats
and 4
from dogs
)..
The AGGREGATE
Argument
In the previous example we saw how the scores were added together to produce an aggregate score. We can change this with the AGGREGATE
argument so that it returns either the minimum or maximum score from the original sorted set.
To do this we use the AGGREGATE
keyword followed by the option we want to use.
We can use MIN
for “minimum”:
ZINTER 2 cats dogs AGGREGATE MIN WITHSCORES
Result:
1) "fluffy" 2) "2" 3) "scratch" 4) "3"
And MAX
for “maximum”:
ZINTER 2 cats dogs AGGREGATE MAX WITHSCORES
Result:
1) "fluffy" 2) "3" 3) "scratch" 4) "4"
We can also explicitly state SUM
for the default behaviour of adding the scores:
ZINTER 2 cats dogs AGGREGATE SUM WITHSCORES
Result:
1) "fluffy" 2) "5" 3) "scratch" 4) "7"
The WEIGHTS
Argument
We can use the WEIGHTS
argument to specify a multiplication factor for each input sorted set. This means that the score of every element in the input sorted set is multiplied by this factor before being passed to the aggregation function.
We can apply a different weighting to each sorted set.
Example:
ZINTER 2 cats dogs WEIGHTS 100 100 WITHSCORES
Result:
1) "fluffy" 2) "500" 3) "scratch" 4) "700"
In this example, I specified that the members of both sets should be multiplied by 100 and the result reflects this.
Here’s an example of applying a different weighting to each set:
ZINTER 2 cats dogs WEIGHTS 100 200 WITHSCORES
Result:
1) "fluffy" 2) "800" 3) "scratch" 4) "1100"
This time each element in the cats
sorted set was multiplied by 100, while each element in the dogs
sorted set was multiplied by 200.
The default multiplication factor is 1.
Wrong Data Type
If any of the keys don’t contain a sorted set, an error occurs:
ZINTER 2 cats country
Result:
(error) WRONGTYPE Operation against a key holding the wrong kind of value
However, non-sorted sets are fine:
ZINTER 2 cats users
Result:
1) "fluffy" 2) "scratch"
In this example, the users
key contains a non-sorted set.