Composite indexing using Redis in a hierarchical data model

Viewed 2331

I have a data model like this:
Fields:

  1. counter number (e.g. 00888, 00777, 00123 etc)
  2. counter code (e.g. XA, XD, ZA, SI etc)
  3. start date (e.g. 2017-12-31 ...)
  4. end date (e.g. 2017-12-31 ...)
  5. Other counter date (e.g. xxxxx)

Current Datastructure organization is like this (root and multiple child format):

counter_num + counter_code
       ---> start_date + end_date --> xxxxxxxx
       ---> start_date + end_date --> xxxxxxxx
       ---> start_date + end_date --> xxxxxxxx

Example:

00888 + XA
       ---> Jan 10 + Jan 20 --> xxxxxxxx
       ---> Jan 21 + Jan 31 --> xxxxxxxx
       ---> Feb 01 + Dec 31 --> xxxxxxxx

00888 + ZI
       ---> Jan 09 + Feb 24 --> xxxxxxxx
       ---> Feb 25 + Dec 31 --> xxxxxxxx

00777 + XA
       ---> Jan 09 + Feb 24 --> xxxxxxxx
       ---> Feb 25 + Dec 31 --> xxxxxxxx

Today the retrieval happens in 2 ways:

//Fetch unique counter data using all the composite keys
counter_number + counter_code + date (start_date <= date <= end_date)

//Fetch all the counter codes and corresponding data matching the below conditions
counter_number + date (start_date <= date <= end_date)

What's the best way to model this in redis as I need to cache some of the frequently hit data. I feel sorted sets should do this somehow, but unable to model it.

UPDATE:

Just to remove the confusion, the ask here is not for an SQL "BETWEEN" like query. 'Coz I don't know what the start_date and end_date values are. Think they are just column names.

What I don't want is

SELECT * FROM redis_db  
WHERE counter_num AND 
date_value BETWEEN start_date AND end_date

What I want is

SELECT * FROM redis_db
WHERE counter_num AND
start_date <= specifc_date AND end_date >= specific_date

NOTE: The requirement is pretty much close to 2D indexing of what is proposed in Redis multi-dimensional indexing document

https://redis.io/topics/indexes#multi-dimensional-indexes

I understood the concept but unable to digest the implementation detail that is given.

2 Answers
Related