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
Redis do not support complex searches directly. So I suggest split all searches to several simple searches. The
Item
model also should be split.Item
object:Item
instance should have a unique key.Item
instance to a string. eg: JSON String.Item
instance in redis with the unique key and encoded string value.ZADD
, save to sorted set with key:PRICE_RANGE
score:Item price value
member:Item unique key
.ZRANGEBYSCORE
to get members with price range.Hope to help you.
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 tableKeyword
.On the
Keyword
table, you can put a constraint and make it a set.The
Keyword
should have a foreign key against theItem
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.