|
| 1 | +// EXAMPLE: cs_home_json |
| 2 | +// BINDER_ID netsync-cs_home_json |
| 3 | +// STEP_START import |
| 4 | +using NRedisStack.RedisStackCommands; |
| 5 | +using NRedisStack.Search; |
| 6 | +using NRedisStack.Search.Aggregation; |
| 7 | +using NRedisStack.Search.Literals.Enums; |
| 8 | +using StackExchange.Redis; |
| 9 | +// STEP_END |
| 10 | + |
| 11 | +// REMOVE_START |
| 12 | +using NRedisStack.Tests; |
| 13 | + |
| 14 | +namespace Doc; |
| 15 | +[Collection("DocsTests")] |
| 16 | +// REMOVE_END |
| 17 | + |
| 18 | +public class HomeJsonExample |
| 19 | +// REMOVE_START |
| 20 | +: AbstractNRedisStackTest, IDisposable |
| 21 | +// REMOVE_END |
| 22 | +{ |
| 23 | + // REMOVE_START |
| 24 | + public HomeJsonExample(EndpointsFixture fixture) : base(fixture) { } |
| 25 | + |
| 26 | + [SkippableFact] |
| 27 | + // REMOVE_END |
| 28 | + public void Run() |
| 29 | + { |
| 30 | + //REMOVE_START |
| 31 | + // This is needed because we're constructing ConfigurationOptions in the test before calling GetConnection |
| 32 | + SkipIfTargetConnectionDoesNotExist(EndpointsFixture.Env.Standalone); |
| 33 | + var _ = GetCleanDatabase(EndpointsFixture.Env.Standalone); |
| 34 | + //REMOVE_END |
| 35 | + |
| 36 | + // STEP_START connect |
| 37 | + var muxer = ConnectionMultiplexer.Connect("localhost:6379"); |
| 38 | + var db = muxer.GetDatabase(); |
| 39 | + // STEP_END |
| 40 | + |
| 41 | + // STEP_START cleanup_json |
| 42 | + db.KeyDelete(["user:1", "user:2", "user:3"]); |
| 43 | + try { db.FT().DropIndex("idx:users"); } catch { } |
| 44 | + // STEP_END |
| 45 | + |
| 46 | + // STEP_START create_data |
| 47 | + var user1 = new |
| 48 | + { |
| 49 | + name = "Paul John", |
| 50 | + |
| 51 | + age = 42, |
| 52 | + city = "London" |
| 53 | + }; |
| 54 | + |
| 55 | + var user2 = new |
| 56 | + { |
| 57 | + name = "Eden Zamir", |
| 58 | + |
| 59 | + age = 29, |
| 60 | + city = "Tel Aviv" |
| 61 | + }; |
| 62 | + |
| 63 | + var user3 = new |
| 64 | + { |
| 65 | + name = "Paul Zamir", |
| 66 | + |
| 67 | + age = 35, |
| 68 | + city = "Tel Aviv" |
| 69 | + }; |
| 70 | + // STEP_END |
| 71 | + |
| 72 | + // STEP_START make_index |
| 73 | + var schema = new Schema() |
| 74 | + .AddTextField(new FieldName("$.name", "name")) |
| 75 | + .AddTagField(new FieldName("$.city", "city")) |
| 76 | + .AddNumericField(new FieldName("$.age", "age")); |
| 77 | + |
| 78 | + bool indexCreated = db.FT().Create( |
| 79 | + "idx:users", |
| 80 | + new FTCreateParams() |
| 81 | + .On(IndexDataType.JSON) |
| 82 | + .Prefix("user:"), |
| 83 | + schema |
| 84 | + ); |
| 85 | + // STEP_END |
| 86 | + // REMOVE_START |
| 87 | + Assert.True(indexCreated); |
| 88 | + // REMOVE_END |
| 89 | + |
| 90 | + |
| 91 | + // STEP_START add_data |
| 92 | + bool user1Set = db.JSON().Set("user:1", "$", user1); |
| 93 | + bool user2Set = db.JSON().Set("user:2", "$", user2); |
| 94 | + bool user3Set = db.JSON().Set("user:3", "$", user3); |
| 95 | + // STEP_END |
| 96 | + // REMOVE_START |
| 97 | + Assert.True(user1Set); |
| 98 | + Assert.True(user2Set); |
| 99 | + Assert.True(user3Set); |
| 100 | + // REMOVE_END |
| 101 | + |
| 102 | + |
| 103 | + // STEP_START query1 |
| 104 | + SearchResult findPaulResult = db.FT().Search( |
| 105 | + "idx:users", |
| 106 | + new("Paul @age:[30 40]") |
| 107 | + ); |
| 108 | + Console.WriteLine(string.Join( |
| 109 | + ", ", |
| 110 | + findPaulResult.Documents.Select(x => x["json"]) |
| 111 | + )); |
| 112 | + // >>> {"name":"Paul Zamir","email":"[email protected]", ... |
| 113 | + // STEP_END |
| 114 | + // REMOVE_START |
| 115 | + Assert.Equal( |
| 116 | + "{\"name\":\"Paul Zamir\",\"email\":\"[email protected]\",\"age\":35,\"city\":\"Tel Aviv\"}", |
| 117 | + string.Join(", ", findPaulResult.Documents.Select(x => x["json"])) |
| 118 | + ); |
| 119 | + // REMOVE_END |
| 120 | + |
| 121 | + |
| 122 | + // STEP_START query2 |
| 123 | + var citiesResult = db.FT().Search( |
| 124 | + "idx:users", |
| 125 | + new Query("Paul") |
| 126 | + .ReturnFields(new FieldName("$.city", "city")) |
| 127 | + ); |
| 128 | + Console.WriteLine(string.Join( |
| 129 | + ", ", |
| 130 | + citiesResult.Documents.Select(x => x["city"]).OrderBy(x => x) |
| 131 | + )); |
| 132 | + // >>> London, Tel Aviv |
| 133 | + // STEP_END |
| 134 | + // REMOVE_START |
| 135 | + Assert.Equal( |
| 136 | + "London, Tel Aviv", |
| 137 | + string.Join(", ", citiesResult.Documents.Select(x => x["city"]).OrderBy(x => x)) |
| 138 | + ); |
| 139 | + // REMOVE_END |
| 140 | + |
| 141 | + |
| 142 | + // STEP_START query3 |
| 143 | + AggregationRequest aggRequest = new AggregationRequest("*") |
| 144 | + .GroupBy("@city", Reducers.Count().As("count")); |
| 145 | + |
| 146 | + AggregationResult aggResult = db.FT().Aggregate("idx:users", aggRequest); |
| 147 | + IReadOnlyList<Dictionary<string, RedisValue>> resultsList = |
| 148 | + aggResult.GetResults(); |
| 149 | + |
| 150 | + for (var i = 0; i < resultsList.Count; i++) |
| 151 | + { |
| 152 | + Dictionary<string, RedisValue> item = resultsList.ElementAt(i); |
| 153 | + Console.WriteLine($"{item["city"]} - {item["count"]}"); |
| 154 | + } |
| 155 | + // >>> London - 1 |
| 156 | + // >>> Tel Aviv - 2 |
| 157 | + // STEP_END |
| 158 | + // REMOVE_START |
| 159 | + Assert.Equal(2, resultsList.Count); |
| 160 | + |
| 161 | + var sortedResults = resultsList.OrderBy(x => x["city"]); |
| 162 | + Dictionary<string, RedisValue> testItem = sortedResults.ElementAt(0); |
| 163 | + Assert.Equal("London", testItem["city"]); |
| 164 | + Assert.Equal(1, testItem["count"]); |
| 165 | + |
| 166 | + testItem = sortedResults.ElementAt(1); |
| 167 | + Assert.Equal("Tel Aviv", testItem["city"]); |
| 168 | + Assert.Equal(2, testItem["count"]); |
| 169 | + // REMOVE_END |
| 170 | + |
| 171 | + // STEP_START cleanup_hash |
| 172 | + db.KeyDelete(["huser:1", "huser:2", "huser:3"]); |
| 173 | + try { db.FT().DropIndex("hash-idx:users"); } catch { } |
| 174 | + // STEP_END |
| 175 | + |
| 176 | + // STEP_START make_hash_index |
| 177 | + var hashSchema = new Schema() |
| 178 | + .AddTextField("name") |
| 179 | + .AddTagField("city") |
| 180 | + .AddNumericField("age"); |
| 181 | + |
| 182 | + bool hashIndexCreated = db.FT().Create( |
| 183 | + "hash-idx:users", |
| 184 | + new FTCreateParams() |
| 185 | + .On(IndexDataType.HASH) |
| 186 | + .Prefix("huser:"), |
| 187 | + hashSchema |
| 188 | + ); |
| 189 | + // STEP_END |
| 190 | + // REMOVE_START |
| 191 | + Assert.True(hashIndexCreated); |
| 192 | + // REMOVE_END |
| 193 | + |
| 194 | + // STEP_START add_hash_data |
| 195 | + db.HashSet("huser:1", [ |
| 196 | + new("name", "Paul John"), |
| 197 | + new("email", "[email protected]"), |
| 198 | + new("age", 42), |
| 199 | + new("city", "London") |
| 200 | + ]); |
| 201 | + |
| 202 | + db.HashSet("huser:2", [ |
| 203 | + new("name", "Eden Zamir"), |
| 204 | + new("email", "[email protected]"), |
| 205 | + new("age", 29), |
| 206 | + new("city", "Tel Aviv") |
| 207 | + ]); |
| 208 | + |
| 209 | + db.HashSet("huser:3", [ |
| 210 | + new("name", "Paul Zamir"), |
| 211 | + new("email", "[email protected]"), |
| 212 | + new("age", 35), |
| 213 | + new("city", "Tel Aviv") |
| 214 | + ]); |
| 215 | + // STEP_END |
| 216 | + |
| 217 | + // STEP_START query1_hash |
| 218 | + SearchResult findPaulHashResult = db.FT().Search( |
| 219 | + "hash-idx:users", |
| 220 | + new("Paul @age:[30 40]") |
| 221 | + ); |
| 222 | + |
| 223 | + foreach (Document doc in findPaulHashResult.Documents) |
| 224 | + { |
| 225 | + Console.WriteLine( |
| 226 | + $"Name: {doc["name"]}, email: {doc["email"]}, " + |
| 227 | + $"age: {doc["age"]}, city:{doc["city"]}" |
| 228 | + ); |
| 229 | + } |
| 230 | + // >>> Name: Paul Zamir, email: [email protected], age: 35, ... |
| 231 | + // STEP_END |
| 232 | + // REMOVE_START |
| 233 | + Document d = findPaulHashResult.Documents[0]; |
| 234 | + Assert.Equal( |
| 235 | + "Name: Paul Zamir, email: [email protected], age: 35, city:Tel Aviv", |
| 236 | + $"Name: {d["name"]}, email: {d["email"]}, " + |
| 237 | + $"age: {d["age"]}, city:{d["city"]}" |
| 238 | + ); |
| 239 | + // REMOVE_END |
| 240 | + } |
| 241 | +} |
0 commit comments