skip to Main Content

Key format is customer|project|task|location

I want to search something like this customer|* |task| *

is this possible?

2

Answers


  1. I believe the KEYS and SCAN commands will allow you to find keys that match patterns like that. It supports glob-style patterns like this:

    • h?llo matches hello, hallo and hxllo
    • h*llo matches hllo and heeeello
    • h[ae]llo matches hello and hallo, but not hillo
    • h[^e]llo matches hallo, hbllo, … but not hello
    • h[a-b]llo matches hallo and hbllo

    If you go this route, use SCAN. KEYS blocks the single thread that Redis commands run against for the entire run. SCAN pulls back the data in batches and scales better.

    redis.cloud:6379> SCAN 0 MATCH customer|*|task|* COUNT 100
    1) "876"
    2) 1) "customer|word|task|asdf"
    

    You’ll want to call SCAN multiple times with the returned number until you get a 0 back. Not all calls will return values:

    redis.cloud:6379> SCAN 876 MATCH customer|*|task|* COUNT 100
    1) "1023"
    2) (empty array)
    
    

    However, you might want to consider storing it as a Hash or JSON instead of a String and using Search. You can define an index with the FT.CREATE command and then use the FT.SEARCH command to find what you are looking for.

    redis.cloud:6379> HSET customer:1 customer "Willy Wonka" project "Chocolate Consumption" task "Eat More Chocloate" location "United Kingdon"
    
    redis.cloud:6379> FT.CREATE customer:index PREFIX 1 customer: SCHEMA customer TAG project TAG task TAG location TAG
    
    redis.cloud:6379> FT.SEARCH customer:index "@project:{Choco*} @task:{Eat More Chocolate}"
    

    Hope that helps.

    Login or Signup to reply.
  2. You should definitely not use REDIS this way as performances will be terrible.

    But, you are looking for KEYS command :

    KEYS customer|*|task|*
    

    Not sure if the pipe has to be escaped or not. I believe it doesn’t.

    You probably rather take a look at that then : https://redis.io/docs/interact/search-and-query/indexing/ and index your objects properly for way better performances.

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