Shikhil Gupta
Shikhil Gupta

Reputation: 41

Redis Support on Secondary index

Does redis provide support for secondary index on string data type?

I have installed redis server to check that, but could not find out clearly to do this.

I would like to know how i can store the data in redis along with seconday key.

Example let say i want to store thevehicle id:- registration no, in my case this will be primary key, vehicle color:- Red, this could be secondary key and payload related to vehicle info is my value. Now, I also want to find all payload whose color is red in redis.

Same as like i can do in MySql like

Select * from table where secondarycolumn="red"

Also, I would like to know while setting value in redis using primary key, how can i set the secondary index value for it.

Upvotes: 4

Views: 6232

Answers (3)

Siscia
Siscia

Reputation: 1491

zeeSQL is a novel Redis module that introduces exactly secondary indexing.

This allows the developer to set up once the fields they want to index and just let the module do all the book-keeping around maintaining the index.

In your particular case, the very first step would be to create a new zeeSQL database.

> ZEESQL.CREATE_DB Vehicles
OK

Then you will instruct zeeSQL to keep a secondary index for all the Vehicles and their color.

> ZEESQL.INDEX Vehicles NEW PREFIX vehicles:* TABLE Registrations SCHEMA color STRING
OK

Then, you can insert new values in the Redis Hash with:

> HMSET vehicles:12344 color red
OK
> HMSET vehicles:11334 color blue
OK

The values are stored in both Redis but also in the zeeSQL secondary indexes.

The secondary indexes of zeeSQL is a simple SQLite table, that you can then query, for instance with with ZEESQL.QUERY command.

> ZEESQL.QUERY Vehicles COMMAND "SELECT * FROM Registrations WHERE color = 'red';"

A deeper explanation on how to create Redis secondary index using zeeSQL is also available here.

Upvotes: 0

Guy Korland
Guy Korland

Reputation: 9568

You can easily add support for secondary index in Redis by deploying the RediSearch.

RediSearch supports defining automatic indexing of the hash fields and then to easily query those indexes using a simple query phrase.

e.g

FT.SEARCH myIdx "@name:Joe @age:[70-90]"

Upvotes: 2

MD Ruhul Amin
MD Ruhul Amin

Reputation: 4502

For secondary index you can use Hash data type. eg:

to store:

HSET myindex akash Manuel

To get:

HGET myindex akash

this will return: Manuel

check this link: Redis Hash

Upvotes: 1

Related Questions