Skip to content

Commit 3cef280

Browse files
DOC-4734 added geo indexing examples (#3240)
* DOC-4734 added geo indexing examples * DOC-4734 delete keys before starting tests --------- Co-authored-by: Nedyalko Dyakov <nedyalko.dyakov@gmail.com>
1 parent 0b34b19 commit 3cef280

File tree

1 file changed

+206
-0
lines changed

1 file changed

+206
-0
lines changed

doctests/geo_index_test.go

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

0 commit comments

Comments
 (0)