Reputation: 51
I'am looking what functionalities that Redis Enterprise can offer, and came up with following questions. Content in JSON using JSON.Set
JSON.SET warehouse:1 $ '{
"city": "Boston",
"location": "42.361145, -71.057083",
"inventory": [
{
"id": 15970,
"gender": "Men",
"season":["Fall", "Winter"],
"description": "Turtle Check Men Navy Blue Shirt",
"price": 34.95
},
{
"id": 59263,
"gender": "Women",
"season": ["Fall", "Winter", "Spring", "Summer"],
"description": "Titan Women Silver Watch",
"price": 129.99
},
{
"id": 46885,
"gender": "Boys",
"season": ["Fall"],
"description": "Ben 10 Boys Navy Blue Slippers",
"price": 45.99
}
]
}'
How to write JSON.GET warehouse:1 for "season" contains "Winter"
Upvotes: 0
Views: 591
Reputation: 171
If you know which JSON document to look into, JSONPath filters can be used, as suggested by Guy Royse,
For example, to get the id of items with Winter
in season:
JSON.GET warehouse:1 '$.inventory[?(@.season[?(@=="Winter")])].id'
If you do not know which JSON document to look into, RediSearch can be used, as suggested by Simon Prickett, For example:
FT.CREATE idx:inventory ON JSON SCHEMA '$.inventory[*].season[*]' AS season TAG
And then search for documents
FT.SEARCH idx:inventory @season:{Winter}
Or also retrieve the relevant id
FT.SEARCH idx:inventory @season:{Winter} RETURN 1 '$.inventory[?(@.season[?(@=="Winter")])].id'
Upvotes: 1
Reputation: 4332
What you are looking for is a "contains" operation for JSONPath. At the moment, I don't believe this is possible. This isn't a limitation with Redis Enterprise or Redis Stack, this is a limitation of JSONPath itself.
I did some googling and found some bits and bobs with proposed syntax for JSONPath to do this. However, nothing implemented yet.
There might be a clever hack to get around this but I couldn't think of one. I would suggest that you check out the JSONPath docs for Redis, looking specifically at the filtering examples, and that you play around with what you can do with JSONPath with an online evaluator.
Upvotes: 0