Redis : Sorted sets for specific SMEMBERS - redis

im new to Redis and try to display a sorted set for specific values that are part of a sadd. Is there a way to do this in Redis?
SADD friends "Sarah"
SADD friends "Lisa"
SADD friends "Tim"
ZADD users 200 "Sarah"
ZADD users 133 "Lisa"
ZADD users 410 "Peter"
ZADD users 280 "Tim"
ZADD users 205 "Hans"
I want to display a sorted set with score only with friends.
1) "Tim"
2) "280"
3) "Sarah"
4) "200"
5) "Lisa"
6) "133"

You can use ZINTERSTORE as it can work with regular SETs as well. Note that it will use 1 for the elements scores, but you can remove it with a weight 0:
ZINTERSTORE aux_zset 2 friends users weights 0 1
The result ZSET has your answer:
127.0.0.1:6379> zrange aux_zset 0 -1 withscores
1) "Lisa"
2) "133"
3) "Sarah"
4) "200"
5) "Tim"
6) "280"

Related

Redis sort by score and date

I'm trying to strategies how to calculate position/rank based on score and date
I have a two pieces of data with the same score shown in the image below.
If i run the command
$ZADD usersScores 11 userId6
It will add it above userId5 which is not something I want
Is it possible to add it below userId1 maybe based on date?
This is my goal essentially where i would like to show the position based on the amount and the oldest would be ontop via date
Thanks!
Sorted Set ordering
Sorted sets are ordered by their scores first, and then lexigraphically by their members if the scores are equivalent. So in your case if you added the following keys:
127.0.0.1:6379> ZADD myset 11 user1
(integer) 1
127.0.0.1:6379> ZADD myset 11 user5
(integer) 1
127.0.0.1:6379> ZADD myset 24 user3
(integer) 1
127.0.0.1:6379> zadd myset 54 user2
(integer) 1
127.0.0.1:6379> zadd myset 11 user6
(integer) 1
and then did a ZRANGE, you'd get them in ascending order with the members ordered in alphabetical order:
127.0.0.1:6379> zrange myset 0 -1 WITHSCORES
1) "user1"
2) "11"
3) "user5"
4) "11"
5) "user6"
6) "11"
7) "user3"
8) "24"
9) "user2"
10) "54"
What it looks like, is your GUI is using, or are just passing in the rev argument to ZRANGE under the hood, which orders everything in reverse (scores descending, reverse alphabetical)
127.0.0.1:6379> ZRANGE myset 0 -1 WITHSCORES REV
1) "user2"
2) "54"
3) "user3"
4) "24"
5) "user6"
6) "11"
7) "user5"
8) "11"
9) "user1"
10) "11"
Second sort-by
There are a couple of ways you could handle the second sort by with a sorted set, let's assume you wanted to do this with a 32 bit unix timestamp
You could prepend the second sortby field to the member name so if user6's score came in at 1659579650, you'd just make the member name be 1659579650:user1, then when it orders it, it would do so lexigraphically
You could combine the score with the second, since 11 is the score, we could shift 11 32 bits to the left and add/or the timestamp to it, which comes out to 48,904,219,906, those will then order correctly, and then when you want to find the score, all you need to do is right-shift the score by 32 bits.

Merge Redis set members values using a pattern

I have a simple zset named pets:
redis-cli zadd pets 1 cat1
redis-cli zadd pets 1 cat2
redis-cli zadd pets 1 cat3
redis-cli zadd pets 1 rat1
redis-cli zadd pets 1 rat2
The value is always 1. Now the set state is redis-cli zrange pets 0 -1 withscores:
1) "cat1"
2) "1"
3) "cat2"
4) "1"
5) "cat3"
6) "1"
7) "rat2"
8) "1"
9) "rat1"
10) "1"
Is there a way to merge set members based on a pattern? I want to sum all the members with the name starting with a prefix, in my case the pet type, so as to end up with the following:
1) "cat"
2) "3"
3) "rat"
4) "2"
I have tried zinterstore and zunionstore but they enforce having the same name for members of the sets.
Nope, there isn't a built-in Redis gimmick for that, but you could majik it with a Lua script (see the EVAL command).
That said, if what you need is count of pet by prefix, you should consider an alternative data model that can serve that.

In Redis, is it possible to predicate the inclusion of a member in the result of ZRANGE based on its existence in a different ZSET?

