1

I have an array of item like {code: 'item code', desc: 'item desc'} where I have to perform LIKE comparison with them; which is exactly like what LIKE works in SQL.

Is there anyway to do this with redis?

Tuan Anh Tran
  • 6,807
  • 6
  • 37
  • 54
  • I think Redis only has pattern lookup with `KEYS` command. However you can always implement more complex operation using Lua scripting interface that Redis provides. – woozyking Mar 25 '16 at 05:59
  • KEYS command should not be used in production environment. Redis doesn't support wildchar search like SQL does. You need to structure your data differently: http://stackoverflow.com/questions/7604455/how-to-search-in-redis – Panu Oksala Mar 25 '16 at 06:52

2 Answers2

3

There is no like operator for Redis but the author of Redis has a nice article on how to implement autocomplete: http://oldblog.antirez.com/post/autocomplete-with-redis.html

Joseph Simpson
  • 4,054
  • 1
  • 24
  • 28
1

If you use a sorted set with same score for all the items, then you can use ZSCAN command to match with a glob-style pattern.

For example:

redis> ZADD z 0 one 0 two 0 three 0 four
(integer) 4
redis> ZSCAN z 0 match *o*
1) "0"
2) 1) "four"
   2) "0"
   3) "one"
   4) "0"
   5) "two"
   6) "0"
thepirat000
  • 12,362
  • 4
  • 46
  • 72