Skip to content

Commit d3800c4

Browse files
DOC-4734 added geo indexing examples
1 parent 0e3ea5f commit d3800c4

File tree

1 file changed

+205
-0
lines changed

1 file changed

+205
-0
lines changed

doctests/geo_index_test.go

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
// EXAMPLE: geoindex
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+
func ExampleClient_geoindex() {
15+
ctx := context.Background()
16+
17+
rdb := redis.NewClient(&redis.Options{
18+
Addr: "localhost:6379",
19+
Password: "", // no password docs
20+
DB: 0, // use default DB
21+
Protocol: 2,
22+
})
23+
// REMOVE_START
24+
rdb.FTDropIndex(ctx, "productidx")
25+
rdb.FTDropIndex(ctx, "geomidx")
26+
// REMOVE_END
27+
28+
// STEP_START create_geo_idx
29+
geoCreateResult, err := rdb.FTCreate(ctx,
30+
"productidx",
31+
&redis.FTCreateOptions{
32+
OnJSON: true,
33+
Prefix: []interface{}{"product:"},
34+
},
35+
&redis.FieldSchema{
36+
FieldName: "$.location",
37+
As: "location",
38+
FieldType: redis.SearchFieldTypeGeo,
39+
},
40+
).Result()
41+
42+
if err != nil {
43+
panic(err)
44+
}
45+
46+
fmt.Println(geoCreateResult) // >>> OK
47+
// STEP_END
48+
49+
// STEP_START add_geo_json
50+
prd46885 := map[string]interface{}{
51+
"description": "Navy Blue Slippers",
52+
"price": 45.99,
53+
"city": "Denver",
54+
"location": "-104.991531, 39.742043",
55+
}
56+
57+
gjResult1, err := rdb.JSONSet(ctx, "product:46885", "$", prd46885).Result()
58+
59+
if err != nil {
60+
panic(err)
61+
}
62+
63+
fmt.Println(gjResult1) // >>> OK
64+
65+
prd46886 := map[string]interface{}{
66+
"description": "Bright Green Socks",
67+
"price": 25.50,
68+
"city": "Fort Collins",
69+
"location": "-105.0618814,40.5150098",
70+
}
71+
72+
gjResult2, err := rdb.JSONSet(ctx, "product:46886", "$", prd46886).Result()
73+
74+
if err != nil {
75+
panic(err)
76+
}
77+
78+
fmt.Println(gjResult2) // >>> OK
79+
// STEP_END
80+
81+
// STEP_START geo_query
82+
geoQueryResult, err := rdb.FTSearch(ctx, "productidx",
83+
"@location:[-104.800644 38.846127 100 mi]",
84+
).Result()
85+
86+
if err != nil {
87+
panic(err)
88+
}
89+
90+
fmt.Println(geoQueryResult)
91+
// >>> {1 [{product:46885...
92+
// STEP_END
93+
94+
// STEP_START create_gshape_idx
95+
geomCreateResult, err := rdb.FTCreate(ctx, "geomidx",
96+
&redis.FTCreateOptions{
97+
OnJSON: true,
98+
Prefix: []interface{}{"shape:"},
99+
},
100+
&redis.FieldSchema{
101+
FieldName: "$.name",
102+
As: "name",
103+
FieldType: redis.SearchFieldTypeText,
104+
},
105+
&redis.FieldSchema{
106+
FieldName: "$.geom",
107+
As: "geom",
108+
FieldType: redis.SearchFieldTypeGeoShape,
109+
GeoShapeFieldType: "FLAT",
110+
},
111+
).Result()
112+
113+
if err != nil {
114+
panic(err)
115+
}
116+
117+
fmt.Println(geomCreateResult) // >>> OK
118+
// STEP_END
119+
120+
// STEP_START add_gshape_json
121+
shape1 := map[string]interface{}{
122+
"name": "Green Square",
123+
"geom": "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))",
124+
}
125+
126+
gmjResult1, err := rdb.JSONSet(ctx, "shape:1", "$", shape1).Result()
127+
128+
if err != nil {
129+
panic(err)
130+
}
131+
132+
fmt.Println(gmjResult1) // >>> OK
133+
134+
shape2 := map[string]interface{}{
135+
"name": "Red Rectangle",
136+
"geom": "POLYGON ((2 2.5, 2 3.5, 3.5 3.5, 3.5 2.5, 2 2.5))",
137+
}
138+
139+
gmjResult2, err := rdb.JSONSet(ctx, "shape:2", "$", shape2).Result()
140+
141+
if err != nil {
142+
panic(err)
143+
}
144+
145+
fmt.Println(gmjResult2) // >>> OK
146+
147+
shape3 := map[string]interface{}{
148+
"name": "Blue Triangle",
149+
"geom": "POLYGON ((3.5 1, 3.75 2, 4 1, 3.5 1))",
150+
}
151+
152+
gmjResult3, err := rdb.JSONSet(ctx, "shape:3", "$", shape3).Result()
153+
154+
if err != nil {
155+
panic(err)
156+
}
157+
158+
fmt.Println(gmjResult3) // >>> OK
159+
160+
shape4 := map[string]interface{}{
161+
"name": "Purple Point",
162+
"geom": "POINT (2 2)",
163+
}
164+
165+
gmjResult4, err := rdb.JSONSet(ctx, "shape:4", "$", shape4).Result()
166+
167+
if err != nil {
168+
panic(err)
169+
}
170+
171+
fmt.Println(gmjResult4) // >>> OK
172+
// STEP_END
173+
174+
// STEP_START gshape_query
175+
geomQueryResult, err := rdb.FTSearchWithArgs(ctx, "geomidx",
176+
"(-@name:(Green Square) @geom:[WITHIN $qshape])",
177+
&redis.FTSearchOptions{
178+
Params: map[string]interface{}{
179+
"qshape": "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))",
180+
},
181+
DialectVersion: 4,
182+
Limit: 1,
183+
},
184+
).Result()
185+
186+
if err != nil {
187+
panic(err)
188+
}
189+
190+
fmt.Println(geomQueryResult)
191+
// >>> {1 [{shape:4...
192+
// STEP_END
193+
194+
// Output:
195+
// OK
196+
// OK
197+
// OK
198+
// {1 [{product:46885 <nil> <nil> <nil> map[$:{"city":"Denver","description":"Navy Blue Slippers","location":"-104.991531, 39.742043","price":45.99}]}]}
199+
// OK
200+
// OK
201+
// OK
202+
// OK
203+
// OK
204+
// {1 [{shape:4 <nil> <nil> <nil> map[$:[{"geom":"POINT (2 2)","name":"Purple Point"}]]}]}
205+
}

0 commit comments

Comments
 (0)