How can I implement an autocomplete using redis?
Say for example I have an array ["alfred","joel","jeff","addick"]
. When I type a
I get ["alfred", "addick"]
I hope you get the point. How can I implement this using redis commands efficiently(if possible but I think it is). It would be great if I could get some simple commands I can try out via telnet to mimic this behaviour.
Thanks
P.S: Merry x-mas to all of you :)
I just went through an awesome post which serve the exact problem you are talking about, and more. Check it out
Here's a dead simple algorithm in PHP for alphabetical autocomplete with redis:
Based on the article Auto Complete with Redis by Salvatore, except I hax the need to generate an additional autocomplete dictionary, at the expense of a tiny little bit of performance penalty (a couple of zadds and zrems extra) but in most cases it should perform well. The script assumes phpredis, but it should practically be the same with predis.
Output examples:
Probably unrelated, but if you landed here, you might also be interested in the easy, proper, fast and scalable way to autocomplete UI fields with suggestions:
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters-completion.html
If you're dealing with a large data set, I would suggest considering implementing this as a trie. I have thrown together a small bit of Ruby that would do this:
For example:
Read more on Tries at Wikipedia's entry on Tries.
You will definitely want to optimize your suggest method to not return ALL values, instead only returning the first X values it finds. It would defeat the purpose to iterate the entire data structure.
Here is a port of original antirez's Ruby implementation in Python:
I also found this snippet when reading Simon Willison's impressive Redis tutorial.
Solution: