|  | 
|  | 1 | +// EXAMPLE: query_range | 
|  | 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 | +func ExampleClient_query_range() { | 
|  | 13 | +	ctx := context.Background() | 
|  | 14 | + | 
|  | 15 | +	rdb := redis.NewClient(&redis.Options{ | 
|  | 16 | +		Addr:     "localhost:6379", | 
|  | 17 | +		Password: "", // no password docs | 
|  | 18 | +		DB:       0,  // use default DB | 
|  | 19 | +		Protocol: 2, | 
|  | 20 | +	}) | 
|  | 21 | + | 
|  | 22 | +	// HIDE_END | 
|  | 23 | +	// REMOVE_START | 
|  | 24 | +	rdb.FTDropIndex(ctx, "idx:bicycle") | 
|  | 25 | +	rdb.FTDropIndex(ctx, "idx:email") | 
|  | 26 | +	// REMOVE_END | 
|  | 27 | + | 
|  | 28 | +	_, err := rdb.FTCreate(ctx, "idx:bicycle", | 
|  | 29 | +		&redis.FTCreateOptions{ | 
|  | 30 | +			OnJSON: true, | 
|  | 31 | +			Prefix: []interface{}{"bicycle:"}, | 
|  | 32 | +		}, | 
|  | 33 | +		&redis.FieldSchema{ | 
|  | 34 | +			FieldName: "$.brand", | 
|  | 35 | +			As:        "brand", | 
|  | 36 | +			FieldType: redis.SearchFieldTypeText, | 
|  | 37 | +		}, | 
|  | 38 | +		&redis.FieldSchema{ | 
|  | 39 | +			FieldName: "$.model", | 
|  | 40 | +			As:        "model", | 
|  | 41 | +			FieldType: redis.SearchFieldTypeText, | 
|  | 42 | +		}, | 
|  | 43 | +		&redis.FieldSchema{ | 
|  | 44 | +			FieldName: "$.description", | 
|  | 45 | +			As:        "description", | 
|  | 46 | +			FieldType: redis.SearchFieldTypeText, | 
|  | 47 | +		}, | 
|  | 48 | +		&redis.FieldSchema{ | 
|  | 49 | +			FieldName: "$.price", | 
|  | 50 | +			As:        "price", | 
|  | 51 | +			FieldType: redis.SearchFieldTypeNumeric, | 
|  | 52 | +		}, | 
|  | 53 | +		&redis.FieldSchema{ | 
|  | 54 | +			FieldName: "$.condition", | 
|  | 55 | +			As:        "condition", | 
|  | 56 | +			FieldType: redis.SearchFieldTypeTag, | 
|  | 57 | +		}, | 
|  | 58 | +	).Result() | 
|  | 59 | + | 
|  | 60 | +	if err != nil { | 
|  | 61 | +		panic(err) | 
|  | 62 | +	} | 
|  | 63 | + | 
|  | 64 | +	exampleJsons := []map[string]interface{}{ | 
|  | 65 | +		{ | 
|  | 66 | +			"pickup_zone": "POLYGON((-74.0610 40.7578, -73.9510 40.7578, -73.9510 40.6678, " + | 
|  | 67 | +				"-74.0610 40.6678, -74.0610 40.7578))", | 
|  | 68 | +			"store_location": "-74.0060,40.7128", | 
|  | 69 | +			"brand":          "Velorim", | 
|  | 70 | +			"model":          "Jigger", | 
|  | 71 | +			"price":          270, | 
|  | 72 | +			"description": "Small and powerful, the Jigger is the best ride for the smallest of tikes! " + | 
|  | 73 | +				"This is the tiniest kids pedal bike on the market available without a coaster brake, the Jigger " + | 
|  | 74 | +				"is the vehicle of choice for the rare tenacious little rider raring to go.", | 
|  | 75 | +			"condition": "new", | 
|  | 76 | +		}, | 
|  | 77 | +		{ | 
|  | 78 | +			"pickup_zone": "POLYGON((-118.2887 34.0972, -118.1987 34.0972, -118.1987 33.9872, " + | 
|  | 79 | +				"-118.2887 33.9872, -118.2887 34.0972))", | 
|  | 80 | +			"store_location": "-118.2437,34.0522", | 
|  | 81 | +			"brand":          "Bicyk", | 
|  | 82 | +			"model":          "Hillcraft", | 
|  | 83 | +			"price":          1200, | 
|  | 84 | +			"description": "Kids want to ride with as little weight as possible. Especially " + | 
|  | 85 | +				"on an incline! They may be at the age when a 27.5'' wheel bike is just too clumsy coming " + | 
|  | 86 | +				"off a 24'' bike. The Hillcraft 26 is just the solution they need!", | 
|  | 87 | +			"condition": "used", | 
|  | 88 | +		}, | 
|  | 89 | +		{ | 
|  | 90 | +			"pickup_zone": "POLYGON((-87.6848 41.9331, -87.5748 41.9331, -87.5748 41.8231, " + | 
|  | 91 | +				"-87.6848 41.8231, -87.6848 41.9331))", | 
|  | 92 | +			"store_location": "-87.6298,41.8781", | 
|  | 93 | +			"brand":          "Nord", | 
|  | 94 | +			"model":          "Chook air 5", | 
|  | 95 | +			"price":          815, | 
|  | 96 | +			"description": "The Chook Air 5  gives kids aged six years and older a durable " + | 
|  | 97 | +				"and uberlight mountain bike for their first experience on tracks and easy cruising through " + | 
|  | 98 | +				"forests and fields. The lower  top tube makes it easy to mount and dismount in any " + | 
|  | 99 | +				"situation, giving your kids greater safety on the trails.", | 
|  | 100 | +			"condition": "used", | 
|  | 101 | +		}, | 
|  | 102 | +		{ | 
|  | 103 | +			"pickup_zone": "POLYGON((-80.2433 25.8067, -80.1333 25.8067, -80.1333 25.6967, " + | 
|  | 104 | +				"-80.2433 25.6967, -80.2433 25.8067))", | 
|  | 105 | +			"store_location": "-80.1918,25.7617", | 
|  | 106 | +			"brand":          "Eva", | 
|  | 107 | +			"model":          "Eva 291", | 
|  | 108 | +			"price":          3400, | 
|  | 109 | +			"description": "The sister company to Nord, Eva launched in 2005 as the first " + | 
|  | 110 | +				"and only women-dedicated bicycle brand. Designed by women for women, allEva bikes " + | 
|  | 111 | +				"are optimized for the feminine physique using analytics from a body metrics database. " + | 
|  | 112 | +				"If you like 29ers, try the Eva 291. It’s a brand new bike for 2022.. This " + | 
|  | 113 | +				"full-suspension, cross-country ride has been designed for velocity. The 291 has " + | 
|  | 114 | +				"100mm of front and rear travel, a superlight aluminum frame and fast-rolling " + | 
|  | 115 | +				"29-inch wheels. Yippee!", | 
|  | 116 | +			"condition": "used", | 
|  | 117 | +		}, | 
|  | 118 | +		{ | 
|  | 119 | +			"pickup_zone": "POLYGON((-122.4644 37.8199, -122.3544 37.8199, -122.3544 37.7099, " + | 
|  | 120 | +				"-122.4644 37.7099, -122.4644 37.8199))", | 
|  | 121 | +			"store_location": "-122.4194,37.7749", | 
|  | 122 | +			"brand":          "Noka Bikes", | 
|  | 123 | +			"model":          "Kahuna", | 
|  | 124 | +			"price":          3200, | 
|  | 125 | +			"description": "Whether you want to try your hand at XC racing or are looking " + | 
|  | 126 | +				"for a lively trail bike that's just as inspiring on the climbs as it is over rougher " + | 
|  | 127 | +				"ground, the Wilder is one heck of a bike built specifically for short women. Both the " + | 
|  | 128 | +				"frames and components have been tweaked to include a women’s saddle, different bars " + | 
|  | 129 | +				"and unique colourway.", | 
|  | 130 | +			"condition": "used", | 
|  | 131 | +		}, | 
|  | 132 | +		{ | 
|  | 133 | +			"pickup_zone": "POLYGON((-0.1778 51.5524, 0.0822 51.5524, 0.0822 51.4024, " + | 
|  | 134 | +				"-0.1778 51.4024, -0.1778 51.5524))", | 
|  | 135 | +			"store_location": "-0.1278,51.5074", | 
|  | 136 | +			"brand":          "Breakout", | 
|  | 137 | +			"model":          "XBN 2.1 Alloy", | 
|  | 138 | +			"price":          810, | 
|  | 139 | +			"description": "The XBN 2.1 Alloy is our entry-level road bike – but that’s " + | 
|  | 140 | +				"not to say that it’s a basic machine. With an internal weld aluminium frame, a full " + | 
|  | 141 | +				"carbon fork, and the slick-shifting Claris gears from Shimano’s, this is a bike which " + | 
|  | 142 | +				"doesn’t break the bank and delivers craved performance.", | 
|  | 143 | +			"condition": "new", | 
|  | 144 | +		}, | 
|  | 145 | +		{ | 
|  | 146 | +			"pickup_zone": "POLYGON((2.1767 48.9016, 2.5267 48.9016, 2.5267 48.5516, " + | 
|  | 147 | +				"2.1767 48.5516, 2.1767 48.9016))", | 
|  | 148 | +			"store_location": "2.3522,48.8566", | 
|  | 149 | +			"brand":          "ScramBikes", | 
|  | 150 | +			"model":          "WattBike", | 
|  | 151 | +			"price":          2300, | 
|  | 152 | +			"description": "The WattBike is the best e-bike for people who still " + | 
|  | 153 | +				"feel young at heart. It has a Bafang 1000W mid-drive system and a 48V 17.5AH " + | 
|  | 154 | +				"Samsung Lithium-Ion battery, allowing you to ride for more than 60 miles on one " + | 
|  | 155 | +				"charge. It’s great for tackling hilly terrain or if you just fancy a more " + | 
|  | 156 | +				"leisurely ride. With three working modes, you can choose between E-bike, " + | 
|  | 157 | +				"assisted bicycle, and normal bike modes.", | 
|  | 158 | +			"condition": "new", | 
|  | 159 | +		}, | 
|  | 160 | +		{ | 
|  | 161 | +			"pickup_zone": "POLYGON((13.3260 52.5700, 13.6550 52.5700, 13.6550 52.2700, " + | 
|  | 162 | +				"13.3260 52.2700, 13.3260 52.5700))", | 
|  | 163 | +			"store_location": "13.4050,52.5200", | 
|  | 164 | +			"brand":          "Peaknetic", | 
|  | 165 | +			"model":          "Secto", | 
|  | 166 | +			"price":          430, | 
|  | 167 | +			"description": "If you struggle with stiff fingers or a kinked neck or " + | 
|  | 168 | +				"back after a few minutes on the road, this lightweight, aluminum bike alleviates " + | 
|  | 169 | +				"those issues and allows you to enjoy the ride. From the ergonomic grips to the " + | 
|  | 170 | +				"lumbar-supporting seat position, the Roll Low-Entry offers incredible comfort. " + | 
|  | 171 | +				"The rear-inclined seat tube facilitates stability by allowing you to put a foot " + | 
|  | 172 | +				"on the ground to balance at a stop, and the low step-over frame makes it " + | 
|  | 173 | +				"accessible for all ability and mobility levels. The saddle is very soft, with " + | 
|  | 174 | +				"a wide back to support your hip joints and a cutout in the center to redistribute " + | 
|  | 175 | +				"that pressure. Rim brakes deliver satisfactory braking control, and the wide tires " + | 
|  | 176 | +				"provide a smooth, stable ride on paved roads and gravel. Rack and fender mounts " + | 
|  | 177 | +				"facilitate setting up the Roll Low-Entry as your preferred commuter, and the " + | 
|  | 178 | +				"BMX-like handlebar offers space for mounting a flashlight, bell, or phone holder.", | 
|  | 179 | +			"condition": "new", | 
|  | 180 | +		}, | 
|  | 181 | +		{ | 
|  | 182 | +			"pickup_zone": "POLYGON((1.9450 41.4301, 2.4018 41.4301, 2.4018 41.1987, " + | 
|  | 183 | +				"1.9450 41.1987, 1.9450 41.4301))", | 
|  | 184 | +			"store_location": "2.1734, 41.3851", | 
|  | 185 | +			"brand":          "nHill", | 
|  | 186 | +			"model":          "Summit", | 
|  | 187 | +			"price":          1200, | 
|  | 188 | +			"description": "This budget mountain bike from nHill performs well both " + | 
|  | 189 | +				"on bike paths and on the trail. The fork with 100mm of travel absorbs rough " + | 
|  | 190 | +				"terrain. Fat Kenda Booster tires give you grip in corners and on wet trails. " + | 
|  | 191 | +				"The Shimano Tourney drivetrain offered enough gears for finding a comfortable " + | 
|  | 192 | +				"pace to ride uphill, and the Tektro hydraulic disc brakes break smoothly. " + | 
|  | 193 | +				"Whether you want an affordable bike that you can take to work, but also take " + | 
|  | 194 | +				"trail in mountains on the weekends or you’re just after a stable, comfortable " + | 
|  | 195 | +				"ride for the bike path, the Summit gives a good value for money.", | 
|  | 196 | +			"condition": "new", | 
|  | 197 | +		}, | 
|  | 198 | +		{ | 
|  | 199 | +			"pickup_zone": "POLYGON((12.4464 42.1028, 12.5464 42.1028, " + | 
|  | 200 | +				"12.5464 41.7028, 12.4464 41.7028, 12.4464 42.1028))", | 
|  | 201 | +			"store_location": "12.4964,41.9028", | 
|  | 202 | +			"model":          "ThrillCycle", | 
|  | 203 | +			"brand":          "BikeShind", | 
|  | 204 | +			"price":          815, | 
|  | 205 | +			"description": "An artsy,  retro-inspired bicycle that’s as " + | 
|  | 206 | +				"functional as it is pretty: The ThrillCycle steel frame offers a smooth ride. " + | 
|  | 207 | +				"A 9-speed drivetrain has enough gears for coasting in the city, but we wouldn’t " + | 
|  | 208 | +				"suggest taking it to the mountains. Fenders protect you from mud, and a rear " + | 
|  | 209 | +				"basket lets you transport groceries, flowers and books. The ThrillCycle comes " + | 
|  | 210 | +				"with a limited lifetime warranty, so this little guy will last you long " + | 
|  | 211 | +				"past graduation.", | 
|  | 212 | +			"condition": "refurbished", | 
|  | 213 | +		}, | 
|  | 214 | +	} | 
|  | 215 | + | 
|  | 216 | +	for i, json := range exampleJsons { | 
|  | 217 | +		_, err := rdb.JSONSet(ctx, fmt.Sprintf("bicycle:%v", i), "$", json).Result() | 
|  | 218 | + | 
|  | 219 | +		if err != nil { | 
|  | 220 | +			panic(err) | 
|  | 221 | +		} | 
|  | 222 | +	} | 
|  | 223 | + | 
|  | 224 | +	// STEP_START range1 | 
|  | 225 | +	res1, err := rdb.FTSearchWithArgs(ctx, | 
|  | 226 | +		"idx:bicycle", "@price:[500 1000]", | 
|  | 227 | +		&redis.FTSearchOptions{ | 
|  | 228 | +			Return: []redis.FTSearchReturn{ | 
|  | 229 | +				{ | 
|  | 230 | +					FieldName: "price", | 
|  | 231 | +				}, | 
|  | 232 | +			}, | 
|  | 233 | +		}, | 
|  | 234 | +	).Result() | 
|  | 235 | + | 
|  | 236 | +	if err != nil { | 
|  | 237 | +		panic(err) | 
|  | 238 | +	} | 
|  | 239 | + | 
|  | 240 | +	fmt.Println(res1.Total) // >>> 3 | 
|  | 241 | + | 
|  | 242 | +	for _, doc := range res1.Docs { | 
|  | 243 | +		fmt.Printf("%v : price %v\n", doc.ID, doc.Fields["price"]) | 
|  | 244 | +	} | 
|  | 245 | +	// >>> bicycle:2 : price 815 | 
|  | 246 | +	// >>> bicycle:5 : price 810 | 
|  | 247 | +	// >>> bicycle:9 : price 815 | 
|  | 248 | +	// STEP_END | 
|  | 249 | + | 
|  | 250 | +	// STEP_START range2 | 
|  | 251 | +	res2, err := rdb.FTSearchWithArgs(ctx, | 
|  | 252 | +		"idx:bicycle", "*", | 
|  | 253 | +		&redis.FTSearchOptions{ | 
|  | 254 | +			Filters: []redis.FTSearchFilter{ | 
|  | 255 | +				{ | 
|  | 256 | +					FieldName: "price", | 
|  | 257 | +					Min:       500, | 
|  | 258 | +					Max:       1000, | 
|  | 259 | +				}, | 
|  | 260 | +			}, | 
|  | 261 | +			Return: []redis.FTSearchReturn{ | 
|  | 262 | +				{ | 
|  | 263 | +					FieldName: "price", | 
|  | 264 | +				}, | 
|  | 265 | +			}, | 
|  | 266 | +		}, | 
|  | 267 | +	).Result() | 
|  | 268 | + | 
|  | 269 | +	if err != nil { | 
|  | 270 | +		panic(err) | 
|  | 271 | +	} | 
|  | 272 | + | 
|  | 273 | +	fmt.Println(res2.Total) // >>> 3 | 
|  | 274 | + | 
|  | 275 | +	for _, doc := range res2.Docs { | 
|  | 276 | +		fmt.Printf("%v : price %v\n", doc.ID, doc.Fields["price"]) | 
|  | 277 | +	} | 
|  | 278 | +	// >>> bicycle:2 : price 815 | 
|  | 279 | +	// >>> bicycle:5 : price 810 | 
|  | 280 | +	// >>> bicycle:9 : price 815 | 
|  | 281 | +	// STEP_END | 
|  | 282 | + | 
|  | 283 | +	// STEP_START range3 | 
|  | 284 | +	res3, err := rdb.FTSearchWithArgs(ctx, | 
|  | 285 | +		"idx:bicycle", "*", | 
|  | 286 | +		&redis.FTSearchOptions{ | 
|  | 287 | +			Return: []redis.FTSearchReturn{ | 
|  | 288 | +				{ | 
|  | 289 | +					FieldName: "price", | 
|  | 290 | +				}, | 
|  | 291 | +			}, | 
|  | 292 | +			Filters: []redis.FTSearchFilter{ | 
|  | 293 | +				{ | 
|  | 294 | +					FieldName: "price", | 
|  | 295 | +					Min:       "(1000", | 
|  | 296 | +					Max:       "+inf", | 
|  | 297 | +				}, | 
|  | 298 | +			}, | 
|  | 299 | +		}, | 
|  | 300 | +	).Result() | 
|  | 301 | + | 
|  | 302 | +	if err != nil { | 
|  | 303 | +		panic(err) | 
|  | 304 | +	} | 
|  | 305 | + | 
|  | 306 | +	fmt.Println(res3.Total) // >>> 5 | 
|  | 307 | + | 
|  | 308 | +	for _, doc := range res3.Docs { | 
|  | 309 | +		fmt.Printf("%v : price %v\n", doc.ID, doc.Fields["price"]) | 
|  | 310 | +	} | 
|  | 311 | +	// >>> bicycle:1 : price 1200 | 
|  | 312 | +	// >>> bicycle:4 : price 3200 | 
|  | 313 | +	// >>> bicycle:6 : price 2300 | 
|  | 314 | +	// >>> bicycle:3 : price 3400 | 
|  | 315 | +	// >>> bicycle:8 : price 1200 | 
|  | 316 | +	// STEP_END | 
|  | 317 | + | 
|  | 318 | +	// STEP_START range4 | 
|  | 319 | +	res4, err := rdb.FTSearchWithArgs(ctx, | 
|  | 320 | +		"idx:bicycle", | 
|  | 321 | +		"@price:[-inf 2000]", | 
|  | 322 | +		&redis.FTSearchOptions{ | 
|  | 323 | +			Return: []redis.FTSearchReturn{ | 
|  | 324 | +				{ | 
|  | 325 | +					FieldName: "price", | 
|  | 326 | +				}, | 
|  | 327 | +			}, | 
|  | 328 | +			SortBy: []redis.FTSearchSortBy{ | 
|  | 329 | +				{ | 
|  | 330 | +					FieldName: "price", | 
|  | 331 | +					Asc:       true, | 
|  | 332 | +				}, | 
|  | 333 | +			}, | 
|  | 334 | +			LimitOffset: 0, | 
|  | 335 | +			Limit:       5, | 
|  | 336 | +		}, | 
|  | 337 | +	).Result() | 
|  | 338 | + | 
|  | 339 | +	if err != nil { | 
|  | 340 | +		panic(err) | 
|  | 341 | +	} | 
|  | 342 | + | 
|  | 343 | +	fmt.Println(res4.Total) // >>> 7 | 
|  | 344 | + | 
|  | 345 | +	for _, doc := range res4.Docs { | 
|  | 346 | +		fmt.Printf("%v : price %v\n", doc.ID, doc.Fields["price"]) | 
|  | 347 | +	} | 
|  | 348 | +	// >>> bicycle:0 : price 270 | 
|  | 349 | +	// >>> bicycle:7 : price 430 | 
|  | 350 | +	// >>> bicycle:5 : price 810 | 
|  | 351 | +	// >>> bicycle:2 : price 815 | 
|  | 352 | +	// >>> bicycle:9 : price 815 | 
|  | 353 | +	// STEP_END | 
|  | 354 | + | 
|  | 355 | +	// Output: | 
|  | 356 | +	// 3 | 
|  | 357 | +	// bicycle:2 : price 815 | 
|  | 358 | +	// bicycle:5 : price 810 | 
|  | 359 | +	// bicycle:9 : price 815 | 
|  | 360 | +	// 3 | 
|  | 361 | +	// bicycle:2 : price 815 | 
|  | 362 | +	// bicycle:5 : price 810 | 
|  | 363 | +	// bicycle:9 : price 815 | 
|  | 364 | +	// 5 | 
|  | 365 | +	// bicycle:1 : price 1200 | 
|  | 366 | +	// bicycle:4 : price 3200 | 
|  | 367 | +	// bicycle:6 : price 2300 | 
|  | 368 | +	// bicycle:3 : price 3400 | 
|  | 369 | +	// bicycle:8 : price 1200 | 
|  | 370 | +	// 7 | 
|  | 371 | +	// bicycle:0 : price 270 | 
|  | 372 | +	// bicycle:7 : price 430 | 
|  | 373 | +	// bicycle:5 : price 810 | 
|  | 374 | +	// bicycle:2 : price 815 | 
|  | 375 | +	// bicycle:9 : price 815 | 
|  | 376 | +} | 
0 commit comments