skip to Main Content

How do you model this kind of "object" in Redis for maximum searchability?

public class Item {
    Double price;
    String geoHash;
    Long startAvailabilty; // timestamp
    Long endAvailabilty; // timestamp
    Set<Keywords> keywords;
    String category;
    String dateCreated; // iso date
    String dateUpdated; // iso date
    Integer likes;
    Boolean isActive;
}

Where it would be possible to query any or all of the values, e.g. price range, timestamp range query from the two timestamp fields, keywords query from within an embedded set and with a Boolean value query. Sorting by ISO date, sorting by the number of likes.

2

Answers


  1. Redis do not support complex searches directly. So I suggest split all searches to several simple searches. The Item model also should be split.

    • save Item object:
    1. each Item instance should have a unique key.
    2. encode Item instance to a string. eg: JSON String.
    3. save Item instance in redis with the unique key and encoded string value.
    • For price range search:
    1. use ZADD, save to sorted set with key:PRICE_RANGE score:Item price value member:Item unique key.
    2. use ZRANGEBYSCORE to get members with price range.
    3. iter the members(unique key of item) to get all items
    4. decode all items
    • For timestamp range query …
    • For …

    Hope to help you.

    Login or Signup to reply.
  2. Redis does not support the kind of access pattern you are looking for.

    However, thanks to Redis module, you can achieve the same goal.

    zeeSQL is a novel Redis module that embed a SQL database into Redis.
    zeeSQL allows to search all your data with simple and familiar SQL queries.

    In your specific use case, I would define two tables, the table Item and the table Keyword.

    On the Keyword table, you can put a constraint and make it a set.
    The Keyword should have a foreign key against the Item table and being unique, on the tuple (ItemID, keyword)

    At this point you can just populate your table, and look for item into them using SQL syntax, while maintaining the performance of Redis, zeeSQL works in-memory.

    Another option with zeeSQL, it is to store your data in Redis as hash elements, and use zeeSQL secondary indexes.

    In this way your data reside in both Redis, for very fast access, and in zeeSQL for searchability.
    On zeeSQL secondary indexes is possible to define SQL indexes to make your queries even faster.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search