|
| 1 | +// EXAMPLE: search_quickstart |
| 2 | +using System; |
| 3 | +using NRedisStack; |
| 4 | +using NRedisStack.RedisStackCommands; |
| 5 | +using NRedisStack.Search; |
| 6 | +using NRedisStack.Search.Aggregation; |
| 7 | +using NRedisStack.Search.Literals.Enums; |
| 8 | +using StackExchange.Redis; |
| 9 | + |
| 10 | +namespace NRedisStack.Doc; |
| 11 | + |
| 12 | +public class SearchQuickstartExample |
| 13 | +{ |
| 14 | + [Fact] |
| 15 | + public void run() |
| 16 | + { |
| 17 | + // STEP_START connect |
| 18 | + var redis = ConnectionMultiplexer.Connect("localhost:6379"); |
| 19 | + var db = redis.GetDatabase(); |
| 20 | + var ft = db.FT(); |
| 21 | + var json = db.JSON(); |
| 22 | + // STEP_END |
| 23 | + |
| 24 | + // REMOVE_START |
| 25 | + try { ft.DropIndex("idx:bicycle"); } catch { }; |
| 26 | + // REMOVE_END |
| 27 | + |
| 28 | + // STEP_START data_sample |
| 29 | + var bike1 = new { |
| 30 | + Brand = "Diaz Ltd", |
| 31 | + Model = "Dealer Sl", |
| 32 | + Price = 7315.58M, |
| 33 | + Description="The Diaz Ltd Dealer Sl is a reliable choice" + |
| 34 | + " for urban cycling. The Diaz Ltd Dealer Sl " + |
| 35 | + "is a comfortable choice for urban cycling.", |
| 36 | + Condition="used" |
| 37 | + }; |
| 38 | + // STEP_END |
| 39 | + |
| 40 | + var bicycles = new[] { |
| 41 | + bike1, |
| 42 | + new { |
| 43 | + Brand = "Bridges Group", |
| 44 | + Model = "Project Pro", |
| 45 | + Price = 3610.82M, |
| 46 | + Description = "This mountain bike is perfect for mountain biking. The Bridges Group Project Pro is a responsive choice for mountain biking.", |
| 47 | + Condition = "used" |
| 48 | + }, |
| 49 | + new { |
| 50 | + Brand = "Vega, Cole and Miller", |
| 51 | + Model = "Group Advanced", |
| 52 | + Price = 8961.42M, |
| 53 | + Description = "The Vega, Cole and Miller Group Advanced provides an excellent ride. With its fast carbon frame and 24 gears, this bicycle is perfect for any terrain.", |
| 54 | + Condition = "used" |
| 55 | + }, |
| 56 | + new { |
| 57 | + Brand = "Powell-Montgomery", |
| 58 | + Model = "Angle Race", |
| 59 | + Price = 4050.27M, |
| 60 | + Description = "The Powell-Montgomery Angle Race is a smooth choice for road cycling. The Powell-Montgomery Angle Race provides a durable ride.", |
| 61 | + Condition = "used" |
| 62 | + }, |
| 63 | + new { |
| 64 | + Brand = "Gill-Lewis", |
| 65 | + Model = "Action Evo", |
| 66 | + Price = 283.68M, |
| 67 | + Description = "The Gill-Lewis Action Evo provides a smooth ride. The Gill-Lewis Action Evo provides an excellent ride.", |
| 68 | + Condition = "used" |
| 69 | + }, |
| 70 | + new { |
| 71 | + Brand = "Rodriguez-Guerrero", |
| 72 | + Model = "Drama Comp", |
| 73 | + Price = 4462.55M, |
| 74 | + Description = "This kids bike is perfect for young riders. With its excellent aluminum frame and 12 gears, this bicycle is perfect for any terrain.", |
| 75 | + Condition = "new" |
| 76 | + }, |
| 77 | + new { |
| 78 | + Brand = "Moore PLC", |
| 79 | + Model = "Award Race", |
| 80 | + Price = 3790.76M, |
| 81 | + Description = "This olive folding bike features a carbon frame and 27.5 inch wheels. This folding bike is perfect for compact storage and transportation.", |
| 82 | + Condition = "new" |
| 83 | + }, |
| 84 | + new { |
| 85 | + Brand = "Hall, Haley and Hayes", |
| 86 | + Model = "Weekend Plus", |
| 87 | + Price = 2008.4M, |
| 88 | + Description = "The Hall, Haley and Hayes Weekend Plus provides a comfortable ride. This blue kids bike features a steel frame and 29.0 inch wheels.", |
| 89 | + Condition = "new" |
| 90 | + }, |
| 91 | + new { |
| 92 | + Brand = "Peck-Carson", |
| 93 | + Model = "Sun Hybrid", |
| 94 | + Price = 9874.95M, |
| 95 | + Description = "With its comfortable aluminum frame and 25 gears, this bicycle is perfect for any terrain. The Peck-Carson Sun Hybrid provides a comfortable ride.", |
| 96 | + Condition = "new" |
| 97 | + }, |
| 98 | + new { |
| 99 | + Brand = "Fowler Ltd", |
| 100 | + Model = "Weekend Trail", |
| 101 | + Price = 3833.71M, |
| 102 | + Description = "The Fowler Ltd Letter Trail is a comfortable choice for transporting cargo. This cargo bike is perfect for transporting cargo.", |
| 103 | + Condition = "refurbished" |
| 104 | + } |
| 105 | + }; |
| 106 | + |
| 107 | + // STEP_START define_index |
| 108 | + var schema = new Schema() |
| 109 | + .AddTextField(new FieldName("$.Brand", "Brand")) |
| 110 | + .AddTextField(new FieldName("$.Model", "Model")) |
| 111 | + .AddTextField(new FieldName("$.Description", "Description")) |
| 112 | + .AddNumericField(new FieldName("$.Price", "Price")) |
| 113 | + .AddTagField(new FieldName("$.Condition", "Condition")); |
| 114 | + // STEP_END |
| 115 | + |
| 116 | + // STEP_START create_index |
| 117 | + ft.Create( |
| 118 | + "idx:bicycle", |
| 119 | + new FTCreateParams().On(IndexDataType.JSON).Prefix("bicycle:"), |
| 120 | + schema); |
| 121 | + // STEP_END |
| 122 | + |
| 123 | + // STEP_START add_documents |
| 124 | + for (int i=0; i < bicycles.Length; i++) |
| 125 | + { |
| 126 | + json.Set($"bicycle:{i}", "$", bicycles[i]); |
| 127 | + } |
| 128 | + // STEP_END |
| 129 | + |
| 130 | + // STEP_START query_single_term_and_num_range |
| 131 | + var query = new Query("folding @Price:[1000 4000]"); |
| 132 | + var res = ft.Search("idx:bicycle", query).Documents; |
| 133 | + Console.WriteLine(string.Join("\n", res.Select(x => x["json"]))); |
| 134 | + // Prints: {"Brand":"Moore PLC","Model":"Award Race","Price":3790.76,"Description":"This olive folding bike features a carbon frame and 27.5 inch wheels. This folding bike is perfect for compact storage and transportation.","Condition":"new"} |
| 135 | + // STEP_END |
| 136 | + // REMOVE_START |
| 137 | + Assert.Single(res); |
| 138 | + Assert.Equal("bicycle:6", res[0].Id); |
| 139 | + // REMOVE_END |
| 140 | + |
| 141 | + // STEP_START query_single_term_limit_fields |
| 142 | + var cargoQuery = new Query("cargo").ReturnFields("Price"); |
| 143 | + var cargoRes = ft.Search("idx:bicycle", cargoQuery).Documents; |
| 144 | + Console.WriteLine(cargoRes.First()["Price"]); |
| 145 | + // Prints: 3833.71 |
| 146 | + // STEP_END |
| 147 | + // REMOVE_START |
| 148 | + Assert.Single(cargoRes); |
| 149 | + Assert.Equal("bicycle:9", cargoRes[0].Id); |
| 150 | + // REMOVE_END |
| 151 | + |
| 152 | + // STEP_START simple_aggregation |
| 153 | + var request = new AggregationRequest("*").GroupBy("@Condition", Reducers.Count().As("Count")); |
| 154 | + var result = ft.Aggregate("idx:bicycle", request); |
| 155 | + |
| 156 | + for (var i=0; i<result.TotalResults; i++) |
| 157 | + { |
| 158 | + var row = result.GetRow(i); |
| 159 | + Console.WriteLine($"{row["Condition"]} - {row["Count"]}"); |
| 160 | + } |
| 161 | + // Prints: |
| 162 | + // refurbished - 1 |
| 163 | + // used - 5 |
| 164 | + // new - 4 |
| 165 | + // STEP_END |
| 166 | + // REMOVE_START |
| 167 | + Assert.Equal(3, result.TotalResults); |
| 168 | + // REMOVE_END |
| 169 | + } |
| 170 | +} |
0 commit comments