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