All Products
Search
Document Center

Tair:exZset

Last Updated:Aug 29, 2023

The TairZset (exZset) data structure allows you to sort data of the DOUBLE type based on 256 dimensions. It is ideal for implementing leaderboards for use in industries such as gaming, live streaming, music, and e-commerce. TairZset improves data processing efficiency and is also easy to implement on the client side because you do not need to encode, decode, or encapsulate the data.

Overview

The Sorted Set (also known as ZSET) data structure of open source Redis allows you to sort DOUBLE-typed scores only by one dimension instead of multiple dimensions. For example, you can use the IEEE Standard for Floating-Point Arithmetic (IEEE 754) standard to implement multidimensional sorting by concatenating score data. However, this method has drawbacks such as complex logic, reduced precision, and unavailability of the EXZINCRBY command.

To help you implement multidimensional sorting, Alibaba Cloud developed the TairZset data structure. Compared with the preceding method, TairZset has the following advantages:
  • Allows DOUBLE-typed scores to be sorted based on a maximum of 256 dimensions. The scores are displayed from left to right based on their priorities.

    In a multidimensional sorting, a left score has higher priority than a right score. Take the comparison of three-dimensional scores in the score1#score2#score3 format as an example. TairZset compares the score1s of multiple three-dimensional scores and moves on to score2s only when score1s are equal. If score1s are not equal, the ranking of score1s represents the ranking of the three-dimensional scores involved. By the same logic, score3s are compared only if score2s are equal. If all score1s are equal and the same holds true for score2s and score3s, the involved multidimensional scores are ranked in ASCII sort order.

    For easier understanding, you can imagine number signs (#) as decimal points (.). This way, 0#99 < 99#90 < 99#99 can be seen as 0.99 < 99.90 < 99.99.

  • Supports the EXZINCRBY command. You no longer need to perform the following operations: retrieve current data, apply the increments to the data, and then write the data back to Tair databases.
  • Supports APIs similar to those available for open source Redis ZSET.
  • Allows you to implement regular leaderboards and distributed leaderboards to detect malware. If malware is detected, the feature generates alert events.
  • Supports the open source TairJedis client. For more information about the TairJedis client, visit GitHub. You can use the TairJedis client without the need to encode, decode, or encapsulate data. You can also encapsulate clients for other programming languages by referring to the open source code.
Typical scenarios
TairZset is ideal for implementing leaderboards for use in industries such as gaming, livestreaming, music, and e-commerce. The following section lists some examples:
  • Leaderboards for the livestreaming industry: In live matches, commentators are sorted by their current popularity value. If the popularity value is the same, they are sorted by the number of their likes. If the number of likes is also the same, they are sorted by the value of their gifts.
  • Leaderboards for medals: Participants are sorted by the number of their gold, silver, and bronze medals. If the number of gold medals is the same, they are sorted by the number of silver medals. If the number of silver medals is also the same, they are sorted by the number of bronze medals.
  • Leaderboards for the gaming industry: Players are sorted based on multiple factors, including their scores, task completion speeds, and achievement levels.

The module is open-sourced. For more information, visit GitHub.

Best practices

Prerequisites

The instance on which TairZset data is stored is a DRAM-based instance of Tair that runs the minor version of 1.7.1 or later. For more information about DRAM-based instances, see DRAM-based instances.
Note We recommend that you update your instance to the latest minor version for more features and higher stability. For more information, see Update the minor version of an instance. If your instance is a cluster instance or read/write splitting instance, we recommend that you update the proxy nodes in the instance to the latest minor version to ensure that all commands can be run as expected. For more information about cluster instances and read/write splitting instances, see Cluster architecture and Read/write splitting architecture.

Precautions

The TairZset data that you want to manage is stored on Tair instances.

TairZset commands

CommandSyntaxDescription
EXZADDEXZADD key [NX|XX] [CH] [INCR] score member [score member ...]Stores the specified element and its score in a TairZset key. You can specify multiple scores and elements.
Note To implement multidimensional sorting, you can separate the scores of each dimension with number signs (#). Example: 111#222#121. All elements of a key must have the same score format.
EXZINCRBYEXZINCRBY key increment memberIncreases the score of an element in a TairZset key. increment indicates the value that you want to add to the score.
EXZSCOREEXZSCORE key memberReturns the score of an element in a TairZset key. If the key or the element does not exist, a value of nil is returned.
EXZRANGEEXZRANGE key min max [WITHSCORES]Returns the elements of a TairZset key within the specified score range.
EXZREVRANGEEXZREVRANGE key min max [WITHSCORES]Returns the elements of a TairZset key within the specified score range. Elements are stored by score in descending order, and elements with the same score are sorted in reverse lexicographical order.
Note This command is similar to EXZRANGE except that this command sorts the results in reverse.
EXZRANGEBYSCOREEXZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]Returns the elements of a TairZset key whose scores are greater than or equal to the min value and are less than or equal to the max value. The returned elements are sorted by score in ascending order, and elements with the same score are returned in lexicographical order.
EXZREVRANGEBYSCOREEXZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]Returns the elements of a TairZset key whose scores are greater than or equal to the min value and are less than or equal to the max value. Contrary to the default sorting of TairZset elements, the elements returned by this command are sorted by score in descending order, and elements with the same score are sorted in reverse lexicographical order.
Note This command is similar to EXZRANGEBYSCORE except that this command sorts the results in reverse and places max in front of min.
EXZRANGEBYLEXEXZRANGEBYLEX key min max [LIMIT offset count]Returns the elements whose scores are between the min value and the max value when all elements of a key have the same score to ensure that elements are sorted in lexicographical order.
EXZREVRANGEBYLEXEXZREVRANGEBYLEX key max min [LIMIT offset count]Returns the elements whose scores are between the min value and the max value when all elements of a key have the same score to ensure that elements are sorted in lexicographical order.
Note This command is similar to EXZRANGEBYLEX except that this command sorts the results in reverse and places max in front of min.
EXZREMEXZREM key member [member ...]Removes specified elements from a TairZset key. If the specified element does not exist, the element is ignored.
EXZREMRANGEBYSCOREEXZREMRANGEBYSCORE key min maxRemoves the elements from a TairZset key whose scores are greater than or equal to the min value and are less than or equal to the max value.
EXZREMRANGEBYRANKEXZREMRANGEBYRANK key start stopRemoves the elements from a TairZset key whose ranks are within the range of the start value and the stop value.
EXZREMRANGEBYLEXEXZREMRANGEBYLEX key min maxRemoves the elements whose scores are between the min value and the max value when all elements of a key have the same score to ensure that elements are sorted in lexicographical order.
Note If you use the min and max parameters to specify the same range for the EXZREMRANGEBYLEX and EXZRANGEBYLEX commands, the elements removed by the EXZREMRANGEBYLEX command are the same as those returned by the EXZRANGEBYLEX command.
EXZCARDEXZCARD keyReturns the cardinality of a TairZset key. Cardinality indicates the number of elements in a key.
EXZRANKEXZRANK key member Returns the rank of an element in a TairZset key. Ranks are sorted by score in ascending order. Ranks use a zero-based numbering scheme, and the rank of the element that has the lowest score is 0. Rank is also known as index.
EXZREVRANKEXZREVRANK key memberReturns the rank of an element in a TairZset key. Ranks are sorted by score in descending order. Ranks use a zero-based numbering scheme, and the rank of the element that has the highest score is 0. Rank is also known as index.
Note This command is similar to EXZRANK except that this command sorts the results in reverse.
EXZCOUNTEXZCOUNT key min maxReturns the number of elements in a TairZset key whose scores are between the min value and the max value.
EXZLEXCOUNTEXZLEXCOUNT key min maxReturns the number of elements whose scores are between the min value and the max value when all elements of a key have the same score to ensure that elements are sorted in lexicographical order.
EXZRANKBYSCOREEXZRANKBYSCORE key score
Calculates the rank of the specified element score in a TairZset key. Ranks are sorted by score in ascending order. Ranks use a zero-based numbering scheme, and the rank of the element that has the lowest score is 0. Rank is also known as index.
Note If the score does not exist, the estimated rank of the score in the key is returned. By default, if the score already exists, Tair ranks the score in front of the existing score in the key.
EXZREVRANKBYSCOREEXZREVRANKBYSCORE key score
Calculates the rank of the specified element score in a TairZset key. Ranks are sorted by score in descending order. Ranks use a zero-based numbering scheme, and the rank of the element that has the highest score is 0. Rank is also known as index.
Note If the score does not exist, the estimated rank of the score in the key is returned. By default, if the score already exists, Tair ranks the score behind the existing score in the key.
DELDEL key [key ...]Deletes one or more TairZset keys.
Note The following list describes the conventions for the command syntax used in this topic:
  • Uppercase keyword: indicates the command keyword.
  • Italic text: indicates variables.
  • [options]: indicates that the enclosed parameters are optional. Parameters that are not enclosed by brackets must be specified.
  • A|B: indicates that the parameters separated by the vertical bars (|) are mutually exclusive. Only one of the parameters can be specified.
  • ...: indicates that the parameter preceding this symbol can be repeatedly specified.

