Skip to content

Commit 2418111

Browse files
DOC-4332 added geo query examples (#3257)
Co-authored-by: Nedyalko Dyakov <[email protected]>
1 parent fb809bb commit 2418111

File tree

1 file changed

+327
-0
lines changed

1 file changed

+327
-0
lines changed

doctests/query_geo_test.go

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

0 commit comments

Comments
 (0)