Compound queries with Redis

For training purposes, I am trying to write a simple structured document repository in Redis. In my sample application, I index millions of documents that look something like this:

<book id="1234">
    <title>Quick Brown Fox</title>
    <year>1999</year>
    <isbn>309815</isbn>
    <author>Fred</author>
</book>

I am writing a small query language that allows me to say YEAR = 1999 AND TITLE="Quick Brown Fox"(again, just for my training, I don’t care that I invent the wheel!), And this should return the documents matching identifier ( 1234in this case). Expressions ANDand ORcan be arbitrarily nested.

For each document, I generate keys as follows

BOOK_TITLE.QUICK_BROWN_FOX = 1234
BOOK_YEAR.1999 = 1234

I use SADD to flip these documents in a series of sets in the form KEYNAME.VALUE = { REFS }.

, . , YEAR=1999, SMEMBERS, . , AND OR.

, :

(TITLE=Dental Surgery OR TITLE=DIY Appendectomy)
    AND
(YEAR = 1999 AND AUTHOR = FOO)

Redis .

-- Stage one generates the intermediate results and returns RANDOM_GENERATED_KEY3
SUNIONSTORE RANDOMLY_GENERATED_KEY1 BOOK_TITLE.DENTAL_SURGERY BOOK_TITLE.DIY_APPENDECTOMY
SINTERSTORE RANDOMLY_GENERATED_KEY2 BOOK_YEAR.1999 BOOK_YEAR.1998
SINTERSTORE RANDOMLY_GENERATED_KEY3 RANDOMLY_GENERATED_KEY1 RANDOMLY_GENERATED_KEY2

-- Retrieving the top level results just requires the last key generated
SMEMBERS RANDOMLY_GENERATED_KEY3

AND, SINTERSTORE ( OR SUNIONSTORE). ( TTL, Redis up ). , SMEMBERS. , , , , Redis, .

, Redis ?

+4
1

. , , .

  • ( ) . , , , , .
  • . , , .
+2

All Articles