I am reading Josiah Carlson's Redis in Action book.
In chapter 2 he re-scales the members of a ZSET to half of their original scores using the following statement in Python:
conn.zinterstore('viewed:', {'viewed:': .5})
My main issue with this is as follows:
The default aggregation function is SUM
so wouldn't this just end up adding the scores to the scores I already have ?
Wouldn't the scores be SCORE + 0.5 * SCORE
?
Why is it just overwriting the value ?
Github link to code here.
Thanks.
You're thinking of this as the intersection of the viewed:
set with itself—an intersection of two sets. In fact, since there's only one element in the second argument, there's only one set in the intersection. Every element of that set is therefore in the intersection, and its score is weighted by 0.5.
Perhaps you were thrown off by the fact that the first argument is viewed:
? That's just the destination key to be saved to, it doesn't participate in the intersection. You can see how the Python arguments are mapped to Redis arguments here.
Going at it from the other direction, this is what the code would actually look like if it was computing SCORE + 0.5 * SCORE
:
conn.zinterstore('viewed:', {`viewed:': 1, 'viewed:': .5})