2

I am working on Redis using Spring Data Redis(SDR). I want to query my hash data based on the value(not field name).

This is my hash data -

 Address address1 = Address.builder()
 .city("New York")
 .country("USA")
 .build();
 
 Address address2 = Address.builder()
 .city("New Jersy")
 .country("USA")
 .build();
 
 Address address3 = Address.builder()
 .city("Ohio")
 .country("USA")
 .build();
 Set<Address> addresses = Sets.newHashSet(address1, address2, address3);
 
 Person person = Person.builder()
 .firstname("John")
 .lastname("Leo")
 .address(addresses)
 .build();
 redisTemplate.opsForHash().putAll("employee", jacksonMapper.toHash(person));

Now we have APIs to query by map key, field name like below

Map<String, Object> entries = redisTemplate.opsForHash().entries("employee"); //query by map key
Object object = redisTemplate.opsForHash().get("employee", "address[0].city"); //query by field name

But I want to query by field value like -

Find all cities starting with 'N'

Find all countries matching the pattern 'U*'

Any help in this regard is appreciated.

asked Jan 3, 2020 at 17:40
1
  • @shreyas-k did you consider using RediSearch? Commented Jan 4, 2020 at 20:31

1 Answer 1

2

There is no Redis command to scan by values on a hash. HSCAN scans the field-names, not the values.

HSCAN does return the field and the value, so you could use it to return all the cities or countries, using a pattern like "address[*].city", and then do the filtering locally.

HSCAN is opsForHash().scan(H key, ScanOptions options).

If filtering locally is not acceptable, you can:

  • Use a Lua script to filter the values Redis-server-side.
  • Create a secondary structure, a Redis set (sets store unique values), like employee:cities where you store (again) these queryable values with SSCAN.
  • Install the RediSearch module on your Redis and move your data to use this, as documents.
  • Create your own Redis module.
answered Jan 3, 2020 at 21:04
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.