EXZADD

ItemDescription
SyntaxEXZADD key [NX|XX] [CH] [INCR] score member [score member ...]
Time complexityO(N)
Command descriptionAdds the specified element with the specified score to a TairZset key. You can specify multiple scores and elements. The system uses different strategies based on whether the key and element exist:
Note To implement multidimensional sorting, you can separate the scores of each dimension with number signs (#). Example: 111#222#121. All elements of a key must have the same score format.
  • If the key exists but its data structure is not TairZset, an error is returned.
  • If the key does not exist, the system creates a TairZset key and adds the element to the key.
  • If the element is already an element of the key, the score of the element is updated and the element is reinserted into the right position to avoid disruptions to sorting.
  • Each score is represented by a string of double-precision floating-point numbers. The +inf and -inf values are valid values.
Parameter
  • NX: adds new elements and does not update existing elements.
  • XX: updates existing elements and does not add new elements.
  • CH: changes the command output from the number of new elements added to the total number of elements that have changed.
    Note Changed elements include new elements and existing elements with updated scores. If the score of an existing element in the command line does not change, the element is not counted as a changed element.
  • INCR: When this parameter is specified, EXZADD acts similarly to EXZINCRBY, which indicates that only one pair of score and element can be specified in this mode.
OutputThe output is an integer. The following rules describe the output:
  • If no parameters are specified, the output is the number of elements added to a TairZset key.
  • If the CH parameter is specified, the output is the number of elements that have changed (elements that have been added or updated).
  • If the INCR parameter is specified, the command returns the new score of the element as a string. If multidimensional scores are used, the score is returned in the "score1# score2# score3#..." format. Example: 2#0#6.
    Note If this command is stopped because the XX or NX parameter is included in this command, a value of nil is returned.
Example
Sample command:
EXZADD testkey NX 1#0#3 a 1#0#2 b
Sample output:
(integer) 2

EXZINCRBY

ItemDescription
SyntaxEXZINCRBY key increment member
Time complexityO(log(N))
Command descriptionIncreases the score of an element in a TairZset key. increment indicates the value that you want to add to the score. The system uses different strategies based on whether the key and element exist:
  • If the key exists but its data structure is not TairZset, an error is returned.
  • If the key does not exist, the system creates a TairZset key and uses the element as the only element of the key.
  • If the key has no element, the system adds an element whose score is the increment value to the key, which indicates that the original score of the element is assumed to be 0.0.
Note
  • To implement multidimensional sorting, you can separate the scores of each dimension with number signs (#). Example: 111#222#121. All elements of a key must have the same score format.
  • The score value is a string of numeric values and can be a string of double-precision floating-point numbers. If you want to lower the score of an element, specify a negative number.
ParameterNone
OutputThe new score of an element as a string. If multidimensional scores are used, the scores are returned in the "score1# score2# score3#..." format. Example: 2#0#6.
Example

Sample command:

EXZINCRBY testkey 2#2#1 a

Sample output:

"3#2#4"

EXZSCORE

ItemDescription
SyntaxEXZSCORE key member
Time complexityO(1)
Command descriptionReturns the score of an element in a TairZset key. If the key or the element does not exist, a value of nil is returned.
ParameterNone
OutputThe score of an element as a string. If multidimensional scores are used, the scores are returned in the "score1# score2# score3#..." format. Example: 2#0#6.
Example

Sample command:

EXZSCORE testkey a

Sample output:

"3#2#4"

EXZRANGE

ItemDescription
SyntaxEXZRANGE key min max [WITHSCORES]
Time complexityO(log(N)+M), where N indicates the number of elements in the TairZset key and M indicates the number of elements to be returned.
Command descriptionReturns the elements of a TairZset key within the specified score range.
Parameter
  • min and max: indicate zero-based indexes, where 0 is the first element and 1 is the next element. Other elements follow the same rule. You can use the two parameters to specify a closed interval.
    Note
    • The indexes can also be negative numbers, which indicate offsets from the end of TairZset elements returned. For example, -1 indicates the last element of a key and -2 indicates the second to last element of a key. Other elements follow the same rule.
    • To query all elements, you can set min to 0 and max to -1.
    • If the min value is greater than the index of the last element of a key or the max value, an empty list is returned.
  • WITHSCORES: returns the scores of elements. The scores are returned in the value1, score1, ..., valueN, scoreN format. Example:
    1) "b"
    2) "1#0#2"
    3) "a"
    4) "3#2#4"
