Sunday, 12 April 2015

ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]


Syntax
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
   Returns all the elements in the sorted set, where the score is in  between min and max. The elements are ordered from low to highest scores.

127.0.0.1:6379> zadd set1 1 one 2 two 3 three 4 four 5 five 
(integer) 5 
127.0.0.1:6379> zadd set1 1 six 2 seven 3 eight 4 nine 5 ten 
(integer) 5 
127.0.0.1:6379> zadd set1 1 eleven 2 twelve 3 thirteen 4 fourteen 5 fifteen 
(integer) 5 
127.0.0.1:6379> zrangebyscore set1 2 5 
 1) "seven" 
 2) "twelve" 
 3) "two" 
 4) "eight" 
 5) "thirteen" 
 6) "three" 
 7) "four" 
 8) "fourteen" 
 9) "nine" 
10) "fifteen" 
11) "five" 
12) "ten" 
 
The WITHSCORES argument return both the element and its score.

127.0.0.1:6379> zrangebyscore set1 2 5 withscores 
 1) "seven" 
 2) "2" 
 3) "twelve" 
 4) "2" 
 5) "two" 
 6) "2" 
 7) "eight" 
 8) "3" 
 9) "thirteen" 
10) "3" 
11) "three" 
12) "3" 
13) "four" 
14) "4" 
15) "fourteen" 
16) "4" 
17) "nine" 
18) "4" 
19) "fifteen" 
20) "5" 
21) "five" 
22) "5" 
23) "ten" 
24) "5" 


count and offset are used to get subset of results.

if you want the first 5 results, count = 5, offset = 0
if you want the results 10 to 15, count = 5, offset = 10

127.0.0.1:6379> zrangebyscore set1 2 5 limit 0 5 
1) "seven" 
2) "twelve" 
3) "two" 
4) "eight" 
5) "thirteen" 
127.0.0.1:6379> zrangebyscore set1 2 5 limit 5 5 
1) "three" 
2) "four" 
3) "fourteen" 
4) "nine" 
5) "fifteen" 
127.0.0.1:6379> zrangebyscore set1 2 5 limit 10 5 
1) "five" 
2) "ten" 


By default min and max are inclusive (min <= score <= max). If you want exclusive results, you can use '('.

ZRANGEBYSCORE set1 (1 5
Will return all elements with 1 < score <= 5

ZRANGEBYSCORE set1 (5 (10
Will return all the elements with 5 < score < 10


127.0.0.1:6379> zrangebyscore set1 2 5 
 1) "seven" 
 2) "twelve" 
 3) "two" 
 4) "eight" 
 5) "thirteen" 
 6) "three" 
 7) "four" 
 8) "fourteen" 
 9) "nine" 
10) "fifteen" 
11) "five" 
12) "ten" 
127.0.0.1:6379> zrangebyscore set1 (2 5 
1) "eight" 
2) "thirteen" 
3) "three" 
4) "four" 
5) "fourteen" 
6) "nine" 
7) "fifteen" 
8) "five" 
9) "ten" 
127.0.0.1:6379> zrangebyscore set1 (2 (5 
1) "eight" 
2) "thirteen" 
3) "three" 
4) "four" 
5) "fourteen" 
6) "nine" 
127.0.0.1:6379> 


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment