user1187968
user1187968

Reputation: 8026

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)

Upvotes: 1

Views: 471

Answers (2)

Ofir Luzon
Ofir Luzon

Reputation: 10947

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.

Upvotes: 2

Niloct
Niloct

Reputation: 10015

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

Upvotes: 1

Related Questions