OutputA list of elements within the specified score range. If the WITHSCORES parameter is specified, the scores of elements are returned.
Example

Sample command:

EXZRANGE testkey 0 -1 WITHSCORES

Sample output:

1) "b"
2) "1#0#2"
3) "a"
4) "3#2#4"

EXZREVRANGE

ItemDescription
SyntaxEXZREVRANGE key min max [WITHSCORES]
Time complexityO(log(N)+M), where N indicates the number of elements in the TairZset key and M indicates the number of elements to be returned.
Command descriptionReturns the elements of a TairZset key within the specified score range. Elements are stored by score in descending order, and elements with the same score are sorted in reverse lexicographical order.
Note This command is similar to EXZRANGE except that this command sorts the results in reverse.
Parameter
  • min and max: indicate zero-based indexes, where 0 is the first element and 1 is the next element. Other elements follow the same rule. You can use the two parameters to specify a closed interval.
    Note
    • The indexes can also be negative numbers, which indicate offsets from the end of TairZset elements returned. For example, -1 indicates the last element of a key and -2 indicates the second to last element of a key. Other elements follow the same rule.
    • To query all elements, you can set min to 0 and max to -1.
    • If the min value is greater than the index of the last element of a key or the max value, an empty list is returned.
  • WITHSCORES: returns the scores of elements. The scores are returned in the value1, score1, ..., valueN, scoreN format. Example:
    1) "b"
    2) "1#0#2"
    3) "a"
    4) "3#2#4"
