Updated February 17, 2023
Introduction to Redis ZRANGE
Redis ZRANGE performs different types of queries in range by using the index, score, and using lexicographical order. From the version of redis 6.0, the ZRANGE command will replace the ZREVRANGE and other commands. Basically, the redis ZRANGE command is returning the specified range of elements from the sorted set which was stored in the key. The element is considered in an order from a score of lowest to the highest.
Key Takeaways
- Redis ZRANGE command is used to fetch the members from the sorted set, which we have defined using the ZRANGE command.
- We are using the ZRANGE command with the REV argument to reverse the sorted order; this command has many capabilities and will replace ZRANGEbyscore and other multiple commands.
What is Redis ZRANGE?
In redis ZRANGE we are using the lexicographical order of elements by using equal scores. Both the start and stop elements contain zero indexes where we can say that 0 is the first element and 1 is considered as the next element. It will also indicate the negative numbers from the end of the sorted set that we have used.
Redis sorted set contains the non-repeating and unique string types member in an ordered manner. By default, the sorted set is ordered in ascending order by using the score values. The sorted set maintains the zero-based index, from which member is ranked 0, 1, and so on. When the scores of two or more members are similar, lexicographical ordering is used. The sorted set of orders is ordered by using a ranked score.
Redis ZRANGE Sorted Set
We don’t need to order the Redis ZRANGE sorted set element again because it is already in the ordered list. We can quickly query the member. Redis sorted set is built on the data structure that was dual ported and allows us to add, update, remove, and read operations that were used to execute the time complexity operations. In the redis ZRANGE sorted set, the start and stop will contain the zero-based indexes with 0 as the first element and -1 as the last element.
Below example shows how the redis ZRANGE sorted set works as follows:
First, we define the ZADD command to add the key and elements. We are defining the key name as ZRANGE_key and defining the three elements as follows. In the below example we have added three elements but by using ZRANGE sorted set it only shows only 2 elements because we have defined the range as 0 to 1.
Code:
ZADD zrange_key 1 val1 2 val2 3 val3
ZRANGE zrange_key 0 1
Output:
In the below example, we define the key name with the zadd command as ZRANGE_key2 and define the five elements. While using ZRANGE we are using start as 1 and end as 5, so it will skip the first value and then retrieve the remaining values as follows.
Code:
ZADD zrange_key2 1 val1 2 val2 3 val3 4 val4 5 val5
ZRANGE zrange_key2 1 5
Output:
Redis ZRANGE Command
The redis ZRANGE command is used to retrieve a range of numbers from a sorted set that was stored in a key using another redis command. The below example shows the ZRANGE command by using byscore argument, we are using starting number as zero and the ending number as 1 and 4 as follows.
Code:
ZADD zrange_key3 1 val1 2 val2 3 val3 4 val4 5 val5
ZRANGE zrange_key3 0 1 BYSCORE
ZRANGE zrange_key3 0 4 BYSCORE
Output:
In the below example, we are not defining any argument with the ZRANGE command. We are using only the start and end numbers for displaying the elements. We are creating the key name as ZRANGE_key4 and defining the five values with the specified keys as follows.
Code:
ZADD zrange_key4 1 val1 2 val2 3 val3 4 val4 5 val5
ZRANGE zrange_key4 0 4
Output:
In the below example, we are using only the start number as 0 and the end number as -1 for displaying the elements. We are creating the key name as ZRANGE_key5 and defining the five values with the specified keys as follows.
Code:
ZADD zrange_key5 1 val1 2 val2 3 val3 4 val4 5 val5
ZRANGE zrange_key5 0 -1
Output:
In the below example, we are using the start number as 0 and the end number as 1 for displaying the elements also we are using REV arguments. We are creating the key name as ZRANGE_key6 and defining the three values with the specified key as follows.
Code:
ZADD zrange_key6 1 val1 2 val2 3 val3
ZRANGE zrange_key6 0 1 REV
Output:
In the below example, we are using the start number as 0 and the end number as 3 for displaying the elements also we are using withscores arguments. We are creating the key name as ZRANGE_key7 and defining the three values with the specified keys as follows.
Code:
ZADD zrange_key7 1 val1 2 val2 3 val3
ZRANGE zrange_key7 0 1 WITHSCORES
Output:
Redis ZRANGE Arguments
The ZRANGE command contains multiple arguments. Below is the syntax of redis ZRANGE as follows:
Syntax:
ZRANGE name_of_key Start_num end_num [REV] [BYSCORE | BYLEX] [limit offset count]
ZRANGE will contain multiple arguments as follows. Start and end argument is important in the ZRANGE command.
- Name_of_key – This is defined as a key name that we are using with the ZRANGE command.
- Start – This is defined as a range query starting value.
- End – This is defined as a range query of ending value.
- Byscore – We are specifying the range based on a score.
- Bylex – We are specifying the range based on the value which was lexicographical.
- Rev – This argument is reversing the order of returned members, where the index is holding the score which was highest.
- Limit – This argument is limiting the number which was returned by specified offset.
- Withscores – As we know ZRANGE command is only returning the members, to return a member with numbers we are using the withscores argument.
Top Redis ZRANGE Elements
In redis ZRANGE command elements is very important. We are using different types of elements in the ZRANGE command. The below example shows that we are using the start and end elements as 0 and 1 respectively.
Code:
ZADD key_zrange 1 val1 2 val2 3 val3
ZRANGE key_zrange0 1
Output:
In the below example, we are using 0 and -1 elements to define the redis ZRANGE as follows. We are defining the key name as key_range and defining the five-element value.
Code:
ZADD key_zrange1 1 val1 2 val2 3 val3 4 val4 5 val5
ZRANGE key_zrange1 0 -1
Output:
In the below example, we are using 1 and 1 elements to define the redis ZRANGE as follows. We are defining the key name as key_range2 and defining the five-element value.
Code:
ZADD key_zrange2 1 val1 2 val2 3 val3 4 val4 5 val5
ZRANGE key_zrange2 1 1
Output:
FAQs
Given below are the FAQs mentioned:
Q1. What is the use of redis ZRANGE command?
Answer: ZRANGE command is used to fetch the range members from the sorted set. We are using different types of ranges in the ZRANGE command.
Q2. Which arguments we are using with ZRANGE command?
Answer: We are using start, end, rev, Bylex, byscore, limit, and withscores arguments with the ZRANGE command.
Q3. What is the use of the withscores argument in the ZRANGE command?
Answer: In redis ZRANGE command withscores argument is used to display the number and values of elements.
Conclusion
In redis ZRANGE we are using lexicographical order of elements by using equal scores. Both the start and stop elements contain zero indexes where we can say that 0 is the first element and 1 is considered as the next element. It performs different types of queries in range by using the index, score, and using lexicographical order.
Recommended Articles
This is a guide to Redis ZRANGE. Here we discuss the introduction, redis ZRANGE sorted set, command, arguments, and elements. You may also have a look at the following articles to learn more –