Summary
This post will provide some snippets from Redis Query Workshop available on GitHub. That workshop covers parallel examples in CLI, Python, Nodejs, Java, and C#. This post will focus on Nodejs examples.
Basic JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
await client.json.set('ex3:3', '$', {"obj1": {"str1": "val1", "num2": 2}}); | |
await client.json.set('ex3:3', '$.obj1.num2', 3); | |
result = await client.json.get('ex3:3'); | |
console.log(result); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ obj1: { str1: 'val1', num2: 3 } } |
Basic Search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
await client.ft.create('idx1', { | |
'$.id': { | |
type: SchemaFieldTypes.NUMERIC, | |
AS: 'id' | |
}, | |
'$.gender': { | |
type: SchemaFieldTypes.TAG, | |
AS: 'gender' | |
}, | |
'$.season.*': { | |
type: SchemaFieldTypes.TAG, | |
AS: 'season' | |
}, | |
'$.description': { | |
type: SchemaFieldTypes.TEXT, | |
AS: 'description' | |
}, | |
'$.price': { | |
type: SchemaFieldTypes.NUMERIC, | |
AS: 'price' | |
}, | |
'$.city': { | |
type: SchemaFieldTypes.TEXT, | |
AS: 'city' | |
}, | |
'$.coords': { | |
type: SchemaFieldTypes.GEO, | |
AS: 'coords' | |
} | |
}, { ON: 'JSON', PREFIX: 'product:'}); | |
await client.json.set('product:15970', '$', {"id": 15970, "gender": "Men", "season":["Fall", "Winter"], "description": "Turtle Check Men Navy Blue Shirt", "price": 34.95, "city": "Boston", "coords": "-71.057083, 42.361145"}); | |
await client.json.set('product:59263', '$', {"id": 59263, "gender": "Women", "season":["Fall", "Winter", "Spring", "Summer"],"description": "Titan Women Silver Watch", "price": 129.99, "city": "Dallas", "coords": "-96.808891, 32.779167"}); | |
await client.json.set('product:46885', '$', {"id": 46885, "gender": "Boys", "season":["Fall"], "description": "Ben 10 Boys Navy Blue Slippers", "price": 45.99, "city": "Denver", "coords": "-104.991531, 39.742043"}); | |
result = await client.ft.search('idx1', '@price:[40,130]'); | |
console.log(JSON.stringify(result, null, 4)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"total": 2, | |
"documents": [ | |
{ | |
"id": "product:46885", | |
"value": { | |
"id": 46885, | |
"gender": "Boys", | |
"season": [ | |
"Fall" | |
], | |
"description": "Ben 10 Boys Navy Blue Slippers", | |
"price": 45.99, | |
"city": "Denver", | |
"coords": "-104.991531, 39.742043" | |
} | |
}, | |
{ | |
"id": "product:59263", | |
"value": { | |
"id": 59263, | |
"gender": "Women", | |
"season": [ | |
"Fall", | |
"Winter", | |
"Spring", | |
"Summer" | |
], | |
"description": "Titan Women Silver Watch", | |
"price": 129.99, | |
"city": "Dallas", | |
"coords": "-96.808891, 32.779167" | |
} | |
} | |
] | |
} |
Advanced JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
await client.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 | |
}]}); | |
result = await client.json.get('warehouse:1', { path: '$.inventory[?(@.description=="Turtle Check Men Navy Blue Shirt")]' }); | |
console.log(JSON.stringify(result, null, 4)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"id": 15970, | |
"gender": "Men", | |
"season": [ | |
"Fall", | |
"Winter" | |
], | |
"description": "Turtle Check Men Navy Blue Shirt", | |
"price": 34.95 | |
} | |
] |
Advanced Search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
await client.ft.create('wh_idx', { | |
'$.city': { | |
type: SchemaFieldTypes.TEXT, | |
AS: 'city' | |
} | |
}, { ON: 'JSON', PREFIX: 'warehouse:'}); | |
await client.json.set('warehouse:1', '$', { | |
"city": "Boston", | |
"location": "-71.057083, 42.361145", | |
"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 | |
} | |
]}); | |
await client.json.set('warehouse:2', '$', { | |
"city": "Dallas", | |
"location": "-96.808891, 32.779167", | |
"inventory": [ | |
{ | |
"id": 51919, | |
"gender": "Women", | |
"season":["Summer"], | |
"description": "Nyk Black Horado Handbag", | |
"price": 52.49 | |
}, | |
{ | |
"id": 4602, | |
"gender": "Unisex", | |
"season": ["Fall", "Winter"], | |
"description": "Wildcraft Red Trailblazer Backpack", | |
"price": 50.99 | |
}, | |
{ | |
"id": 37561, | |
"gender": "Girls", | |
"season": ["Spring", "Summer"], | |
"description": "Madagascar3 Infant Pink Snapsuit Romper", | |
"price": 23.95 | |
} | |
]}); | |
result = await client.ft.search('wh_idx', '@city:(Dallas)', { | |
RETURN: ['$.inventory[?(@.gender=="Women" || @.gender=="Girls")]'], | |
DIALECT: 3 | |
}); | |
console.log(JSON.stringify(result, null, 4)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"total": 1, | |
"documents": [ | |
{ | |
"id": "warehouse:2", | |
"value": { | |
"$.inventory[?(@.gender==\"Women\" || @.gender==\"Girls\")]": "[{\"id\":51919,\"gender\":\"Women\",\"season\":[\"Summer\"],\"description\":\"Nyk Black Horado Handbag\",\"price\":52.49},{\"id\":37561,\"gender\":\"Girls\",\"season\":[\"Spring\",\"Summer\"],\"description\":\"Madagascar3 Infant Pink Snapsuit Romper\",\"price\":23.95}]" | |
} | |
} | |
] | |
} |
Source
Copyright ©1993-2024 Joey E Whelan, All rights reserved.