2

I am using jedis to interact with redis in java language.

Problem Background

I am using hash to store user all information like:

hmset user:1 name xxx email yyy age zzz
hmset user:2 name aaa email bbb age ccc

Now in order to get any user data i can perform operation like:

hmget user:1 name email

Problem

My problem is that I want to search a user with name xxx. Now in this case I dont know the actual key. Mean I dont know I have to hit user:1 or user:2.

So how should I handle this case?

Any help would be very appreciative.

Community
  • 1
  • 1
Mr37037
  • 738
  • 2
  • 13
  • 29

1 Answers1

0

Based on the response of How to search in redis for hash keys?, Redis is a key-value store. So if you want to perform a search on a value in your map, either you need to maintain a manual index in Redis or use another database.

If you really want to use Redis, a possible index for your problem to search by name would be:

sadd name:xxx 1
sadd name:aaa 2

Then, you can retrieve the id of the member named xxx with:

SINTER name:xxx

Finally you have all you need to create the key of your map: user:1

Community
  • 1
  • 1
Kuhess
  • 2,571
  • 1
  • 18
  • 17