Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis finding hashes by field values

Tags:

redis

nosql

When using Redis in order to create a "record" you can create a hash with several fields. For example:

HMSET myhash field1 "Hello" field2 "World"
HMSET myhash2 field1 "Goodbye" field2 "World"

You can retrieve this by knowing the key values, however what I want to know is there any way to retrieve all hashes that have "World" in field2?

like image 915
Chris Maness Avatar asked Jun 26 '12 14:06

Chris Maness


People also ask

What does HSET do in Redis?

Redis HSET command is used to set field in the hash stored at the key to value. If the key does not exist, a new key holding a hash is created. If the field already exists in the hash, it is overwritten.

What is Hashkey in Redis?

A Redis hash is a data type that represents a mapping between a string field and a string value. Hashes can hold many field-value pairs and are designed to not take up much space, making them ideal for representing data objects.

Does Redis use hash table?

It is implemented as a Hash table and handles collisions by Chaining. A Redis Hash can store up to 4 billion key value pairs. If the value is Integer, Redis hash allows you to atomically increment or decrement the value.

Is Redis hash ordered?

NO, Redis hash doesn't maintain the order. Since it's a hash, it's unordered.


1 Answers

There are no indexes in redis, and it doesn't implement SQL. It's a key-value store. You provide a key, it gets you a value.

That said, you can implement this by maintaining secondary indexes yourself. For example:

create a record and an index entry

HMSET myhash field1 Hello field2 World
SADD field2_world myhash

update a record, delete old index entry, create new one

SREM field2_world myhash
HMSET myhash field2 Mundo
SADD field2_mundo myhash

find all records which have "World" in field2

SMEMBERS field2_world

I hope you get the idea.

like image 143
Sergio Tulentsev Avatar answered Nov 15 '22 15:11

Sergio Tulentsev