zsquare
zsquare

Reputation: 10146

Redis find key based on set value

Im using redis to store tags for certain entities. Examples of the data:

+-------------+--------------------------------+
|     key     |             value              |
+-------------+--------------------------------+
| book:1:tags |     [python, ruby, rails]      |
+-------------+--------------------------------+
| book:2:tags |     [fiction, fantasy]         |
+-------------+--------------------------------+
| book:3:tags |     [fiction, adventure]       |
+-------------+--------------------------------+

How do I find all books with a particular tag, ie all books tagged with fiction?

Upvotes: 3

Views: 1237

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230286

You have to maintain reverse indexes yourself. Along with those keys that you posted, you should create reverse references.

tag:python => [1]
tag:ruby => [1]
tag:rails => [1]

tag:fiction => [2, 3]
tag:fantasy => [2]
tag:adventure => [3]

Then it's trivial to do what you want. But maybe you should consider using another tool for the job. For example, MongoDB can efficiently index and query arrays.

Upvotes: 5

Related Questions