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