## Redis Learning Note – 08 Command in Redis (Sorted Set)

Every member of a Sorted Set is associated with score, that is used in order to take the sorted set ordered, from the smallest to the greatest score. While members are unique, scores may be repeated. Sorted sets are sorted by their score in an ascending way. The same element only exists a single time, no repeated elements are permitted. The score can be modified both by ZADD that will update the element score, and as a side effect, its position on the sorted set, and by ZINCRBY that can be used in order to update the score relatively to its previous value. The current score of an element can be retrieved using the ZSCORE command, that can also be used to verify if an element already exists or not.

When multiple elements have the same score, they are ordered lexicographically (they are still ordered by score as a first key, however, locally, all the elements with the same score are relatively ordered lexicographically). The lexicographic ordering used is binary, it compares strings as array of bytes.

ZADD key score member [score member …]

Adds all the specified members with the specified scores to the sorted set stored at key. The score values should be the string representation of a double precision floating point number. +inf and -inf values are valid values as well.

ZREM key member [member …]

Removes the specified members from the sorted set stored at key.

ZCARD key

Returns the sorted set cardinality (number of elements) of the sorted set stored at key.

ZRANGE key start stop [WITHSCORES]

Returns the specified range of elements in the sorted set stored at key. Both start and stop are zero-based indexes, where 0 is the first element, 1 is the next element and so on. They can also be negative numbers indicating offsets from the end of the sorted set. If start is larger than the largest index in the sorted set, or start > stop, an empty list is returned. If stop is larger than the end of the sorted set Redis will treat it like it is the last element of the sorted set. It is possible to pass the WITHSCORES option in order to return the scores of the elements together with the elements.

ZREVRANK key member

Returns the rank of member in the sorted set stored at key, with the scores ordered from high to low.

ZREVRANGE key start stop [WITHSCORES]

Returns the specified range of elements in the sorted set stored at key. The elements are considered to be ordered from the highest to the lowest score. Descending lexicographical order is used for elements with equal score.

ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]

Returns all the elements in the sorted set at key with a score between min and max (including elements with score equal to min or max). The elements are considered to be ordered from low to high scores.

The elements having the same score are returned in lexicographical order (this follows from a property of the sorted set implementation in Redis and does not involve further computation).

The optional LIMIT argument can be used to only get a range of the matching elements (similar to SELECT LIMIT offset, count in SQL). Keep in mind that if offset is large, the sorted set needs to be traversed for offset elements before getting to the elements to return, which can add up to O(N) time complexity.

min and max can be -inf and +inf, so that you are not required to know the highest or lowest score in the sorted set to get all elements from or up to a certain score.

By default, the interval specified by min and max is closed (inclusive). It is possible to specify an open interval (exclusive) by prefixing the score with the character (.

ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]

In contrary to the default ordering of sorted sets, for this command the elements are considered to be ordered from high to low scores.

ZREMRANGEBYRANK key start stop

Removes all elements in the sorted set stored at key with rank between start and stop.

ZREMRANGEBYSCORE key min max

Removes all elements in the sorted set stored at key with a score between min and max (inclusive).

ZUNIONSTORE destination numkeys key [key …] [WEIGHTS weight [weight …]] [AGGREGATE SUM|MIN|MAX]

Computes the union of numkeys sorted sets given by the specified keys, and stores the result in destination. It is mandatory to provide the number of input keys (numkeys) before passing the input keys and the other (optional) arguments.

By default, the resulting score of an element is the sum of its scores in the sorted sets where it exists.

Using the WEIGHTS option, it is possible to specify a multiplication factor for each input sorted set. This means that the score of every element in every input sorted set is multiplied by this factor before being passed to the aggregation function. When WEIGHTS is not given, the multiplication factors default to 1.

With the AGGREGATE option, it is possible to specify how the results of the union are aggregated. This option defaults to SUM, where the score of an element is summed across the inputs where it exists. When this option is set to either MIN or MAX, the resulting set will contain the minimum or maximum score of an element across the inputs where it exists.

If destination already exists, it is overwritten.

ZINTERSTORE destination numkeys key [key …] [WEIGHTS weight [weight …]] [AGGREGATE SUM|MIN|MAX]

Computes the intersection of numkeys sorted sets given by the specified keys, and stores the result in destination.

Source:

http://redis.io/commands#sorted_set

http://redis.io/topics/data-types#sorted-sets