How to set Redis to be case insensitive while filtering keys?

Viewed 1092

I am using redis to set and filter key value pair in my application. But it return always case sensitive data while filtering using HSCAN. I need to get case insensitive data from redis. How can I get this?

How to set Redis to be case insensitive?

ex: If I search "foo", it should return the following results,

Foo
foo
FOO
fOO

Kindly provide your inputs on this.

1 Answers

There's no built-in way to do that, however, you can hack it.

Instead of doing: HSET hash foo val, HSET hash Foo val, and HSET hash fOo val, you make these fields has a common prefix, e.g. FOO:

HSET hash FOO:foo val
HSET hash FOO:Foo val
HSET hash FOO:fOo val

Then instead of call HSCAN hash 0 MATCH foo*, you can use HSCAN hash 0 MATCH FOO:* to scan items case-insensitively.

In a word, encode your field with a case-insensitive prefix, e.g. all chars uppercased or lowercased.

Related