ZREMRANGEBYRANK difference between ZREMRANGEBYSCORE - redis

What's the difference between ZREMRANGEBYRANK and ZREMRANGEBYSCORE ?
Explain it me regarding to following query:
127.0.0.1:6379> ZRANGEBYSCORE my 1 10 WITHSCORES
1) "b"
2) "1"
3) "a"
4) "4"

Let's Set your ZSet first :
ZADD myzset 1 "one"
ZADD myzset 2 "two"
ZADD myzset 3 "three"
ZADD myzset 4 "four"
ZADD myzset 5 "five"
ZADD myzset 6 "six"
ZADD myzset 7 "seven"
ZADD myzset 8 "eight"
ZADD myzset 9 "nine"
ZADD myzset 10 "ten"
Let's See What you are selecting using below command :
127.0.0.1:6379> ZRANGE myzset 0 10 WITHSCORES
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
7) "four"
8) "4"
9) "five"
10) "5"
11) "six"
12) "6"
13) "seven"
14) "7"
15) "eight"
16) "8"
17) "nine"
18) "9"
19) "ten"
20) "10"
So It will Select All Elements.
Output of ZREMRANGEBYSCORE :
127.0.0.1:6379> ZREMRANGEBYSCORE myzset 5 8
(integer) 4
127.0.0.1:6379>
127.0.0.1:6379> ZRANGE myzset 0 10 WITHSCORES
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
7) "four"
8) "4"
9) "nine"
10) "9"
11) "ten"
12) "10"
So Elements Having Minimum Score 5 to Maximum Score 8 will be removed. Here Removed Element with scores : 5,6,7,8
Output of ZREMRANGEBYRANK :
127.0.0.1:6379> ZREMRANGEBYRANK myzset 5 8
(integer) 4
127.0.0.1:6379>
127.0.0.1:6379> ZRANGE myzset 0 10 WITHSCORES
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
7) "four"
8) "4"
9) "five"
10) "5"
11) "ten"
12) "10"
So Elements Having Minimum Index 5 to Maximum Index 8 will be removed. Here Removed Elements : 6,7,8,9, whose index were 5,6,7,8
As Per Redis Commands Documentation :
ZREMRANGEBYSCORE key min max
Removes all elements in the sorted set stored at key with a score
between min and max (inclusive).
ZREMRANGEBYRANK key start stop
Removes all elements in the sorted set stored at key with rank between
start and stop. Both start and stop are 0 -based indexes with 0 being
the element with the lowest score. These indexes can be negative
numbers, where they indicate offsets starting at the element with the
highest score. For example: -1 is the element with the highest score,
-2 the element with the second highest score and so forth.

Related

How to write the below given shorted set in Redis