Output

A list of elements within the specified score range. If the WITHSCORES parameter is specified, the scores of elements are returned.

Example

Sample command:

EXZREVRANGE testkey 0 -1 WITHSCORES

Sample output:

1) "a"
2) "3#2#4"
3) "b"
4) "1#0#2"

EXZRANGEBYSCORE

ItemDescription
SyntaxEXZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
Time complexityO(log(N)+M), where N indicates the number of elements in the TairZset key and M indicates the number of elements to be returned.
Note When M is set to a constant, this expression can be regarded as O(log(N)). For example, you can specify that the first 10 elements are always returned by using the LIMIT clause.
Command descriptionReturns the elements of a TairZset key whose scores are greater than or equal to the min value and are less than or equal to the max value. Returned elements are sorted by score in ascending order. Elements with the same score are returned in lexicographical order.
Parameter
  • min: indicates the lowest score. max: indicates the highest score. If multidimensional scores are used, scores of each dimension are separated by a number sign (#).
    Note
    • If you are not sure about the highest and lowest element scores of a key and you want to query elements whose scores are greater than or equal to or are less than or equal to the specified value, set min to negative infinity (-inf) and max to positive infinity (+inf).
    • The default score range is a closed interval. To specify an open interval, add a parenthesis. For example, (1 5 indicates that elements whose scores are greater than 1 and are less than or equal to 5 are returned.
  • WITHSCORES: returns the scores of elements.
  • LIMIT offset count: specifies the number and interval of returned elements. If you set count to a negative integer, all elements that start from the specified offset are returned.
    Note If the offset value is large, the key needs to be traversed to identify offset elements before elements are returned. This increases time complexity.
Output

A list of elements within the specified score range. If the WITHSCORES parameter is specified, the scores of elements are returned.

Example

Sample command:

EXZRANGEBYSCORE testkey 0#0#0 6#6#6 WITHSCORES

Sample output:

1) "b"
2) "1#0#2"
3) "a"
4) "3#2#4"

EXZREVRANGEBYSCORE

