Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Redis do prefix matching?

Tags:

redis

Lets say I have a set of cities in the world like so:

EUKLOND
EUKMANC
EUKEDIN
EITROME
EITMILA
EITNAPE
EFRPARI
EFRAVIG
EFRBRES

Where the first letter is continent, next two are country and the trailing 4 are an abbreviated city name.

I would like to be able to search this set by passing in "E" which would return all the entries or EIT and retrieve all the entries for Italy or EFRPARI and get just the Paris entry.

Is this something I can do with Redis?

like image 685
jdoig Avatar asked Feb 19 '12 09:02

jdoig


People also ask

What is prefix in Redis?

Due to the 'flat' nature of the Redis storage, the prefix is used as a 'namespace' for grouping the key/values inside a cache (and to avoid collision with other caches or keys inside Redis).

Can we query in Redis?

A NoSQL database, Redis doesn't use structured query language, otherwise known as SQL. Redis instead comes with its own set of commands for managing and accessing data.

How do I count keys in Redis?

The first command you can use to get the total number of keys in a Redis database is the DBSIZE command. This simple command should return the total number of keys in a selected database as an integer value. The above example command shows that there are 203 keys in the database at index 10.


1 Answers

Generally, it's an Auto-Complete scenario.

Salvatore Sanfilippo (@Antirez), Redis's author, wrote a thorough blog post about how to accomplish this.

UPDATE: I just saw another great blog post, that first takes Salvatore's solution and explains it in a clear way, and second offers another solution that is good also for multiple-word phrases.

like image 98
Ofer Zelig Avatar answered Oct 04 '22 19:10

Ofer Zelig