|
| 1 | +// EXAMPLE: cs_home_json |
| 2 | + |
| 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 | +// HIDE_START |
| 19 | +public class HomeJsonExample |
| 20 | +{ |
| 21 | + |
| 22 | + [SkipIfRedis(Is.OSSCluster)] |
| 23 | + public void run() |
| 24 | + { |
| 25 | + // STEP_START connect |
| 26 | + var muxer = ConnectionMultiplexer.Connect("localhost:6379"); |
| 27 | + var db = muxer.GetDatabase(); |
| 28 | + // STEP_END |
| 29 | + |
| 30 | + //REMOVE_START |
| 31 | + // Clear any keys here before using them in tests. |
| 32 | + db.KeyDelete(new RedisKey[] { "user:1", "user:2", "user:3" }); |
| 33 | + try { db.FT().DropIndex("idx:users"); } catch { } |
| 34 | + //REMOVE_END |
| 35 | + // HIDE_END |
| 36 | + |
| 37 | + // STEP_START create_data |
| 38 | + var user1 = new |
| 39 | + { |
| 40 | + name = "Paul John", |
| 41 | + |
| 42 | + age = 42, |
| 43 | + city = "London" |
| 44 | + }; |
| 45 | + |
| 46 | + var user2 = new |
| 47 | + { |
| 48 | + name = "Eden Zamir", |
| 49 | + |
| 50 | + age = 29, |
| 51 | + city = "Tel Aviv" |
| 52 | + }; |
| 53 | + |
| 54 | + var user3 = new |
| 55 | + { |
| 56 | + name = "Paul Zamir", |
| 57 | + |
| 58 | + age = 35, |
| 59 | + city = "Tel Aviv" |
| 60 | + }; |
| 61 | + // STEP_END |
| 62 | + |
| 63 | + // STEP_START make_index |
| 64 | + var schema = new Schema() |
| 65 | + .AddTextField(new FieldName("$.name", "name")) |
| 66 | + .AddTagField(new FieldName("$.city", "city")) |
| 67 | + .AddNumericField(new FieldName("$.age", "age")); |
| 68 | + |
| 69 | + bool indexCreated = db.FT().Create( |
| 70 | + "idx:users", |
| 71 | + new FTCreateParams() |
| 72 | + .On(IndexDataType.JSON) |
| 73 | + .Prefix("user:"), |
| 74 | + schema |
| 75 | + ); |
| 76 | + // STEP_END |
| 77 | + |
| 78 | + // Tests for 'make_index' step. |
| 79 | + // REMOVE_START |
| 80 | + Assert.True(indexCreated); |
| 81 | + // REMOVE_END |
| 82 | + |
| 83 | + |
| 84 | + // STEP_START add_data |
| 85 | + bool user1Set = db.JSON().Set("user:1", "$", user1); |
| 86 | + bool user2Set = db.JSON().Set("user:2", "$", user2); |
| 87 | + bool user3Set = db.JSON().Set("user:3", "$", user3); |
| 88 | + // STEP_END |
| 89 | + |
| 90 | + // Tests for 'add_data' step. |
| 91 | + // REMOVE_START |
| 92 | + Assert.True(user1Set); |
| 93 | + Assert.True(user2Set); |
| 94 | + Assert.True(user3Set); |
| 95 | + // REMOVE_END |
| 96 | + |
| 97 | + |
| 98 | + // STEP_START query1 |
| 99 | + SearchResult findPaulResult = db.FT().Search( |
| 100 | + "idx:users", |
| 101 | + new Query("Paul @age:[30 40]") |
| 102 | + ); |
| 103 | + Console.WriteLine(string.Join( |
| 104 | + ", ", |
| 105 | + findPaulResult.Documents.Select(x => x["json"]) |
| 106 | + )); |
| 107 | + // >>> {"name":"Paul Zamir","email":"[email protected]", ... |
| 108 | + // STEP_END |
| 109 | + |
| 110 | + // Tests for 'query1' step. |
| 111 | + // REMOVE_START |
| 112 | + Assert.Equal( |
| 113 | + "{\"name\":\"Paul Zamir\",\"email\":\"[email protected]\",\"age\":35,\"city\":\"Tel Aviv\"}", |
| 114 | + string.Join(", ", findPaulResult.Documents.Select(x => x["json"])) |
| 115 | + ); |
| 116 | + // REMOVE_END |
| 117 | + |
| 118 | + |
| 119 | + // STEP_START query2 |
| 120 | + var citiesResult = db.FT().Search( |
| 121 | + "idx:users", |
| 122 | + new Query("Paul") |
| 123 | + .ReturnFields(new FieldName("$.city", "city")) |
| 124 | + ); |
| 125 | + Console.WriteLine(string.Join( |
| 126 | + ", ", |
| 127 | + citiesResult.Documents.Select(x => x["city"]).OrderBy(x => x) |
| 128 | + )); |
| 129 | + // >>> London, Tel Aviv |
| 130 | + // STEP_END |
| 131 | + |
| 132 | + // Tests for 'query2' step. |
| 133 | + // REMOVE_START |
| 134 | + Assert.Equal( |
| 135 | + "London, Tel Aviv", |
| 136 | + string.Join(", ", citiesResult.Documents.Select(x => x["city"]).OrderBy(x => x)) |
| 137 | + ); |
| 138 | + // REMOVE_END |
| 139 | + |
| 140 | + |
| 141 | + // STEP_START query3 |
| 142 | + AggregationRequest aggRequest = new AggregationRequest("*") |
| 143 | + .GroupBy("@city", Reducers.Count().As("count")); |
| 144 | + |
| 145 | + AggregationResult aggResult = db.FT().Aggregate("idx:users", aggRequest); |
| 146 | + IReadOnlyList<Dictionary<string, RedisValue>> resultsList = |
| 147 | + aggResult.GetResults(); |
| 148 | + |
| 149 | + for (var i = 0; i < resultsList.Count; i++) |
| 150 | + { |
| 151 | + Dictionary<string, RedisValue> item = resultsList.ElementAt(i); |
| 152 | + Console.WriteLine($"{item["city"]} - {item["count"]}"); |
| 153 | + } |
| 154 | + // >>> London - 1 |
| 155 | + // >>> Tel Aviv - 2 |
| 156 | + // STEP_END |
| 157 | + |
| 158 | + // Tests for 'query3' step. |
| 159 | + // REMOVE_START |
| 160 | + Assert.Equal(2, resultsList.Count); |
| 161 | + |
| 162 | + var sortedResults = resultsList.OrderBy(x => x["city"]); |
| 163 | + Dictionary<string, RedisValue> testItem = sortedResults.ElementAt(0); |
| 164 | + Assert.Equal("London", testItem["city"]); |
| 165 | + Assert.Equal(1, testItem["count"]); |
| 166 | + |
| 167 | + testItem = sortedResults.ElementAt(1); |
| 168 | + Assert.Equal("Tel Aviv", testItem["city"]); |
| 169 | + Assert.Equal(2, testItem["count"]); |
| 170 | + // REMOVE_END |
| 171 | + |
| 172 | + |
| 173 | + // HIDE_START |
| 174 | + } |
| 175 | +} |
| 176 | +// HIDE_END |
| 177 | + |
0 commit comments