...

Source file src/github.com/redis/go-redis/v9/doctests/geo_index_test.go

Documentation: github.com/redis/go-redis/v9/doctests

     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  
    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,
    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) // >>> OK
    83  	// STEP_END
    84  
    85  	// STEP_START geo_query
    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  	// >>> {1 [{product:46885...
    96  	// STEP_END
    97  
    98  	// STEP_START create_gshape_idx
    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) // >>> OK
   122  	// STEP_END
   123  
   124  	// STEP_START add_gshape_json
   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) // >>> OK
   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) // >>> OK
   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) // >>> OK
   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) // >>> OK
   176  	// STEP_END
   177  
   178  	// STEP_START gshape_query
   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  	// >>> {1 [{shape:4...
   196  	// STEP_END
   197  
   198  	// Output:
   199  	// OK
   200  	// OK
   201  	// OK
   202  	// {1 [{product:46885 <nil> <nil> <nil> map[$:{"city":"Denver","description":"Navy Blue Slippers","location":"-104.991531, 39.742043","price":45.99}] <nil>}]}
   203  	// OK
   204  	// OK
   205  	// OK
   206  	// OK
   207  	// OK
   208  	// {1 [{shape:4 <nil> <nil> <nil> map[$:[{"geom":"POINT (2 2)","name":"Purple Point"}]] <nil>}]}
   209  }
   210  

View as plain text