w3resource

Redis Sorted Sets: ZRANGEBYLEX

Redis ZRANGEBYLEX Command

Redis ZRANGEBYLEX command is used to return all the elements in the sorted set at the key with a value between minimum and maximum, when all the elements in a sorted set are inserted with the same score, in order to force lexicographical ordering.

  • min, max are the range of the member. To view all the -, + use.
  • min, if you give the max value must be used '[' or '(' in the front. 
  • '[' Is used to contain a value, and '(' is used to block out. 

Both start and stop are zero-based indexes,

  • 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,

  • -1 being the last element of the sorted set
  • -2 the penultimate element and so on.

Here are some common uses and scenarios for the "ZRANGEBYLEX" command:

  • Lexicographical Queries: Fetch elements within a specified lexicographical range.
  • Alphabetical Sorting: Retrieve elements sorted alphabetically based on their keys.
  • Data Segmentation: Segment data based on lexicographical ranges for further processing.
  • String Management: Manage and retrieve strings within specific lexicographical bounds.
  • Indexing: Create indices for efficient retrieval of elements based on lexicographical criteria.

Syntax:

ZRANGEBYLEX key min max [LIMIT offset count]

Available since

2.8.9.

Return Value

Array reply, a list of elements in the specified score range.

Return Value Type

Array

Example: Redis ZRANGEBYLEX

127.0.0.1:6379> ZADD mycity 1 Delhi 2 London 3 Paris 4 Tokyo 5 NewYork 6 Seoul
(integer) 6
127.0.0.1:6379> ZRANGE mycity 0 -1
1) "Delhi"
2) "London"
3) "Paris"
4) "Tokyo"
5) "NewYork"
6) "Seoul"
127.0.0.1:6379> ZRANGEBYLEX mycity - +
1) "Delhi"
2) "London"
3) "Paris"
4) "Tokyo"
5) "NewYork"
6) "Seoul"
127.0.0.1:6379> ZRANGEBYLEX mycity "[London" +
1) "London"
2) "Paris"
3) "Tokyo"
4) "NewYork"
5) "Seoul"
127.0.0.1:6379> ZRANGEBYLEX mycity "(London" +
1) "Paris"
2) "Tokyo"
3) "NewYork"
4) "Seoul"
127.0.0.1:6379> ZRANGEBYLEX mycity "(London" "(Seoul"
1) "Paris"

Example: Redis ZRANGEBYLEX: Use limit offset count

127.0.0.1:6379> ZADD mycity 1 Delhi 2 London 3 Paris 4 Tokyo 5 NewYork 6 Seoul
(integer) 6
127.0.0.1:6379> ZRANGE mycity 0 -1
1) "Delhi"
2) "London"
3) "Paris"
4) "Tokyo"
5) "NewYork"
6) "Seoul"
127.0.0.1:6379> ZRANGEBYLEX mycity - + LIMIT 0 2
1) "Delhi"
2) "London"
127.0.0.1:6379> ZRANGEBYLEX mycity - + LIMIT 2 3
1) "Paris"
2) "Tokyo"
3) "NewYork"

Previous: ZRANGE
Next: ZRANGEBYSCORE



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/redis/redis-zrangebylex-key-min-max.php