ItemDescription
SyntaxEXZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]
Time complexityO(log(N)+M), where N indicates the number of elements in the TairZset key and M indicates the number of elements to be returned.
Note When M is set to a constant, this expression can be regarded as O(log(N)). For example, you can specify that the first 10 elements are always returned by using the LIMIT clause.
Command descriptionReturns the elements of a TairZset key whose scores are greater than or equal to the min value and are less than or equal to the max value. Contrary to the default sorting of TairZset elements, the elements returned by this command are sorted by score in descending order, and elements with the same score are sorted in reverse lexicographical order.
Note This command is similar to EXZRANGEBYSCORE except that this command sorts the results in reverse and places max in front of min.
Parameter
  • min: indicates the lowest score. max: indicates the highest score. If multidimensional scores are used, scores of each dimension are separated by a number sign (#).
    Note
    • If you are not sure about the highest and lowest element scores of a key and you want to query elements whose scores are greater than or equal to or are less than or equal to the specified value, set min to negative infinity (-inf) and max to positive infinity (+inf).
    • The default score range is a closed interval. To specify an open interval, add a parenthesis. For example, (1 5 indicates that elements whose scores are greater than 1 and are less than or equal to 5 are returned.
  • WITHSCORES: returns the scores of elements.
  • LIMIT offset count: specifies the number and interval of returned elements. If you set count to a negative integer, all elements that start from the specified offset are returned.
    Note If the offset value is large, the key needs to be traversed to identify offset elements before elements are returned. This increases time complexity.
Output

A list of elements within the specified score range. If the WITHSCORES parameter is specified, the scores of elements are returned.

Example

Sample command:

EXZREVRANGEBYSCORE testkey 6#6#6 0#0#0 WITHSCORES

Sample output:

1) "a"
2) "3#2#4"
3) "b"
4) "1#0#2"

EXZRANGEBYLEX

ItemDescription
SyntaxEXZRANGEBYLEX key min max [LIMIT offset count]
Time complexityO(log(N)+M), where N indicates the number of elements in the TairZset key and M indicates the number of elements to be returned.
Note When M is set to a constant, this algorithm can be regarded as O(log(N)). For example, you can specify that the first 10 elements are always returned by using the LIMIT offset count parameter.
Command descriptionReturns the elements whose scores are between the min value and the max value when all elements of a key have the same score to ensure that elements are sorted in lexicographical order.
Note
  • If the elements of a key have different scores, the returned elements are unknown.
  • You can use the memcmp() C function to compare two string elements byte-by-byte. Elements are sorted in ascending order based on the preceding comparison. If two strings contain the same substring, the longer string is assigned a greater value than the shorter string.