How to write the below in Redis
"Create a sorted set myset1 with the following values ( 1, 'a', 2, 'b', 3 , 'c').Create a sorted set myset 2 with the following values (4, 'b', 1, 'c', 0, 'd').Find the intersection of myset1 and myset2 : - Write a command to find the intersection of myset1 and myset2 and store the intersection in out- Write a command to see the resulting intersection output Sorted set "out" : Find the union of myset1 and myset2 - Write a command to take the union of myset1 and myset2 and store the output in unout - Write a command to see the resulting intersection output Sorted set "unout" "
I wrote the code like below and it says 80% correct.
127.0.0.1:6379> zadd myset1 0 1 0 a 0 2 0 b 0 3 0 c (integer) 6 127.0.0.1:6379> zadd myset2 0 4 0 b 0 1 0 c 0 0 0 d (integer) 6 127.0.0.1:6379> zinterstore out 2 myset1 myset2 (integer) 3 127.0.0.1:6379> zrange out 0 -1 1) "1" 2) "b" 3) "c" 127.0.0.1:6379> zunionstore unout 2 myset1 myset2 (integer) 9 127.0.0.1:6379> zrange unout 0 -1 1) "0" 2) "1" 3) "2" 4) "3" 5) "4" 6) "a" 7) "b" 8) "c" 9) "d" 127.0.0.1:6379>
You can use ZINTERSTORE and ZUNIONSTORE commands to computer intersection and union of intersection of sorted sets.
ZINTERSTORE
ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
Computes the intersection of numkeys sorted sets given by the
specified keys, and stores the result in destination. It is mandatory
to provide the number of input keys (numkeys) before passing the input
keys and the other (optional) arguments.
By default, the resulting score of an element is the sum of its scores
in the sorted sets where it exists. Because intersection requires an
element to be a member of every given sorted set, this results in the
score of every element in the resulting sorted set to be equal to the
number of input sorted sets.
ZUNIONSTORE
ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
Computes the union of numkeys sorted sets given by the specified keys,
and stores the result in destination. It is mandatory to provide the
number of input keys (numkeys) before passing the input keys and the
other (optional) arguments.
By default, the resulting score of an element is the sum of its scores
in the sorted sets where it exists.
Edit:
It's not accepting since you're adding score as key as well
Yout ZADD commands should be
ZADD myset1 1, a 2 b 3 c
zadd myset2 4 b 1 c 0 d
Try this
ZADD myset1 1 a 2 b 3 c
ZADD myset2 4 b 1 c 0 d
ZINTERSTORE out 2 myset1 myset2
ZUNIONSTORE unout 2 myset1 myset2

redis how to get Sorted Set Member with Score?

I am new to Redis.
127.0.0.1:6379> zadd myset 1 'one'
(integer) 1
127.0.0.1:6379> zadd myset 2 'two'
(integer) 1
127.0.0.1:6379> zadd myset 3 'three' 4 'four'
(integer) 2
127.0.0.1:6379> zadd myset 10 'ten' 9 'nine'
(integer) 2
I tried ZRANGEBYSCORE, but it shows member only..
127.0.0.1:6379> ZRANGEBYSCORE myset -inf +inf
1) "one"
2) "two"
3) "three"
4) "four"
5) "nine"
6) "ten"
But I want to get Score / Member pairs.
How can I get these pairs ?
As #Ersoy already commented:
you need to add WITHSCORES at the end, ZRANGEBYSCORE myset -inf +inf WITHSCORES
The reply is sent in a format as follows:
1) MEMBER_1
2) SCORE_1
3) MEMBER_2
4) SCORE_1
...
If you want the reply as a list/array of pair, like:
1) MEMBER_1, SCORE_1
2) MEMBER_2, SCORE_2
...
Any sane Redis client[1], even in your preferred language, is likely to convert it as such.
[1] not including redis-cli

How do I delete the last member of a Redis sorted set?

I am using a command line client. Looks like the ZREM doesn't help much. Now I wonder whether that is possible at all.
To remove the element with the highest rank, use zremrangebyrank <key> -1 -1. Here's an example:
127.0.0.1:6379> zadd test 1 one
(integer) 1
127.0.0.1:6379> zadd test 2 two
(integer) 1
127.0.0.1:6379> zadd test 3 three
(integer) 1
127.0.0.1:6379> zrange test 0 -1
1) "one"
2) "two"
3) "three"
127.0.0.1:6379> zremrangebyrank test -1 -1
(integer) 1
127.0.0.1:6379> zrange test 0 -1
1) "one"
2) "two"

Redis lexicographical ordering doesn't work

