{"acl_categories":["@read","@sortedset","@slow"],"arguments":[{"display_text":"key","key_spec_index":0,"name":"key","type":"key"},{"display_text":"min","name":"min","type":"string"},{"display_text":"max","name":"max","type":"string"},{"arguments":[{"display_text":"offset","name":"offset","type":"integer"},{"display_text":"count","name":"count","type":"integer"}],"name":"limit","optional":true,"token":"LIMIT","type":"block"}],"arity":-4,"categories":["docs","develop","stack","oss","rs","rc","oss","kubernetes","clients"],"command_flags":["readonly"],"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)).","description":"Returns members in a sorted set within a lexicographical range.","duplicateOf":"head:data-ai-metadata","group":"sorted-set","key_specs":[{"RO":true,"access":true,"begin_search":{"spec":{"index":1},"type":"index"},"find_keys":{"spec":{"keystep":1,"lastkey":0,"limit":0},"type":"range"}}],"location":"body","since":"2.8.9","syntax_fmt":"ZRANGEBYLEX key min max [LIMIT offset count]","title":"ZRANGEBYLEX","tableOfContents":{"sections":[{"id":"how-to-specify-intervals","title":"How to specify intervals"},{"id":"details-on-strings-comparison","title":"Details on strings comparison"},{"id":"examples","title":"Examples"},{"id":"redis-software-and-redis-cloud-compatibility","title":"Redis Software and Redis Cloud compatibility"},{"id":"return-information","title":"Return information"}]},"codeExamples":[]}
zrangeByLex(
key: byte[],
min: byte[],
offset: byte[] max final int,
count: int
) → List<byte[]>zrangeByLex(
key: String,
min: String,
max: String
) → List<String>zrangeByLex(
key: String,
min: String,
offset: String max final int,
count: int
) → List<String>
zrangebylex(
key: K, // the key.min: String,
max: String
) → List<V>// List<V> array-reply list of elements in the specified range. @since 4.3zrangebylex(
key: K, // the key.range: Range<? extends V>// the range.
) → List<V>// List<V> array-reply list of elements in the specified range. @since 4.3zrangebylex(
key: K, // the key.min: String,
max: String,
offset: long,
count: long
) → List<V>// List<V> array-reply list of elements in the specified range. @since 4.3zrangebylex(
key: K, // the key.range: Range<? extends V>, // the range.limit: Limit// the limit.
) → List<V>// List<V> array-reply list of elements in the specified range. @since 4.3
zrangebylex(
key: K, // the key.min: String,
max: String
) → RedisFuture<List<V>>// List<V> array-reply list of elements in the specified range. @since 4.3zrangebylex(
key: K, // the key.range: Range<? extends V>// the range.
) → RedisFuture<List<V>>// List<V> array-reply list of elements in the specified range. @since 4.3zrangebylex(
key: K, // the key.min: String,
max: String,
offset: long,
count: long
) → RedisFuture<List<V>>// List<V> array-reply list of elements in the specified range. @since 4.3zrangebylex(
key: K, // the key.range: Range<? extends V>, // the range.limit: Limit// the limit.
) → RedisFuture<List<V>>// List<V> array-reply list of elements in the specified range. @since 4.3
zrangebylex(
key: K, // the key.min: String,
max: String
) → Flux<V>// V array-reply list of elements in the specified range. @since 4.3zrangebylex(
key: K, // the key.range: Range<? extends V>// the range.
) → Flux<V>// V array-reply list of elements in the specified range. @since 4.3zrangebylex(
key: K, // the key.min: String,
max: String,
offset: long,
count: long
) → Flux<V>// V array-reply list of elements in the specified range. @since 4.3zrangebylex(
key: K, // the key.range: Range<? extends V>, // the range.limit: Limit// the limit.
) → Flux<V>// V array-reply list of elements in the specified range. @since 4.3
SortedSetRangeByValue(
key: RedisKey, // The key of the sorted set.min: RedisValue, // The min value to filter by.max: RedisValue, // The max value to filter by.exclude: Exclude, // Which of min and max to exclude (defaults to both inclusive).skip: long, // How many items to skip.take: long, // How many items to take.flags: CommandFlags// The flags to use for this operation.
) → RedisValue[]// List of elements in the specified score range.
SortedSetRangeByValue(
key: RedisKey, // The key of the sorted set.min: RedisValue, // The min value to filter by.max: RedisValue, // The max value to filter by.exclude: Exclude, // Which of min and max to exclude (defaults to both inclusive).skip: long, // How many items to skip.take: long, // How many items to take.flags: CommandFlags// The flags to use for this operation.
) → RedisValue[]// List of elements in the specified score range.
zrangebylex(
key: K,
min: M,
max: MM
) → (Vec<String>)zrangebylex_limit(
key: K,
min: M,
max: MM,
offset: isize,
count: isize
) → (Vec<String>)
zrangebylex(
key: K,
min: M,
max: MM
) → (Vec<String>)zrangebylex_limit(
key: K,
min: M,
max: MM,
offset: isize,
count: isize
) → (Vec<String>)
Available since:
Redis Open Source 2.8.9
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)).
When all the elements in a sorted set are inserted with the same score, in order to force lexicographical ordering, this command returns all the elements in the sorted set at key with a value between min and max.
If the elements in the sorted set have different scores, the returned elements are unspecified.
The elements are considered to be ordered from lower to higher strings as compared byte-by-byte using the memcmp() C function. Longer strings are considered greater than shorter strings if the common part is identical.
The optional LIMIT argument can be used to only get a range of the matching
elements (similar to SELECT LIMIT offset, count in SQL). A negative count
returns all elements from the offset.
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.
How to specify intervals
Valid start and stop must start with ( or [, in order to specify
if the range item is respectively exclusive or inclusive.
The special values of + or - for start and stop have the special
meaning or positively infinite and negatively infinite strings, so for
instance the command ZRANGEBYLEX myzset - + is guaranteed to return
all the elements in the sorted set, if all the elements have the same
score.
Details on strings comparison
Strings are compared as binary array of bytes. Because of how the ASCII character
set is specified, this means that usually this also have the effect of comparing
normal ASCII characters in an obvious dictionary way. However this is not true
if non plain ASCII strings are used (for example utf8 strings).
However the user can apply a transformation to the encoded string so that
the first part of the element inserted in the sorted set will compare as the
user requires for the specific application. For example if I want to
add strings that will be compared in a case-insensitive way, but I still
want to retrieve the real case when querying, I can add strings in the
following way:
ZADD autocomplete 0 foo:Foo 0 bar:BAR 0 zap:zap
Because of the first normalized part in every element (before the colon character), we are forcing a given comparison, however after the range is queries using ZRANGEBYLEX the application can display to the user the second part of the string, after the colon.
The binary nature of the comparison allows to use sorted sets as a general
purpose index, for example the first part of the element can be a 64 bit
big endian number: since big endian numbers have the most significant bytes
in the initial positions, the binary comparison will match the numerical
comparison of the numbers. This can be used in order to implement range
queries on 64 bit values. As in the example below, after the first 8 bytes
we can store the value of the element we are actually indexing.
Examples
Redis Software and Redis Cloud compatibility
Redis Software
Redis Cloud
Notes
✅ Standard ✅ Active-Active
✅ Standard ✅ Active-Active
Deprecated as of Redis v6.2.0.
Return information
Array reply: a list of elements in the specified score range.
Array reply: a list of elements in the specified score range.