Parameter
  • min: indicates the minimum string representation value of the element name. max: indicates the maximum string representation value of the element name. You must specify the intervals of characters. The following section shows how to specify intervals:
    • To specify an open interval, add a parenthesis. Example: (a.
    • To specify a closed interval, add a bracket. Example: [a.
    Note + indicates positive infinity, and - indicates negative infinity.
  • LIMIT offset count: specifies the number and interval of returned elements. If you set count to a negative integer, all elements that start from the specified offset are returned.
    Note If the offset value is large, the key needs to be traversed to identify offset elements before elements are returned. This increases time complexity.
OutputA list of elements whose values are within the specified range.
Example

Sample command:

EXZRANGEBYLEX zzz [a [b

Sample output:

1) "aba"
2) "abc"

EXZREVRANGEBYLEX

ItemDescription
SyntaxEXZREVRANGEBYLEX key max min [LIMIT offset count]
Time complexityO(log(N)+M), where N indicates the number of elements in the TairZset key and M indicates the number of elements to be returned.
Note When M is set to a constant, this expression can be regarded as O(log(N)). For example, you can specify that the first 10 elements are always returned by using the LIMIT clause.
Command descriptionReturns the elements whose scores are between the min value and the max value when all elements of a key have the same score to ensure that elements are sorted in lexicographical order.
Note This command is similar to EXZRANGEBYLEX except that this command sorts the results in reverse and places max in front of min.
Parameter
  • min: indicates the minimum string representation value of the element name. max: indicates the maximum string representation value of the element name. You must specify the intervals of characters. The following section shows how to specify intervals:
    • To specify an open interval, add a parenthesis. Example: (a.
    • To specify a closed interval, add a bracket. Example: [a.
    Note + indicates positive infinity, and - indicates negative infinity.
  • LIMIT offset count: specifies the number and interval of returned elements. If you set count to a negative integer, all elements that start from the specified offset are returned.
    Note If the offset value is large, the key needs to be traversed to identify offset elements before elements are returned. This increases time complexity.
OutputA list of elements whose values are within the specified range.
Example

Sample command:

EXZREVRANGEBYLEX zzz [b [a

Sample output:

1) "abc"
2) "aba"

EXZREM

ItemDescription
SyntaxEXZREM key member [member ...]
Time complexityO(M*log(N)), where N indicates the number of elements in the TairZset key and M indicates the number of elements to be removed.
Command descriptionRemoves specified elements from a TairZset key. If the specified element does not exist, the element is ignored.
Note If the key exists but its data structure is not TairZset, an error is returned.
ParameterNone
OutputThe number of elements removed from the key. The elements that do not exist are not included.
Example

Sample command:

EXZREM testkey a

Sample output:

(integer) 1

EXZREMRANGEBYSCORE

ItemDescription
SyntaxEXZREMRANGEBYSCORE key min max
Time complexityO(log(N)+M), where N indicates the number of elements in the TairZset key and M indicates the number of elements to be removed.
Command descriptionRemoves the elements from a TairZset key whose scores are greater than or equal to the min value and are less than or equal to the max value.
Parametermin: indicates the lowest score. max: indicates the highest score. If multidimensional scores are used, scores of each dimension are separated by a number sign (#).
Note
  • If you are not sure about the highest and lowest element scores of a key and you want to remove elements whose scores are greater than or equal to or are less than or equal to the specified value, set min to negative infinity (-inf) and max to positive infinity (+inf).
  • The default score range is a closed interval. To specify an open interval, add a parenthesis. For example, EXZREMRANGEBYSCORE (1 5 indicates that elements whose scores are greater than 1 and are less than or equal to 5 are deleted.
OutputThe number of elements removed.
Example

Sample command:

EXZREMRANGEBYSCORE testkey 3#2#4 6#6#6

Sample output:

(integer) 1

EXZREMRANGEBYRANK

ItemDescription
SyntaxEXZREMRANGEBYRANK key start stop
Time complexityO(log(N)+M), where N indicates the number of elements in the TairZset key and M indicates the number of elements to be removed.
Command descriptionRemoves the elements from a TairZset key whose ranks are within the range of the start value and the stop value.
ParameterBoth start and stop indicate zero-based indexes. 0 indicates the element that has the lowest score. These indexes can be negative numbers. These numbers indicate offsets that start at the element that has the highest score. For example, -1 indicates the element that has the highest score, and -2 indicates element that has the second highest score. Other elements follow the same rule.
OutputThe number of elements removed.
Example

Sample command:

EXZREMRANGEBYRANK testkey 0 1EXZREVRANGEBYSCORE

Sample output:

(integer) 1

EXZREMRANGEBYLEX

ItemDescription
SyntaxEXZREMRANGEBYLEX key min max
Time complexityO(log(N)+M), where N indicates the number of elements in the TairZset key and M indicates the number of elements to be removed.
Command descriptionRemoves the elements whose scores are between the min value and the max value when all elements of a key have the same score to ensure that elements are sorted in lexicographical order.
Note If you use the min and max parameters to specify the same range for the EXZREMRANGEBYLEX and EXZRANGEBYLEX commands, the elements removed by the EXZREMRANGEBYLEX command are the same as those returned by the EXZRANGEBYLEX command.
Parametermin: indicates the minimum string representation value of the element name. max: indicates the maximum string representation value of the element name. You must specify the intervals of characters. The following section shows how to specify intervals:
  • To specify an open interval, add a parenthesis. Example: (a.
  • To specify a closed interval, add a bracket. Example: [a.
OutputThe number of elements removed.
Example

Sample command:

EXZREMRANGEBYLEX [a [b

Sample output:

(integer) 2

EXZCARD

ItemDescription
SyntaxEXZCARD key
Time complexityO(1)
Command descriptionReturns the cardinality of a TairZset key. Cardinality indicates the number of elements in a key.
ParameterNone
OutputThe number of elements in a TairZset key. If the key does not exist, a value of 0 is returned.
Example

Sample command:

EXZCARD testkey

Sample output:

(integer) 2

EXZRANK

ItemDescription
SyntaxEXZRANK key member
Time complexityO(log(N))
Command descriptionReturns the rank of an element in a TairZset key. Ranks are sorted by score in ascending order. Ranks use a zero-based numbering scheme, and the rank of the element that has the lowest score is 0. Rank is also known as index.
ParameterNone
Output
  • If the element exists, the integer rank of the element is returned.
  • If the key or the element in the key does not exist, a value of nil is returned.
Example

Sample command:

EXZRANK testkey b

Sample output:

(integer) 0

EXZREVRANK

ItemDescription
SyntaxEXZREVRANK key member
Time complexityO(log(N))
Command descriptionReturns the rank of an element in a TairZset key. Ranks are sorted by score in descending order. Ranks use a zero-based numbering scheme, and the rank of the element that has the highest score is 0. Rank is also known as index.
Note This command is similar to EXZRANK except that this command sorts the results in reverse.
ParameterNone
Output
  • If the element exists, the integer rank of the element is returned.
  • If the key or the element in the key does not exist, a value of nil is returned.
Example

Sample command:

EXZREVRANK testkey b

Sample output:

(integer) 1

EXZCOUNT

ItemDescription
SyntaxEXZCOUNT key min max
Time complexityO(log(N)), where N indicates the number of elements in the TairZset key.
Note This command uses element ranks to obtain the query range. Therefore, the workloads associated with this command are not proportional to the size of the range.
Command descriptionReturns the number of elements in a TairZset key whose scores are between the min value and the max value.
Parametermin: indicates the lowest score. max: indicates the highest score. If multidimensional scores are used, scores of each dimension are separated by a number sign (#).
Note
  • If you are not sure about the highest and lowest element scores of a key and you want to query elements whose scores are greater than or equal to or are less than or equal to the specified value, set min to negative infinity and max to positive infinity.
  • The default score range is a closed interval. To specify an open interval, add a parenthesis. For example, (1 5 indicates that elements whose scores are greater than 1 and are less than or equal to 5 are returned.
OutputThe integer number of elements within the specified score range.
Example

Sample command:

EXZCOUNT testkey (1#0#2 6#6#6

Sample output:

(integer) 1

EXZLEXCOUNT

ItemDescription
SyntaxEXZLEXCOUNT key min max
Time complexityO(log(N)), where N indicates the number of elements in the TairZset key.
Note This command uses element ranks to obtain the query range. Therefore, the workloads associated with this command are not proportional to the size of the range.
Command descriptionReturns the number of elements whose scores are between the min value and the max value when all elements of a key have the same score to ensure that elements are sorted in lexicographical order.
Note
  • If the elements of a key have different scores, the returned elements are unknown.
  • You can use the memcmp() C function to compare two string elements byte-by-byte. Elements are sorted in ascending order based on the preceding comparison. If two strings contain the same substring, the longer string is assigned a greater value than the shorter string.
Parametermin: indicates the minimum string representation value of the element name. max: indicates the maximum string representation value of the element name. You must specify the intervals of characters. The following section shows how to specify intervals:
  • To specify an open interval, add a parenthesis. Example: (a.
  • To specify a closed interval, add a bracket. Example: [a.
OutputThe integer number of elements within the specified score range.
Example

Sample command:

EXZLEXCOUNT zzz [a [b

Sample output:

(integer) 2

EXZRANKBYSCORE

ItemDescription
SyntaxEXZRANKBYSCORE key score
Time complexityO(log(N))
Command description
Calculates the rank of the specified element score in a TairZset key. Ranks are sorted by score in ascending order. Ranks use a zero-based numbering scheme, and the rank of the element that has the lowest score is 0. Rank is also known as index.
Note If the score does not exist, the estimated rank of the score in the key is returned. By default, if the score already exists, Tair ranks the score in front of the existing score in the key.
ParameterNone
OutputThe rank of the score in the key.
Example

Sample command:

EXZRANKBYSCORE testkey 2#0#2 

Sample output:

(integer) 1

EXZREVRANKBYSCORE

ItemDescription
SyntaxEXZREVRANKBYSCORE key score
Time complexityO(log(N))
Command description
Calculates the rank of the specified element score in a TairZset key. Ranks are sorted by score in descending order. Ranks use a zero-based numbering scheme, and the rank of the element that has the highest score is 0. Rank is also known as index.
Note If the score does not exist, the estimated rank of the score in the key is returned. By default, if the score already exists, Tair ranks the score behind the existing score in the key.
ParameterNone
OutputThe rank of the score in the key.
Example

Sample command:

EXZREVRANKBYSCORE testkey 2#0#2 

Sample output:

(integer) 1