Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to imitate autocomplete search with Redis ZRANGEBYLEX?

I'm trying to make a simple autocomplete system much like the demo here: http://autocomplete.redis.io/ but for some reason I can't get the ZRANGEBYLEX to return the right results. Here's what I'm doing on the Redis CLI:

> zadd autocomplete 0 one 0 two 0 three 0 four 0 five 0 six 0 seven 0 eight 0 nine 0 ten 0 eleven 0 twelve 0 thirteen 0 fourteen 0 fifteen

My set looks good:

> zrangebylex autocomplete - +
1) "eight"
2) "eleven"
3) "fifteen"
4) "five"
5) "four"
6) "fourteen"
7) "nine"
8) "one"
9) "seven"
10) "six"
11) "ten"
12) "thirteen"
13) "three"
14) "twelve"
15) "two"

And if I use ZRANGEBYLEX like this the result makes sense:

zrangebylex autocomplete [e [eight
1) "eight"

But if I want to get all items in the set that start with an "e" I try this (which is very similar to what the autocomplete demo shows) but it doesn't return the right results:

> zrangebylex autocomplete [e [e(0xff)
(empty list or set)

What must one use as params to the ZRANGEBYLEX command in order for it to match any strings beginning with "e" and anything else afterwards?

like image 611
Glen Selle Avatar asked Jun 25 '14 21:06

Glen Selle


1 Answers

This will do the trick:

127.0.0.1:6379> zrangebylex autocomplete [e "[e\xff"
1) "eight"
2) "eleven"

Edit: As noted by Itamar Haber in his comment below, when using a code client, you do not need quotes.

like image 115
Itamar Haber Avatar answered Sep 19 '22 07:09

Itamar Haber