ZREVRANGEBYLEX
Introduction
In Dragonfly, as well as in Redis and Valkey, the ZREVRANGEBYLEX
command is used to return a range of members in a sorted set, where the elements are lexicographically ordered within a specific range, but in reverse order (from higher to lower strings).
This is particularly useful for retrieving ordered elements when the lexicographical order of string values matters, often in cases like word dictionaries, leaderboard systems, or alphabetical searches.
Syntax
ZREVRANGEBYLEX key max min [LIMIT offset count]
- Time complexity: O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements being returned. If M is constant (e.g. always asking for the first 10 elements with LIMIT), you can consider it O(log(N)).
- ACL categories: @read, @sortedset, @slow
Parameter Explanations
key
: The key of the sorted set.max
andmin
:- The maximum and minimum lexicographical values to filter the members.
- Valid
max
andmin
values must start with(
or[
to indicate exclusive or inclusive bounds respectively. - The
+
and-
special values can be used to specify positive and negative infinity strings, respectively.
LIMIT offset count
(optional): Limits the number of elements returned.offset
specifies how many elements to skip, andcount
specifies the maximum number of elements to return.
Return Values
- An array of strings representing the elements in reverse lexicographical order between
max
andmin
.
Code Examples
Basic Example
Return elements in reverse lexicographical order between d
and b
inclusive:
dragonfly$> ZADD myzset 0 "a" 0 "b" 0 "c" 0 "d" 0 "e"
(integer) 5
dragonfly$> ZREVRANGEBYLEX myzset [d [b
1) "d"
2) "c"
3) "b"
Exclusive Range Example
Return elements between d
(exclusive) and b
(inclusive):
dragonfly$> ZADD myzset 0 "a" 0 "b" 0 "c" 0 "d" 0 "e"
(integer) 5
dragonfly$> ZREVRANGEBYLEX myzset (d [b
1) "c"
2) "b"
Using LIMIT
to Restrict Output
Return only the first 2 elements in reverse lexicographical order between f
and a
, skipping the first element (offset=1
):
dragonfly$> ZADD myzset 0 "a" 0 "b" 0 "c" 0 "d" 0 "e" 0 "f"
(integer) 6
dragonfly$> ZREVRANGEBYLEX myzset "[f" "[a" LIMIT 1 2
1) "e"
2) "d"
Using Special Values
Return elements between +
and -
:
dragonfly$> ZADD myzset 0 "a" 0 "b" 0 "c" 0 "d" 0 "e"
(integer) 5
dragonfly$> ZREVRANGEBYLEX myzset + -
1) "e"
2) "d"
3) "c"
4) "b"
5) "a"
Since we are using the ZREVRANGEBYLEX
command, the elements are returned in reverse lexicographical order.
Thus, the following example returns an empty array when the range is specified from -
to +
:
dragonfly$> ZADD myzset 0 "a" 0 "b" 0 "c" 0 "d" 0 "e"
(integer) 5
dragonfly$> ZREVRANGEBYLEX myzset - +
(empty array)
Best Practices
- When dealing with large sorted sets, you should apply the
LIMIT
option to control the number of elements returned. This improves performance and helps avoid overwhelming system memory. - Ensure that the provided
min
andmax
are valid lexicographical ranges (i.e., string values), and consider whether you want the range to include or exclude the boundary elements.
Common Mistakes
- Using the wrong order of
max
andmin
.ZREVRANGEBYLEX
expectsmax
to be greater thanmin
in lexicographical order, as the range starts from higher to lower. - Ignoring the difference between inclusive and exclusive syntax when specifying the
max
andmin
parameters.[
includes the boundary, while(
excludes it.
FAQs
What happens if the key does not exist?
If the specified key does not exist, ZREVRANGEBYLEX
returns an empty array.
Can I use the command with non-string elements in the set?
No, the command works only with string members, as it operates based on lexicographical (alphabetical) ordering, which does not apply to numeric values. Sorted set members are treated as strings even if they look like numeric values.
What happens if no elements in the range satisfy the criteria?
If no members in the sorted set fall within the specified range, an empty array is returned.