MCPcopy
hub / github.com/redis/go-redis / ExampleClient_geoindex

Function ExampleClient_geoindex

doctests/geo_index_test.go:14–209  ·  view source on GitHub ↗

HIDE_END

()

Source from the content-addressed store, hash-verified

12// HIDE_END
13
14func 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
24 // REMOVE_START
25 // make sure we are working with fresh database
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 // REMOVE_END
31
32 // STEP_START create_geo_idx
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) // >>> OK
51 // STEP_END
52
53 // STEP_START add_geo_json
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) // >>> OK
68
69 prd46886 := map[string]interface{}{
70 "description": "Bright Green Socks",
71 "price": 25.50,

Callers

nothing calls this directly

Calls 8

FlushDBMethod · 0.65
FTDropIndexMethod · 0.65
DelMethod · 0.65
ResultMethod · 0.65
FTCreateMethod · 0.65
JSONSetMethod · 0.65
FTSearchMethod · 0.65
FTSearchWithArgsMethod · 0.65

Tested by

no test coverage detected