I'm trying to create a basic autocomplete feature (I created those below manually to test it out first), but somehow I don't get the result I want after adding some keys.
I add every possible version of a word and keep the exact words with * to mark them (For example, if 10 keys are returned and 3 of them have asterisk, they'll be shown as suggestions), so I can query my hash database after that and get hash results.
There are a few duplicate entry attempts, but since it returns integer 0 for them, I presumed that they weren't added for the second time.
I use Redis 3.0.6
127.0.0.1:6379> zadd zset 0 b
(integer) 1
127.0.0.1:6379> zadd zset 0 ba
(integer) 1
127.0.0.1:6379> zadd zset 0 bar
(integer) 1
127.0.0.1:6379> zadd zset 0 bar*
(integer) 1
127.0.0.1:6379> zadd zset 0 f
(integer) 1
127.0.0.1:6379> zadd zset 0 fo
(integer) 1
127.0.0.1:6379> zadd zset 0 foo
(integer) 1
127.0.0.1:6379> zadd zset 0 foo*
(integer) 1
127.0.0.1:6379> zadd zset 0 foob
(integer) 1
127.0.0.1:6379> zadd zset 0 fooba
(integer) 1
127.0.0.1:6379> zadd zset 0 foobar
(integer) 1
127.0.0.1:6379> zadd zset 0 foobar*
(integer) 1
No problem so far.
I want all words that start with fo
127.0.0.1:6379> zrank zset fo
(integer) 5
It gives five, so I increment by one (like shown here if I get it right) and query all keys.
127.0.0.1:6379> zrange zset 6 -1
1) "foo"
2) "foo*"
3) "foob"
4) "fooba"
5) "foobar"
6) "foobar*"
No problem, I get the desired result.
I keep adding keys.
127.0.0.1:6379> zadd zset 0 a
(integer) 1
127.0.0.1:6379> zadd zset 0 b
(integer) 0
127.0.0.1:6379> zadd zset 0 c
(integer) 1
127.0.0.1:6379> zadd zset 0 fi
(integer) 1
127.0.0.1:6379> zadd zset 0 fil
(integer) 1
127.0.0.1:6379> zadd zset 0 filli
(integer) 1
127.0.0.1:6379> zadd zset 0 fillib
(integer) 1
127.0.0.1:6379> zadd zset 0 fillibo
(integer) 1
127.0.0.1:6379> zadd zset 0 filliboy
(integer) 1
127.0.0.1:6379> zadd zset 0 filliboya
(integer) 1
127.0.0.1:6379> zrank zset fo
(integer) 14
I do another search.
127.0.0.1:6379> zrange zset 15 -1
1) "foo"
2) "foo*"
3) "foob"
4) "fooba"
5) "foobar"
6) "foobar*"
Ok again. I keep adding.
127.0.0.1:6379> zadd zset 0 d
(integer) 1
127.0.0.1:6379> zadd zset 0 e
(integer) 1
127.0.0.1:6379> zadd zset 0 x
(integer) 1
127.0.0.1:6379> zadd zset 0 y
(integer) 1
127.0.0.1:6379> zadd zset 0 z
(integer) 1
127.0.0.1:6379> zadd zset 0 filli*
(integer) 1
127.0.0.1:6379> zadd zset 0 filliboya*
(integer) 1
This is the part where things get interesting. I want to get all the words that start with filli, but I can't. Why?
127.0.0.1:6379> zrank zset filli
(integer) 11
127.0.0.1:6379> zrange zset 12 -1
1) "filli*"
2) "fillib"
3) "fillibo"
4) "filliboy"
5) "filliboya"
6) "filliboya*"
7) "fo"
8) "foo"
9) "foo*"
10) "foob"
11) "fooba"
12) "foobar"
13) "foobar*"
14) "x"
15) "y"
16) "z"
127.0.0.1:6379>
The lexicographical ordering is working, but you're asking for the entire range from the member you've retrieved to the end (-1). Since Redis v2.8 you should be using the ZRANGEBYLEX command for that purpose. In your case, it should be as follows:
ZRANGEBYLEX zset [filli [filli\xff

how can I get score from redis sorted set by rank?

I have a sorted set in redis and I want to get score for 3rd element, for example.
redis> ZADD myzset 1 "one"
redis> ZADD myzset 2 "two"
redis> ZADD myzset 3 "three"
redis> ZADD myzset 4 "four"
ZRANGE is just for that. You can choose index with point or range.
ZRANGE myzset 2 2 WITHSCORES