skip to Main Content

Is there any way to get values without key in redis?

My data is as below. Assume that I inserted the three records using hset.

key     field     value
a-7-1   param1     1
a-7-2   param2     2
a-7-3   param1     3

As you can see, there are three keys and I want to extract rows containing param1.
So, when I run a redis-cli command, I want to get records as below.
Note that I want to enter single redis command, not for the script containing multiple shell and other commands.

key     field     value
a-7-1   param1     1
a-7-3   param1     3

I am hard to find any redis-cli that enables those functionality.

I would appreciate it if you could any guidance to me.

2

Answers


  1. you can use hscan to fetch data with patterns

    In your case, you can

    HSCAN redis-key 0 MATCH param1 100
    
    Login or Signup to reply.
  2. This sounds like a problem for RediSearch. You can create an index over your Hashes and then query it. Like this:

    127.0.0.1:6379> HSET a-7-1 param1 1
    127.0.0.1:6379> HSET a-7-2 param2 2
    127.0.0.1:6379> HSET a-7-3 param1 3
    
    127.0.0.1:6379> FT.CREATE a-index ON HASH PREFIX 1 a- SCHEMA param1 NUMERIC param2 NUMERIC
    
    127.0.0.1:6379> FT.SEARCH a-index "@param1:[-inf +inf]" RETURN 1 param1
    1) (integer) 2
    2) "a-7-1"
    3) 1) "param1"
       2) "1"
    4) "a-7-3"
    5) 1) "param1"
       2) "3"
    

    You’ll need a version of Redis with RediSearch installed—I used Redis Stack via Docker—for this sort of stuff.

    RediSearch is a big topic—bigger than I can reasonably post here—so you’ll need to do some digging on the details. But it’s the sort of thing that lets you query Redis and will hopefully be a good starting point for you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search