Redis ZINTERSTORE Command Explained

In Redis, the ZINTERSTORE command computes the intersection of the specified sorted sets and stores the result in the specified key. It works the same as the ZINTER command, except that it stores the result instead of returning it.

If the destination key already exists, it’s overwritten.

Syntax

The syntax goes like this:

ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight
  [weight ...]] [AGGREGATE <SUM | MIN | MAX>]

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 ZINTERSTORE command to capture the intersection of those two sorted sets:

ZINTERSTORE animals 2 cats dogs

Result:

(integer) 2

The integer reply of 2 indicates that two members were added to the animals key.

Let’s check that key:

ZRANGE animals 0 -1 WITHSCORES

Result:

1) "fluffy"
2) "5"
3) "scratch"
4) "7"

As expected, only those members that are in both sorted sets were added to the key. If the key already exists, it’s overwritten with the new data.

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).

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”:

ZINTERSTORE animals 2 cats dogs AGGREGATE MIN

Result:

(integer) 2

Let’s check the contents:

ZRANGE animals 0 -1 WITHSCORES

Result:

1) "fluffy"
2) "2"
3) "scratch"
4) "3"

We can see that it used the minimum values for each of the members.

And MAX for “maximum”:

ZINTERSTORE animals 2 cats dogs AGGREGATE MAX

Result:

(integer) 2

Let’s check the contents:

ZRANGE animals 0 -1 WITHSCORES

Result:

1) "fluffy"
2) "3"
3) "scratch"
4) "4"

We can also explicitly state SUM for the default behaviour of adding the scores:

ZINTERSTORE animals 2 cats dogs AGGREGATE SUM

Result:

(integer) 2

And check the contents:

ZRANGE animals 0 -1 WITHSCORES

Result:

1) "fluffy"
2) "5"
3) "scratch"
4) "7"

As seen in these examples, when the destination key already exists, it’s overwritten with the new data.

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:

ZINTERSTORE animals 2 cats dogs WEIGHTS 100 100

Result:

(integer) 2

And check the contents:

ZRANGE animals 0 -1 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:

ZINTERSTORE animals 2 cats dogs WEIGHTS 100 200

Result:

(integer) 2

And check the contents:

ZRANGE animals 0 -1 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:

ZINTERSTORE animals 2 cats country

Result:

(error) WRONGTYPE Operation against a key holding the wrong kind of value

However, non-sorted sets are fine:

ZINTERSTORE animals 2 cats users

Result:

(integer) 2

Here, the users key contains a set (not a sorted set). We can verify this with the TYPE command:

TYPE users

Result:

set