Skip to content

Commit 20520dd

Browse files
DOC-4331 added full text query examples
1 parent 1b4abd6 commit 20520dd

File tree

1 file changed

+323
-0
lines changed

1 file changed

+323
-0
lines changed

doctests/query_ft_test.go

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

0 commit comments

Comments
 (0)