|
| 1 | +// EXAMPLE: query_em |
| 2 | +// HIDE_START |
| 3 | + |
| 4 | +using NRedisStack.RedisStackCommands; |
| 5 | +using NRedisStack.Search; |
| 6 | +using NRedisStack.Search.Literals.Enums; |
| 7 | +using NRedisStack.Tests; |
| 8 | +using StackExchange.Redis; |
| 9 | + |
| 10 | +// HIDE_END |
| 11 | + |
| 12 | +// REMOVE_START |
| 13 | +namespace Doc; |
| 14 | +[Collection("DocsTests")] |
| 15 | +// REMOVE_END |
| 16 | + |
| 17 | +// HIDE_START |
| 18 | +public class QueryEmExample |
| 19 | +{ |
| 20 | + |
| 21 | + [SkipIfRedis(Is.OSSCluster)] |
| 22 | + public void run() |
| 23 | + { |
| 24 | + var muxer = ConnectionMultiplexer.Connect("localhost:6379"); |
| 25 | + var db = muxer.GetDatabase(); |
| 26 | + //REMOVE_START |
| 27 | + // Clear any keys here before using them in tests. |
| 28 | + try { db.FT().DropIndex("idx:bicycle"); } catch { } |
| 29 | + try { db.FT().DropIndex("idx:email"); } catch { } |
| 30 | + //REMOVE_END |
| 31 | + |
| 32 | + FTCreateParams idxParams = new FTCreateParams() |
| 33 | + .AddPrefix("bicycle:") |
| 34 | + .On(IndexDataType.JSON); |
| 35 | + |
| 36 | + Schema bikeSchema = new Schema() |
| 37 | + .AddTextField(new FieldName("$.description", "description")) |
| 38 | + .AddNumericField(new FieldName("$.price", "price")) |
| 39 | + .AddTagField(new FieldName("$.condition", "condition")); |
| 40 | + |
| 41 | + db.FT().Create("idx:bicycle", idxParams, bikeSchema); |
| 42 | + |
| 43 | + |
| 44 | + var bicycles = new object[] |
| 45 | + { |
| 46 | + new |
| 47 | + { |
| 48 | + brand = "Velorim", |
| 49 | + model = "Jigger", |
| 50 | + price = 270, |
| 51 | + description = "Small and powerful, the Jigger is the best ride " + |
| 52 | + "for the smallest of tikes! This is the tiniest " + |
| 53 | + "kids’ pedal bike on the market available without" + |
| 54 | + " a coaster brake, the Jigger is the vehicle of " + |
| 55 | + "choice for the rare tenacious little rider " + |
| 56 | + "raring to go.", |
| 57 | + condition = "used" |
| 58 | + }, |
| 59 | + new |
| 60 | + { |
| 61 | + brand = "Bicyk", |
| 62 | + model = "Hillcraft", |
| 63 | + price = 1200, |
| 64 | + description = "Kids want to ride with as little weight as possible." + |
| 65 | + " Especially on an incline! They may be at the age " + |
| 66 | + "when a 27.5 inch wheel bike is just too clumsy coming " + |
| 67 | + "off a 24 inch bike. The Hillcraft 26 is just the solution" + |
| 68 | + " they need!", |
| 69 | + condition = "used", |
| 70 | + }, |
| 71 | + new |
| 72 | + { |
| 73 | + brand = "Nord", |
| 74 | + model = "Chook air 5", |
| 75 | + price = 815, |
| 76 | + description = "The Chook Air 5 gives kids aged six years and older " + |
| 77 | + "a durable and uberlight mountain bike for their first" + |
| 78 | + " experience on tracks and easy cruising through forests" + |
| 79 | + " and fields. The lower top tube makes it easy to mount" + |
| 80 | + " and dismount in any situation, giving your kids greater" + |
| 81 | + " safety on the trails.", |
| 82 | + condition = "used", |
| 83 | + }, |
| 84 | + new |
| 85 | + { |
| 86 | + brand = "Eva", |
| 87 | + model = "Eva 291", |
| 88 | + price = 3400, |
| 89 | + description = "The sister company to Nord, Eva launched in 2005 as the" + |
| 90 | + " first and only women-dedicated bicycle brand. Designed" + |
| 91 | + " by women for women, allEva bikes are optimized for the" + |
| 92 | + " feminine physique using analytics from a body metrics" + |
| 93 | + " database. If you like 29ers, try the Eva 291. It’s a " + |
| 94 | + "brand new bike for 2022.. This full-suspension, " + |
| 95 | + "cross-country ride has been designed for velocity. The" + |
| 96 | + " 291 has 100mm of front and rear travel, a superlight " + |
| 97 | + "aluminum frame and fast-rolling 29-inch wheels. Yippee!", |
| 98 | + condition = "used", |
| 99 | + }, |
| 100 | + new |
| 101 | + { |
| 102 | + brand = "Noka Bikes", |
| 103 | + model = "Kahuna", |
| 104 | + price = 3200, |
| 105 | + description = "Whether you want to try your hand at XC racing or are " + |
| 106 | + "looking for a lively trail bike that's just as inspiring" + |
| 107 | + " on the climbs as it is over rougher ground, the Wilder" + |
| 108 | + " is one heck of a bike built specifically for short women." + |
| 109 | + " Both the frames and components have been tweaked to " + |
| 110 | + "include a women’s saddle, different bars and unique " + |
| 111 | + "colourway.", |
| 112 | + condition = "used", |
| 113 | + }, |
| 114 | + new |
| 115 | + { |
| 116 | + brand = "Breakout", |
| 117 | + model = "XBN 2.1 Alloy", |
| 118 | + price = 810, |
| 119 | + description = "The XBN 2.1 Alloy is our entry-level road bike – but that’s" + |
| 120 | + " not to say that it’s a basic machine. With an internal " + |
| 121 | + "weld aluminium frame, a full carbon fork, and the slick-shifting" + |
| 122 | + " Claris gears from Shimano’s, this is a bike which doesn’t" + |
| 123 | + " break the bank and delivers craved performance.", |
| 124 | + condition = "new", |
| 125 | + }, |
| 126 | + new |
| 127 | + { |
| 128 | + brand = "ScramBikes", |
| 129 | + model = "WattBike", |
| 130 | + price = 2300, |
| 131 | + description = "The WattBike is the best e-bike for people who still feel young" + |
| 132 | + " at heart. It has a Bafang 1000W mid-drive system and a 48V" + |
| 133 | + " 17.5AH Samsung Lithium-Ion battery, allowing you to ride for" + |
| 134 | + " more than 60 miles on one charge. It’s great for tackling hilly" + |
| 135 | + " terrain or if you just fancy a more leisurely ride. With three" + |
| 136 | + " working modes, you can choose between E-bike, assisted bicycle," + |
| 137 | + " and normal bike modes.", |
| 138 | + condition = "new", |
| 139 | + }, |
| 140 | + new |
| 141 | + { |
| 142 | + brand = "Peaknetic", |
| 143 | + model = "Secto", |
| 144 | + price = 430, |
| 145 | + description = "If you struggle with stiff fingers or a kinked neck or back after" + |
| 146 | + " a few minutes on the road, this lightweight, aluminum bike" + |
| 147 | + " alleviates those issues and allows you to enjoy the ride. From" + |
| 148 | + " the ergonomic grips to the lumbar-supporting seat position, the" + |
| 149 | + " Roll Low-Entry offers incredible comfort. The rear-inclined seat" + |
| 150 | + " tube facilitates stability by allowing you to put a foot on the" + |
| 151 | + " ground to balance at a stop, and the low step-over frame makes it" + |
| 152 | + " accessible for all ability and mobility levels. The saddle is" + |
| 153 | + " very soft, with a wide back to support your hip joints and a" + |
| 154 | + " cutout in the center to redistribute that pressure. Rim brakes" + |
| 155 | + " deliver satisfactory braking control, and the wide tires provide" + |
| 156 | + " a smooth, stable ride on paved roads and gravel. Rack and fender" + |
| 157 | + " mounts facilitate setting up the Roll Low-Entry as your preferred" + |
| 158 | + " commuter, and the BMX-like handlebar offers space for mounting a" + |
| 159 | + " flashlight, bell, or phone holder.", |
| 160 | + condition = "new", |
| 161 | + }, |
| 162 | + new |
| 163 | + { |
| 164 | + brand = "nHill", |
| 165 | + model = "Summit", |
| 166 | + price = 1200, |
| 167 | + description = "This budget mountain bike from nHill performs well both on bike" + |
| 168 | + " paths and on the trail. The fork with 100mm of travel absorbs" + |
| 169 | + " rough terrain. Fat Kenda Booster tires give you grip in corners" + |
| 170 | + " and on wet trails. The Shimano Tourney drivetrain offered enough" + |
| 171 | + " gears for finding a comfortable pace to ride uphill, and the" + |
| 172 | + " Tektro hydraulic disc brakes break smoothly. Whether you want an" + |
| 173 | + " affordable bike that you can take to work, but also take trail in" + |
| 174 | + " mountains on the weekends or you’re just after a stable," + |
| 175 | + " comfortable ride for the bike path, the Summit gives a good value" + |
| 176 | + " for money.", |
| 177 | + condition = "new", |
| 178 | + }, |
| 179 | + new |
| 180 | + { |
| 181 | + model = "ThrillCycle", |
| 182 | + brand = "BikeShind", |
| 183 | + price = 815, |
| 184 | + description = "An artsy, retro-inspired bicycle that’s as functional as it is" + |
| 185 | + " pretty: The ThrillCycle steel frame offers a smooth ride. A" + |
| 186 | + " 9-speed drivetrain has enough gears for coasting in the city, but" + |
| 187 | + " we wouldn’t suggest taking it to the mountains. Fenders protect" + |
| 188 | + " you from mud, and a rear basket lets you transport groceries," + |
| 189 | + " flowers and books. The ThrillCycle comes with a limited lifetime" + |
| 190 | + " warranty, so this little guy will last you long past graduation.", |
| 191 | + condition = "refurbished", |
| 192 | + }, |
| 193 | + }; |
| 194 | + |
| 195 | + for (var i = 0; i < bicycles.Length; i++) |
| 196 | + { |
| 197 | + db.JSON().Set($"bicycle:{i}", "$", bicycles[i]); |
| 198 | + } |
| 199 | + // HIDE_END |
| 200 | + |
| 201 | + |
| 202 | + // STEP_START em1 |
| 203 | + SearchResult res1 = db.FT().Search( |
| 204 | + "idx:bicycle", |
| 205 | + new Query("@price:[270 270]") |
| 206 | + ); |
| 207 | + Console.WriteLine(res1.TotalResults); // >>> 1 |
| 208 | + |
| 209 | + SearchResult res2 = db.FT().Search( |
| 210 | + "idx:bicycle", |
| 211 | + new Query().AddFilter( |
| 212 | + new Query.NumericFilter("price", 270, 270) |
| 213 | + ) |
| 214 | + ); |
| 215 | + Console.WriteLine(res2.TotalResults); // >>> 1 |
| 216 | + // STEP_END |
| 217 | + |
| 218 | + // Tests for 'em1' step. |
| 219 | + // REMOVE_START |
| 220 | + Assert.Equal(1, res1.TotalResults); |
| 221 | + Assert.Equal(1, res2.TotalResults); |
| 222 | + // REMOVE_END |
| 223 | + |
| 224 | + |
| 225 | + // STEP_START em2 |
| 226 | + SearchResult res3 = db.FT().Search( |
| 227 | + "idx:bicycle", |
| 228 | + new Query("@condition:{new}") |
| 229 | + ); |
| 230 | + Console.WriteLine(res3.TotalResults); // >>> 4 |
| 231 | + // STEP_END |
| 232 | + |
| 233 | + // Tests for 'em2' step. |
| 234 | + // REMOVE_START |
| 235 | + Assert.Equal(4, res3.TotalResults); |
| 236 | + // REMOVE_END |
| 237 | + |
| 238 | + |
| 239 | + // STEP_START em3 |
| 240 | + Schema emailSchema = new Schema() |
| 241 | + .AddTagField(new FieldName("$.email", "email")); |
| 242 | + |
| 243 | + FTCreateParams emailParams = new FTCreateParams() |
| 244 | + .AddPrefix("key:") |
| 245 | + .On(IndexDataType.JSON); |
| 246 | + |
| 247 | + db.FT().Create("idx:email", emailParams, emailSchema); |
| 248 | + |
| 249 | + db.JSON().Set("key:1", "$", "{\"email\": \"[email protected]\"}"); |
| 250 | + |
| 251 | + |
| 252 | + SearchResult res4 = db.FT().Search( |
| 253 | + "idx:email", |
| 254 | + new Query("@email:{test\\@redis\\.com}").Dialect(2) |
| 255 | + ); |
| 256 | + Console.WriteLine(res4.TotalResults); // >>> 1 |
| 257 | + // STEP_END |
| 258 | + |
| 259 | + // Tests for 'em3' step. |
| 260 | + // REMOVE_START |
| 261 | + db.FT().DropIndex("idx:email"); |
| 262 | + Assert.Equal(1, res4.TotalResults); |
| 263 | + // REMOVE_END |
| 264 | + |
| 265 | + |
| 266 | + // STEP_START em4 |
| 267 | + SearchResult res5 = db.FT().Search( |
| 268 | + "idx:bicycle", |
| 269 | + new Query("@description:\"rough terrain\"") |
| 270 | + ); |
| 271 | + Console.WriteLine(res5.TotalResults); // >>> 1 |
| 272 | + // STEP_END |
| 273 | + |
| 274 | + // Tests for 'em4' step. |
| 275 | + // REMOVE_START |
| 276 | + Assert.Equal(1, res5.TotalResults); |
| 277 | + // REMOVE_END |
| 278 | + |
| 279 | + |
| 280 | + // HIDE_START |
| 281 | + } |
| 282 | +} |
| 283 | +// HIDE_END |
| 284 | + |
0 commit comments