How can I get only the first result of ZRANGEBYSCORE? - redis

Right now I'm trying something like the following command.
ZRANGEBYSCORE myzset myvalue inf
It gets me the all the results greater than myvalue. However, I would like to obtain only the first result. How can I do it?

Read the docs: https://redis.io/commands/zrangebyscore
ZRANGEBYSCORE myzset myvalue +inf LIMIT 0 1
Note: use +inf, not inf

Related

Redis - ZRANGEBYSCORE with key matching a regex

I'm trying to get the value of the best key in a sorted set.
This is my query at the moment:
ZREVRANGEBYSCORE genre1 +inf -inf WITHSCORES LIMIT 0 1
This is an example of an add in my set:
ZADD "genre1|genre2|genre3" 3.25153 "film"
I'd like to use the query in a way like this
ZREVRANGEBYSCORE *genre1* +inf -inf WITHSCORES LIMIT 0 1
to match keys containing "...|genre1|..." and not only keys like "genre1".
Any help will be appreciated
This can be accomplished in two or three steps:
1) Use SCAN or KEYS to find the keys matching your pattern.
SCAN 0 MATCH "*genre1*"
1) "9"
2) 1) "genre1|genre2|genre3"
2) "genre1|genre4"
2) For each key, use TYPE to test if it is a Sorted Set. This is only important if you may have other genre1 keys on the db
TYPE "genre1|genre4"
zset
3) Run your ZREVRANGEBYSCORE <key> +inf -inf WITHSCORES LIMIT 0 1 for each key.
See this answer on how you can SCAN for a given type. You can modify the Lua script to include the ZREVRANGEBYSCORE and get your results atomically on a single call.
Finally, consider reviewing if storing the genre combinations is optimal in your case. You may use a sorted set per genre, and then use ZUNIONSTORE or ZINTERSTORE to get scored combinations.

Redis - ZRANGEBYLEX return empty set

I have a set stored in redis like the following:
127.0.0.1:6379> zrange my_set 0 -1
1) "ABC20180108131627044829:XYZ20180108131627044857"
2) "ABC20180108131627044829:XYZ20180108131627044858"
3) "ABC20180108131627044829:XYZ20180108131627044859"
4) "ABC20180108131627044830:XYZ20180108131627044830"
5) "ABC20180108131627044830:XYZ20180108131627044831"
They were added to the set using
ZADD my_set 0 ABC20180108131627044829:XYZ20180108131627044857
ZADD my_set 0 ABC20180108131627044829:XYZ20180108131627044858
ZADD my_set 0 ABC20180108131627044829:XYZ20180108131627044859
ZADD my_set 0 ABC20180108131627044830:XYZ20180108131627044830
ZADD my_set 0 ABC20180108131627044830:XYZ20180108131627044831
I thought I can use the following to get back all the item contains ABC20180108131627044829, but I'm getting an empty list here.
127.0.0.1:6379> ZRANGEBYLEX my_set - [ABC20180108131627044829
(empty list or set)
Your looking for an autocomplete behavior. Here's a ZRANGEBYLEX query that will give you only elements that starts with a string:
ZRANGEBYLEX my_set [STRING [STRING\xff
And for your example:
ZRANGEBYLEX my_set [ABC20180108131627044829 [ABC20180108131627044829\xff
1) "ABC20180108131627044829:XYZ20180108131627044857"
2) "ABC20180108131627044829:XYZ20180108131627044858"
3) "ABC20180108131627044829:XYZ20180108131627044859"
Note, all scores must be equal.
You can't specify partial values in the limits of the ZRANGEBYLEX command, you have to input an entire string, but you can take advantage of lexicographical rules.
This would work:
ZRANGEBYLEX my_set [ABC20180108131627044829 [B
As you see the beginning of the interval seems a partial keyword, but in fact it's not: for example, ABCD comes after ABC in these rules, and B comes after ABC, so you have to tweak your criteria to adapt to this.
Also, a reminder: ZRANGEBYLEX works only for members which have same sorted set score!
EDIT
ZRANGEBYLEX my_set - (ABC20180108131627044830
should work for your example

How do I get the size of a set on Redis?

For lists I can do the operation:
LLEN KeyName
and it will return the size of a list in Redis. What is the equivalent command for sets? I can't seem to find this in any documentation.
You are looking for the SCARD command:
SCARD key
Returns the set cardinality (number of elements) of the set stored at
Return value
Integer reply: the cardinality (number of elements) of the set, or 0 if key does not exist.
You can view all of the set commands on the documentation webpage.
If it's a sorted set, you can use
ZCOUNT myset -inf +inf
or
ZCARD myset
zCard is short for cardinality (cardinality is the number of elements in a set). It gives you total number of members inside of a "sorted set".
Sometimes you might wanna extract how many members are inside of a range in a sorted set. For that you can use zCount.
ZCOUNT cars 0 50 // inclusive
this will include 0 and 55. 0 <= .... <=50. But if you do not want to include them
ZCOUNT cars (0 (50
if it is regular set
SCARD cars

ordered sets in redis: random output in case of score ties

I have an ordered set in Redis (I am actually using a python client https://github.com/andymccurdy/redis-py), for example:
zadd myset 1 key1
zadd myset 1 key2
zadd myset 1 key3
zadd myset 0 key4
Note that 3 keys have the same score.
Using ZRANGE, i would like to get the top 2 entries (i.e lowest scores). "key4" will always be the first result as it has a lower value, but I would like the second return value to be randomly selected between the ties: key1,key2,key3. ZRANGE actually returns the keys in the order they are indexed: "keys1" is always my second result:
zrange myset 0 -1 WITHSCORES
1) "key4"
2) "0"
3) "key1"
4) "1"
5) "key2"
6) "1"
7) "key3"
8) "1"
any idea?
thanks,
J.
As kindly requested by Linus G Thiel, here are more details about my usecase:
I would like to use zsets to perform a simple ranking system. I have a list of items, for each one a score representing the relevance of the item. For the cold start of my system, most of the scores will be identical (i.e 0), and I would like to randomly select among the items having the same score. Otherwise I will always return the exact same lexicographic ordering, which will introduce a bias in the system.
The solution you propose, using one specific set for each duplicated score value will work. I will give it a try.
Thanks,

Get all members in Sorted Set

I have a Sorted set and want to get all members of set. How to identify a max/min score for command :
zrange key min max
?
You're in luck, as zrange does not take scores, but indices. 0 is the first index, and -1 will be interpreted as the last index:
zrange key 0 -1
To get a range by score, you would call zrangebyscore instead -- where -inf and +inf can be used to denote negative and positive infinity, respectively, as Didier Spezia notes in his comment:
zrangebyscore key -inf +inf
Starting with Redis 6.2.0,
To get all the keys and its value together in a single query using the below,
zrange <KEY> 0 -1 WITHSCORES
The optional WITHSCORES argument supplements the command's reply with the scores of elements returned. The returned list contains value1,score1,...,valueN,scoreN instead of value1,...,valueN. Client libraries can return a more appropriate data type (suggestion: an array with (value, score) arrays/tuples).
In newer versions of redis (>= v6.2.0), if you want to get all members of a sorted set between two scores, you should use:
ZRANGE key min max BYSCORE
Adding the BYSCORE option makes redis treat the min & max arguments as scores rather than indices.
(As of this writing, ZRANGEBYSCORE still works, but is considered deprecated.)