Search code examples
redisspring-data-redisetcdredis-cluster

Recursive search in REDIS


In ETCD, Say following are the key-value pairs:

company/google/employees/employee1 employeeID1

company/google/employees/employee2 employeeID2

company/amazon/employees/employee1 employeeID1

company/amazon/employees/employee2 employeeID2

When I do a Fetch on "company/amazon/", I get the employee IDs of all employees in amazon (and the same applies for "company/google/") and a fetch on "company/" fetches all the employee IDs. Is this supported in REDIS ? If not, how can we do the same ?

Thanks in advance.


Solution

  • One way to do this is by using a hash and HSCAN to pattern-match the key.

    One hash with all your data encapsulates the scan to only the relevant keys, and not all keys on the database.

    And HSCAN has the advantage it returns the key and the value. KEYS and SCAN only return the keys and then you need to fetch values.

    > HSET myData company/google/employees/employee1 employeeID1
    (integer) 1
    > HSET myData company/google/employees/employee2 employeeID2
    (integer) 1
    > HSET myData company/amazon/employees/employee1 employeeID1
    (integer) 1
    > HSET myData company/amazon/employees/employee2 employeeID2
    (integer) 1
    > HSCAN myData 0 MATCH company/amazon/* COUNT 100
    1) "0"
    2) 1) "company/amazon/employees/employee1"
       2) "employeeID1"
       3) "company/amazon/employees/employee2"
       4) "employeeID2"
    > HSCAN myData 0 MATCH company/google/* COUNT 100
    1) "0"
    2) 1) "company/google/employees/employee1"
       2) "employeeID1"
       3) "company/google/employees/employee2"
       4) "employeeID2"
    > HSCAN myData 0 MATCH company/* COUNT 100
    1) "0"
    2) 1) "company/google/employees/employee1"
       2) "employeeID1"
       3) "company/google/employees/employee2"
       4) "employeeID2"
       5) "company/amazon/employees/employee1"
       6) "employeeID1"
       7) "company/amazon/employees/employee2"
       8) "employeeID2"