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 C# 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
json.Set("ex3:1", "$", new {field1 = "val1"}); | |
json.Set("ex3:1", "$", new {foo = "bar"}); | |
Console.WriteLine(json.Get(key: "ex3:1", | |
indent: "\t", | |
newLine: "\n" | |
)); |
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
{ | |
"foo":"bar" | |
} |
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
ISearchCommands ft = db.FT(); | |
try {ft.DropIndex("idx1");} catch {}; | |
ft.Create("idx1", new FTCreateParams().On(IndexDataType.JSON) | |
.Prefix("product:"), | |
new Schema().AddNumericField(new FieldName("$.id", "id")) | |
.AddTagField(new FieldName("$.gender", "gender")) | |
.AddTagField(new FieldName("$.season.*", "season")) | |
.AddTextField(new FieldName("$.description", "description")) | |
.AddNumericField(new FieldName("$.price", "price")) | |
.AddTextField(new FieldName("$.city", "city")) | |
.AddGeoField(new FieldName("$.coords", "coords"))); | |
IJsonCommands json = db.JSON(); | |
json.Set("product:15970", "$", new { | |
id = 15970, | |
gender = "Men", | |
season = new[] {"Fall", "Winter"}, | |
description = "Turtle Check Men Navy Blue Shirt", | |
price = 34.95, | |
city = "Boston", | |
coords = "-71.057083, 42.361145" | |
}); | |
json.Set("product:59263", "$", new { | |
id = 59263, | |
gender = "Women", | |
season = new[] {"Fall", "Winter", "Spring", "Summer"}, | |
description = "Titan Women Silver Watch", | |
price = 129.99, | |
city = "Dallas", | |
coords = "-96.808891, 32.779167" | |
}); | |
json.Set("product:46885", "$", new { | |
id = 46885, | |
gender = "Boys", | |
season = new[] {"Fall"}, | |
description = "Ben 10 Boys Navy Blue Slippers", | |
price = 45.99, | |
city = "Denver", | |
coords = "-104.991531, 39.742043" | |
}); | |
foreach (var doc in ft.Search("idx1", new Query("@season:{Spring}")) | |
.Documents.Select(x => x["json"])) | |
{ | |
Console.WriteLine(doc); | |
} |
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":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
IJsonCommands json = db.JSON(); | |
json.Set("warehouse:1", "$", new { | |
city = "Boston", | |
location = "42.361145, -71.057083", | |
inventory = new[] { | |
new { | |
id = 15970, | |
gender = "Men", | |
season = new[] {"Fall", "Winter"}, | |
description = "Turtle Check Men Navy Blue Shirt", | |
price = 34.95 | |
}, | |
new { | |
id = 59263, | |
gender = "Women", | |
season = new[] {"Fall", "Winter", "Spring", "Summer"}, | |
description = "Titan Women Silver Watch", | |
price = 129.99 | |
}, | |
new { | |
id = 46885, | |
gender = "Boys", | |
season = new[] {"Fall"}, | |
description = "Ben 10 Boys Navy Blue Slippers", | |
price = 45.99 | |
} | |
} | |
}); | |
Console.WriteLine(json.Get(key: "warehouse:1", | |
path: "$.inventory[?(@.price<100)]", | |
indent: "\t", | |
newLine: "\n" | |
)); |
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 | |
}, | |
{ | |
"id":46885, | |
"gender":"Boys", | |
"season":[ | |
"Fall" | |
], | |
"description":"Ben 10 Boys Navy Blue Slippers", | |
"price":45.99 | |
} | |
] |
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
ISearchCommands ft = db.FT(); | |
try {ft.DropIndex("wh_idx");} catch {}; | |
ft.Create("wh_idx", new FTCreateParams() | |
.On(IndexDataType.JSON) | |
.Prefix("warehouse:"), | |
new Schema().AddTextField(new FieldName("$.city", "city"))); | |
IJsonCommands json = db.JSON(); | |
json.Set("warehouse:1", "$", new { | |
city = "Boston", | |
location = "-71.057083, 42.361145", | |
inventory = new[] { | |
new { | |
id = 15970, | |
gender = "Men", | |
season = new[] {"Fall", "Winter"}, | |
description = "Turtle Check Men Navy Blue Shirt", | |
price = 34.95 | |
}, | |
new { | |
id = 59263, | |
gender = "Women", | |
season = new[] {"Fall", "Winter", "Spring", "Summer"}, | |
description = "Titan Women Silver Watch", | |
price = 129.99 | |
}, | |
new { | |
id = 46885, | |
gender = "Boys", | |
season = new[] {"Fall"}, | |
description = "Ben 10 Boys Navy Blue Slippers", | |
price = 45.99 | |
} | |
} | |
}); | |
json.Set("warehouse:2", "$", new { | |
city = "Dallas", | |
location = "-96.808891, 32.779167", | |
inventory = new[] { | |
new { | |
id = 51919, | |
gender = "Women", | |
season = new[] {"Summer"}, | |
description = "Nyk Black Horado Handbag", | |
price = 52.49 | |
}, | |
new { | |
id = 4602, | |
gender = "Unisex", | |
season = new[] {"Fall", "Winter"}, | |
description = "Wildcraft Red Trailblazer Backpack", | |
price = 50.99 | |
}, | |
new { | |
id = 37561, | |
gender = "Girls", | |
season = new[] {"Spring", "Summer"}, | |
description = "Madagascar3 Infant Pink Snapsuit Romper", | |
price = 23.95 | |
} | |
} | |
}); | |
foreach (var doc in ft.Search("wh_idx", | |
new Query("@city:(Dallas)") | |
.ReturnFields(new FieldName("$.inventory[?(@.gender==\"Women\" || @.gender==\"Girls\")]", "result")) | |
.Dialect(3)) | |
.Documents.Select(x => x["result"])) | |
{ | |
Console.WriteLine(doc); | |
} |
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":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.