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