4

I am new to the Redis cache implementation. I want to search value in all the keys. The values may or may not be nested collections of list. What command should I use this to search data? https://github.com/antirez/redis/issues/6802

I am implementing the same in .net core. https://github.com/StackExchange/StackExchange.Redis

enter image description here

Jeevanandan J
  • 144
  • 1
  • 8

1 Answers1

0

If you just want to search inside a hash key as in the screenshot, you can use HSCAN to traverse all the fields of the hash, this returns the value as well. Then test for the value client-side. Or, you can move this logic to a Lua script to do it Redis-server-side.

If you want to search in all the keys, consider the following:

  • You will need to traverse the whole keyspace, key by key, using SCAN.
  • Depending on the type, perform the search inside the key.
  • Sets and sorted sets can be searched with SSCAN and ZSCAN for values, using MATCH option.
  • For all other types, you need to do the search by your own.

Again, you can implement the above in a Lua script for a more efficient implementation. This answer can get you started.

LeoMurillo
  • 6,048
  • 1
  • 19
  • 34
  • I have used the following command to get all the values. HSCAN users:hash 0 MATCH user:*. But in my case I will have nested collections. See the image, I would have countries collection inside the object. How would I traverse to that and find the value – Jeeva J Jan 22 '20 at 15:58
  • You can look at installing [RedisJSON](https://oss.redislabs.com/redisjson/), or do it in Lua, using [Lua CJSON](https://www.kyne.com.au/~mark/software/lua-cjson-manual.html#_api_functions) to look inside the value. The CJSON library is already available in redis, see https://stackoverflow.com/questions/59822193/what-version-release-of-lua-and-external-libraries-are-used-in-redis – LeoMurillo Jan 22 '20 at 16:50