I'm tracking members in multiple Sorted Sets in Redis as a way to do multi-column indexing on the members. As an example, let's say I have two Sorted Sets, lastseen (which is epoch time) and points, and I store usernames as members in these Sorted Sets.
I'm wanting to use ZREVRANGEBYSCORE against lastseen to get a list of users who were recently seen, but I only want a user to be included in the results if its also present in the points Sorted Set.
I looked at using ZINTERSTORE to help me do this, but the available AGGREGATE options are not conducive to my scenario. If one of the AGGREGATE options was to use the score from a specific set provided to the ZINTERSTORE command, then it would work, but that's not an option.
---EDIT---
Example:
redis> ZADD lastseen 12345 "foo"
redis> ZADD lastseen 12346 "bar"
redis> ZADD lastseen 12347 "sucka"
redis> ZADD points 5 "foo"
redis> ZADD points 9 "bar"
Now, if I run:
redis> ZREVRANGEBYSCORE lastseen +inf -inf
1) "sucka"
2) "bar"
3) "foo"
What I'm looking for is a way to only get "foo" and "bar" returned by the call to ZREVRANGEBYSCORE for lastseen since "sucka" isn't resent in the points Sorted Set.
I can do:
redis> ZINTERSTORE out 2 lastseen points
redis> ZRANGE out 0 -1 WITHSCORES
1) "foo"
2) "12350"
3) "bar"
4) "12355"
As you can see, the ZINTERSTORE does cut out the "sucka" member, but the scores for the other two members get aggregated using SUM. There's two other aggregation options (MIN and MAX) available for use with ZINTERSTORE. In this case, using MAX would give me what I'm looking for, but that wouldn't be the case if a user's points were greater than the epoch value for the last time they were seen.
You can get what you're asking by setting the WEIGHT of points to 0 and of lastseen to 1 when running ZINTERSTORE so that only the weight of lastseen is considered:
redis> ZINTERSTORE out 2 lastseen points WEIGHTS 1 0
(integer) 2
Then, depending on what ordering you're going for, you can either:
redis> ZRANGE out 0 -1 WITHSCORES
1) "foo"
2) "12345"
3) "bar"
4) "12346"
or:
redis> ZREVRANGE out 0 -1 WITHSCORES
1) "bar"
2) "12346"
3) "foo"
4) "12345"

Redis zrange and zrangebyscore

Are these 2 Redis commands different, except for the second having optional LIMIT argument?
http://redis.io/commands/zrange
http://redis.io/commands/zrangebyscore
They are different:
ZRANGE key start stop ...: start and stop are zero-based indexes (i.e they correspond to a position of an element within the sorted set),
ZRANGEBYSCORE key min max ...: min and max refer to scores (i.e they are used to specify a score range).
So, the first one operates by indexes while the second one (as its name implies) operates by scores. Thus they are used for different purposes.
say, the sorted set is:
value score
tom 0
bob 1
alice 100
lucy 102
when you use zrangebyscores, and the max score is 2, the min score is 0, then, you will get tom and bob;
when you use zrange, and the start is 0, the stop is 2, then you will get tom, bob and alice.
127.0.0.1:6379> zadd example 0 tom 1 bob 100 alice 102 lucy
127.0.0.1:6379> zrange example 0 2 WITHSCORES
1) "tom"
2) "0"
3) "bob"
4) "1"
5) "alice"
6) "100"
127.0.0.1:6379> zrangebyscore example 0 2 WITHSCORES
1) "tom"
2) "0"
3) "bob"
4) "1"
this is the key difference.

Redis: Is it possible sort ordered set result with same score by other key value

ZADD myset 1 ad1
ZADD myset 1 ad2
SET order:ad1 1
SET order:ad2 2
How to sort firstly by ordered set score then the order value?
If you want to store a result (not altering the way data are stored), then you can use the SORT command. It can be applied to zset.
For instance:
> zadd myset 1 albert
> zadd myset 1 joseph
> zadd myset 4 bertrand
> zadd myset 2 casimir
> zadd myset 3 alfred
You can sort by value:
> sort myset alpha
1) "albert"
2) "alfred"
3) "bertrand"
4) "casimir"
5) "joseph"
You can sort by score AND value:
> zrange myset 0 -1
1) "albert"
2) "joseph"
3) "casimir"
4) "alfred"
5) "bertrand"
Now let's add a new property to these objects:
> set order:albert 5
> set order:alfred 3
> set order:casimir 1
> set order:joseph 4
> set order:bertrand 2
You can sort by the new order property:
> sort myset by order:*
1) "casimir"
2) "bertrand"
3) "alfred"
4) "joseph"
5) "albert"
Now if you need to sort by score AND this order property, then you have no other choice than materialize the order property in the zset OR materialize the score in the order property.
For instance, adding the order property in the zset, we get:
> del myset
> zadd myset 1 5:albert
> zadd myset 1 4:joseph
> zadd myset 4 2:bertrand
> zadd myset 2 1:casimir
> zadd myset 3 3:alfred
> zrange myset 0 -1
1) "4:joseph"
2) "5:albert"
3) "1:casimir"
4) "3:alfred"
5) "2:bertrand"
Beware: in that case, the property field is sorted according to the lexicographic order, so the value may have to be normalized if you need numerical order and multiple digits. There are various tricks to do this. For instance, instead of storing "1", "12" or "123", you can store "11", "212" or "3123", the first digit representing the number of digits of your number.
If both the score and the order property can change, this is not very convenient and you will be better served by sorting on client side. This can be achieved in two roundtrips by retrieving the zset with scores on one side, and the corresponding order values on the other side.
> zrange myset 0 -1 withscores
> sort myset by order:* get order:* get #
Up to the client to join the two results (using some kind of dictionary or associative array), and sort to get the final result.