George Olah
George Olah

Reputation: 607

Search in Redis hash key

I have in Redis several similar keys like:

I would like to retrieve all hashes that have the key like '/article/200%'.

Is that possible in Redis? And if yes, how?

Upvotes: 0

Views: 807

Answers (1)

Karthikeyan Gopall
Karthikeyan Gopall

Reputation: 5709

If you mean values or entries inside the hash. Then it is not possible.

hset hash /article/200 1
hset hash /article/200?something 2
hset hash somethingelse 3

retrieving entries inside this hash is not possible. You need handle them in you application logic or you have to write a lua script for this.

If you want hashes i.e, keys to retrieve then it is possible.

hset /article/200 value1 1
hset /article/200?something value2 2
hset somethingelse value3 3
keys "/article/200*" will return /article/200 and /article/200?something

you can use scan ( http://redis.io/commands/scan ) or keys ( http://redis.io/commands/keys ) command to achieve the same.

keys "/article/200*" will give you all the keys matching the given pattern.

Keys are usually blocking and not advisable to use in production. So, use scan to achieve you requirement. Write a simple LUA script ( http://redis.io/commands/eval ) to achieve the same as atomic.

Upvotes: 1

Related Questions