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