Geospatial indexing
Options for indexing geospatial data
Redis supports two different schema types for geospatial data:
GEO
: This uses a simple format where individual geospatial points are specified as numeric longitude-latitude pairs.GEOSHAPE
: This uses a subset of the Well-Known Text (WKT) format to specify both points and polygons using either geographical coordinates or Cartesian coordinates.
The sections below explain how to index these schema types. See the Geospatial reference page for a full description of both types.
GEO
The following command creates a GEO
index for JSON objects that contain
the geospatial data in a field called location
:
> FT.CREATE productidx ON JSON PREFIX 1 product: SCHEMA $.location AS location GEO
OK
import org.json.JSONObject;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.json.Path2;
import redis.clients.jedis.search.Document;
import redis.clients.jedis.search.FTCreateParams;
import redis.clients.jedis.search.FTSearchParams;
import redis.clients.jedis.search.IndexDataType;
import redis.clients.jedis.search.schemafields.*;
import redis.clients.jedis.search.schemafields.GeoShapeField.CoordinateSystem;
import redis.clients.jedis.search.SearchResult;
import redis.clients.jedis.exceptions.JedisDataException;
public class GeoIndexExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
SchemaField[] geoSchema = {
GeoField.of("$.location").as("location")
};
String geoIdxCreateResult = jedis.ftCreate("productidx",
FTCreateParams.createParams()
.on(IndexDataType.JSON)
.addPrefix("product:"),
geoSchema
);
JSONObject prd46885 = new JSONObject()
.put("description", "Navy Blue Slippers")
.put("price", 45.99)
.put("city", "Denver")
.put("location", "-104.991531, 39.742043");
String jsonAddResult1 = jedis.jsonSet("product:46885", new Path2("$"), prd46885);
System.out.println(jsonAddResult1); // >>> OK
JSONObject prd46886 = new JSONObject()
.put("description", "Bright Green Socks")
.put("price", 25.50)
.put("city", "Fort Collins")
.put("location", "-105.0618814,40.5150098");
String jsonAddResult2 = jedis.jsonSet("product:46886", new Path2("$"), prd46886);
System.out.println(jsonAddResult2); // >>> OK
SearchResult geoResult = jedis.ftSearch("productidx",
"@location:[-104.800644 38.846127 100 mi]"
);
System.out.println(geoResult.getTotalResults()); // >>> 1
for (Document doc: geoResult.getDocuments()) {
System.out.println(doc.getId());
}
// >>> product:46885
SchemaField[] geomSchema = {
TextField.of("$.name").as("name"),
GeoShapeField.of("$.geom", CoordinateSystem.FLAT).as("geom")
};
String geomIndexCreateResult = jedis.ftCreate("geomidx",
FTCreateParams.createParams()
.on(IndexDataType.JSON)
.addPrefix("shape"),
geomSchema
);
System.out.println(geomIndexCreateResult); // >>> OK
JSONObject shape1 = new JSONObject()
.put("name", "Green Square")
.put("geom", "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))");
String gmJsonRes1 = jedis.jsonSet("shape:1", new Path2("$"), shape1);
System.out.println(gmJsonRes1); // >>> OK
JSONObject shape2 = new JSONObject()
.put("name", "Red Rectangle")
.put("geom", "POLYGON ((2 2.5, 2 3.5, 3.5 3.5, 3.5 2.5, 2 2.5))");
String gmJsonRes2 = jedis.jsonSet("shape:2", new Path2("$"), shape2);
System.out.println(gmJsonRes2); // >>> OK
JSONObject shape3 = new JSONObject()
.put("name", "Blue Triangle")
.put("geom", "POLYGON ((3.5 1, 3.75 2, 4 1, 3.5 1))");
String gmJsonRes3 = jedis.jsonSet("shape:3", new Path2("$"), shape3);
System.out.println(gmJsonRes3); // >>> OK
JSONObject shape4 = new JSONObject()
.put("name", "Purple Point")
.put("geom", "POINT (2 2)");
String gmJsonRes4 = jedis.jsonSet("shape:4", new Path2("$"), shape4);
System.out.println(gmJsonRes4); // >>> OK
SearchResult geomResult = jedis.ftSearch("geomidx",
"(-@name:(Green Square) @geom:[WITHIN $qshape])",
FTSearchParams.searchParams()
.addParam("qshape", "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))")
.dialect(4)
.limit(0, 1)
);
System.out.println(geomResult.getTotalResults()); // >>> 1
for (Document doc: geomResult.getDocuments()) {
System.out.println(doc.getId());
}
// shape:4
jedis.close();
}
}
package example_commands_test
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
func ExampleClient_geoindex() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
Protocol: 2,
})
geoCreateResult, err := rdb.FTCreate(ctx,
"productidx",
&redis.FTCreateOptions{
OnJSON: true,
Prefix: []interface{}{"product:"},
},
&redis.FieldSchema{
FieldName: "$.location",
As: "location",
FieldType: redis.SearchFieldTypeGeo,
},
).Result()
if err != nil {
panic(err)
}
fmt.Println(geoCreateResult) // >>> OK
prd46885 := map[string]interface{}{
"description": "Navy Blue Slippers",
"price": 45.99,
"city": "Denver",
"location": "-104.991531, 39.742043",
}
gjResult1, err := rdb.JSONSet(ctx, "product:46885", "$", prd46885).Result()
if err != nil {
panic(err)
}
fmt.Println(gjResult1) // >>> OK
prd46886 := map[string]interface{}{
"description": "Bright Green Socks",
"price": 25.50,
"city": "Fort Collins",
"location": "-105.0618814,40.5150098",
}
gjResult2, err := rdb.JSONSet(ctx, "product:46886", "$", prd46886).Result()
if err != nil {
panic(err)
}
fmt.Println(gjResult2) // >>> OK
geoQueryResult, err := rdb.FTSearch(ctx, "productidx",
"@location:[-104.800644 38.846127 100 mi]",
).Result()
if err != nil {
panic(err)
}
fmt.Println(geoQueryResult)
// >>> {1 [{product:46885...
geomCreateResult, err := rdb.FTCreate(ctx, "geomidx",
&redis.FTCreateOptions{
OnJSON: true,
Prefix: []interface{}{"shape:"},
},
&redis.FieldSchema{
FieldName: "$.name",
As: "name",
FieldType: redis.SearchFieldTypeText,
},
&redis.FieldSchema{
FieldName: "$.geom",
As: "geom",
FieldType: redis.SearchFieldTypeGeoShape,
GeoShapeFieldType: "FLAT",
},
).Result()
if err != nil {
panic(err)
}
fmt.Println(geomCreateResult) // >>> OK
shape1 := map[string]interface{}{
"name": "Green Square",
"geom": "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))",
}
gmjResult1, err := rdb.JSONSet(ctx, "shape:1", "$", shape1).Result()
if err != nil {
panic(err)
}
fmt.Println(gmjResult1) // >>> OK
shape2 := map[string]interface{}{
"name": "Red Rectangle",
"geom": "POLYGON ((2 2.5, 2 3.5, 3.5 3.5, 3.5 2.5, 2 2.5))",
}
gmjResult2, err := rdb.JSONSet(ctx, "shape:2", "$", shape2).Result()
if err != nil {
panic(err)
}
fmt.Println(gmjResult2) // >>> OK
shape3 := map[string]interface{}{
"name": "Blue Triangle",
"geom": "POLYGON ((3.5 1, 3.75 2, 4 1, 3.5 1))",
}
gmjResult3, err := rdb.JSONSet(ctx, "shape:3", "$", shape3).Result()
if err != nil {
panic(err)
}
fmt.Println(gmjResult3) // >>> OK
shape4 := map[string]interface{}{
"name": "Purple Point",
"geom": "POINT (2 2)",
}
gmjResult4, err := rdb.JSONSet(ctx, "shape:4", "$", shape4).Result()
if err != nil {
panic(err)
}
fmt.Println(gmjResult4) // >>> OK
geomQueryResult, err := rdb.FTSearchWithArgs(ctx, "geomidx",
"(-@name:(Green Square) @geom:[WITHIN $qshape])",
&redis.FTSearchOptions{
Params: map[string]interface{}{
"qshape": "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))",
},
DialectVersion: 4,
Limit: 1,
},
).Result()
if err != nil {
panic(err)
}
fmt.Println(geomQueryResult)
// >>> {1 [{shape:4...
}
If you now add JSON objects with the product:
prefix and a location
field,
they will be added to the index automatically:
> JSON.SET product:46885 $ '{"description": "Navy Blue Slippers","price": 45.99,"city": "Denver","location": "-104.991531, 39.742043"}'
OK
> JSON.SET product:46886 $ '{"description": "Bright Green Socks","price": 25.50,"city": "Fort Collins","location": "-105.0618814,40.5150098"}'
OK
import org.json.JSONObject;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.json.Path2;
import redis.clients.jedis.search.Document;
import redis.clients.jedis.search.FTCreateParams;
import redis.clients.jedis.search.FTSearchParams;
import redis.clients.jedis.search.IndexDataType;
import redis.clients.jedis.search.schemafields.*;
import redis.clients.jedis.search.schemafields.GeoShapeField.CoordinateSystem;
import redis.clients.jedis.search.SearchResult;
import redis.clients.jedis.exceptions.JedisDataException;
public class GeoIndexExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
SchemaField[] geoSchema = {
GeoField.of("$.location").as("location")
};
String geoIdxCreateResult = jedis.ftCreate("productidx",
FTCreateParams.createParams()
.on(IndexDataType.JSON)
.addPrefix("product:"),
geoSchema
);
JSONObject prd46885 = new JSONObject()
.put("description", "Navy Blue Slippers")
.put("price", 45.99)
.put("city", "Denver")
.put("location", "-104.991531, 39.742043");
String jsonAddResult1 = jedis.jsonSet("product:46885", new Path2("$"), prd46885);
System.out.println(jsonAddResult1); // >>> OK
JSONObject prd46886 = new JSONObject()
.put("description", "Bright Green Socks")
.put("price", 25.50)
.put("city", "Fort Collins")
.put("location", "-105.0618814,40.5150098");
String jsonAddResult2 = jedis.jsonSet("product:46886", new Path2("$"), prd46886);
System.out.println(jsonAddResult2); // >>> OK
SearchResult geoResult = jedis.ftSearch("productidx",
"@location:[-104.800644 38.846127 100 mi]"
);
System.out.println(geoResult.getTotalResults()); // >>> 1
for (Document doc: geoResult.getDocuments()) {
System.out.println(doc.getId());
}
// >>> product:46885
SchemaField[] geomSchema = {
TextField.of("$.name").as("name"),
GeoShapeField.of("$.geom", CoordinateSystem.FLAT).as("geom")
};
String geomIndexCreateResult = jedis.ftCreate("geomidx",
FTCreateParams.createParams()
.on(IndexDataType.JSON)
.addPrefix("shape"),
geomSchema
);
System.out.println(geomIndexCreateResult); // >>> OK
JSONObject shape1 = new JSONObject()
.put("name", "Green Square")
.put("geom", "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))");
String gmJsonRes1 = jedis.jsonSet("shape:1", new Path2("$"), shape1);
System.out.println(gmJsonRes1); // >>> OK
JSONObject shape2 = new JSONObject()
.put("name", "Red Rectangle")
.put("geom", "POLYGON ((2 2.5, 2 3.5, 3.5 3.5, 3.5 2.5, 2 2.5))");
String gmJsonRes2 = jedis.jsonSet("shape:2", new Path2("$"), shape2);
System.out.println(gmJsonRes2); // >>> OK
JSONObject shape3 = new JSONObject()
.put("name", "Blue Triangle")
.put("geom", "POLYGON ((3.5 1, 3.75 2, 4 1, 3.5 1))");
String gmJsonRes3 = jedis.jsonSet("shape:3", new Path2("$"), shape3);
System.out.println(gmJsonRes3); // >>> OK
JSONObject shape4 = new JSONObject()
.put("name", "Purple Point")
.put("geom", "POINT (2 2)");
String gmJsonRes4 = jedis.jsonSet("shape:4", new Path2("$"), shape4);
System.out.println(gmJsonRes4); // >>> OK
SearchResult geomResult = jedis.ftSearch("geomidx",
"(-@name:(Green Square) @geom:[WITHIN $qshape])",
FTSearchParams.searchParams()
.addParam("qshape", "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))")
.dialect(4)
.limit(0, 1)
);
System.out.println(geomResult.getTotalResults()); // >>> 1
for (Document doc: geomResult.getDocuments()) {
System.out.println(doc.getId());
}
// shape:4
jedis.close();
}
}
package example_commands_test
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
func ExampleClient_geoindex() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
Protocol: 2,
})
geoCreateResult, err := rdb.FTCreate(ctx,
"productidx",
&redis.FTCreateOptions{
OnJSON: true,
Prefix: []interface{}{"product:"},
},
&redis.FieldSchema{
FieldName: "$.location",
As: "location",
FieldType: redis.SearchFieldTypeGeo,
},
).Result()
if err != nil {
panic(err)
}
fmt.Println(geoCreateResult) // >>> OK
prd46885 := map[string]interface{}{
"description": "Navy Blue Slippers",
"price": 45.99,
"city": "Denver",
"location": "-104.991531, 39.742043",
}
gjResult1, err := rdb.JSONSet(ctx, "product:46885", "$", prd46885).Result()
if err != nil {
panic(err)
}
fmt.Println(gjResult1) // >>> OK
prd46886 := map[string]interface{}{
"description": "Bright Green Socks",
"price": 25.50,
"city": "Fort Collins",
"location": "-105.0618814,40.5150098",
}
gjResult2, err := rdb.JSONSet(ctx, "product:46886", "$", prd46886).Result()
if err != nil {
panic(err)
}
fmt.Println(gjResult2) // >>> OK
geoQueryResult, err := rdb.FTSearch(ctx, "productidx",
"@location:[-104.800644 38.846127 100 mi]",
).Result()
if err != nil {
panic(err)
}
fmt.Println(geoQueryResult)
// >>> {1 [{product:46885...
geomCreateResult, err := rdb.FTCreate(ctx, "geomidx",
&redis.FTCreateOptions{
OnJSON: true,
Prefix: []interface{}{"shape:"},
},
&redis.FieldSchema{
FieldName: "$.name",
As: "name",
FieldType: redis.SearchFieldTypeText,
},
&redis.FieldSchema{
FieldName: "$.geom",
As: "geom",
FieldType: redis.SearchFieldTypeGeoShape,
GeoShapeFieldType: "FLAT",
},
).Result()
if err != nil {
panic(err)
}
fmt.Println(geomCreateResult) // >>> OK
shape1 := map[string]interface{}{
"name": "Green Square",
"geom": "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))",
}
gmjResult1, err := rdb.JSONSet(ctx, "shape:1", "$", shape1).Result()
if err != nil {
panic(err)
}
fmt.Println(gmjResult1) // >>> OK
shape2 := map[string]interface{}{
"name": "Red Rectangle",
"geom": "POLYGON ((2 2.5, 2 3.5, 3.5 3.5, 3.5 2.5, 2 2.5))",
}
gmjResult2, err := rdb.JSONSet(ctx, "shape:2", "$", shape2).Result()
if err != nil {
panic(err)
}
fmt.Println(gmjResult2) // >>> OK
shape3 := map[string]interface{}{
"name": "Blue Triangle",
"geom": "POLYGON ((3.5 1, 3.75 2, 4 1, 3.5 1))",
}
gmjResult3, err := rdb.JSONSet(ctx, "shape:3", "$", shape3).Result()
if err != nil {
panic(err)
}
fmt.Println(gmjResult3) // >>> OK
shape4 := map[string]interface{}{
"name": "Purple Point",
"geom": "POINT (2 2)",
}
gmjResult4, err := rdb.JSONSet(ctx, "shape:4", "$", shape4).Result()
if err != nil {
panic(err)
}
fmt.Println(gmjResult4) // >>> OK
geomQueryResult, err := rdb.FTSearchWithArgs(ctx, "geomidx",
"(-@name:(Green Square) @geom:[WITHIN $qshape])",
&redis.FTSearchOptions{
Params: map[string]interface{}{
"qshape": "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))",
},
DialectVersion: 4,
Limit: 1,
},
).Result()
if err != nil {
panic(err)
}
fmt.Println(geomQueryResult)
// >>> {1 [{shape:4...
}
The query below finds products within a 100 mile radius of Colorado Springs (Longitude=-104.800644, Latitude=38.846127). This returns only the location in Denver, but a radius of 200 miles would also include the location in Fort Collins:
> FT.SEARCH productidx '@location:[-104.800644 38.846127 100 mi]'
1) "1"
2) "product:46885"
3) 1) "$"
2) "{\"description\":\"Navy Blue Slippers\",\"price\":45.99,\"city\":\"Denver\",\"location\":\"-104.991531, 39.742043\"}"
import org.json.JSONObject;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.json.Path2;
import redis.clients.jedis.search.Document;
import redis.clients.jedis.search.FTCreateParams;
import redis.clients.jedis.search.FTSearchParams;
import redis.clients.jedis.search.IndexDataType;
import redis.clients.jedis.search.schemafields.*;
import redis.clients.jedis.search.schemafields.GeoShapeField.CoordinateSystem;
import redis.clients.jedis.search.SearchResult;
import redis.clients.jedis.exceptions.JedisDataException;
public class GeoIndexExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
SchemaField[] geoSchema = {
GeoField.of("$.location").as("location")
};
String geoIdxCreateResult = jedis.ftCreate("productidx",
FTCreateParams.createParams()
.on(IndexDataType.JSON)
.addPrefix("product:"),
geoSchema
);
JSONObject prd46885 = new JSONObject()
.put("description", "Navy Blue Slippers")
.put("price", 45.99)
.put("city", "Denver")
.put("location", "-104.991531, 39.742043");
String jsonAddResult1 = jedis.jsonSet("product:46885", new Path2("$"), prd46885);
System.out.println(jsonAddResult1); // >>> OK
JSONObject prd46886 = new JSONObject()
.put("description", "Bright Green Socks")
.put("price", 25.50)
.put("city", "Fort Collins")
.put("location", "-105.0618814,40.5150098");
String jsonAddResult2 = jedis.jsonSet("product:46886", new Path2("$"), prd46886);
System.out.println(jsonAddResult2); // >>> OK
SearchResult geoResult = jedis.ftSearch("productidx",
"@location:[-104.800644 38.846127 100 mi]"
);
System.out.println(geoResult.getTotalResults()); // >>> 1
for (Document doc: geoResult.getDocuments()) {
System.out.println(doc.getId());
}
// >>> product:46885
SchemaField[] geomSchema = {
TextField.of("$.name").as("name"),
GeoShapeField.of("$.geom", CoordinateSystem.FLAT).as("geom")
};
String geomIndexCreateResult = jedis.ftCreate("geomidx",
FTCreateParams.createParams()
.on(IndexDataType.JSON)
.addPrefix("shape"),
geomSchema
);
System.out.println(geomIndexCreateResult); // >>> OK
JSONObject shape1 = new JSONObject()
.put("name", "Green Square")
.put("geom", "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))");
String gmJsonRes1 = jedis.jsonSet("shape:1", new Path2("$"), shape1);
System.out.println(gmJsonRes1); // >>> OK
JSONObject shape2 = new JSONObject()
.put("name", "Red Rectangle")
.put("geom", "POLYGON ((2 2.5, 2 3.5, 3.5 3.5, 3.5 2.5, 2 2.5))");
String gmJsonRes2 = jedis.jsonSet("shape:2", new Path2("$"), shape2);
System.out.println(gmJsonRes2); // >>> OK
JSONObject shape3 = new JSONObject()
.put("name", "Blue Triangle")
.put("geom", "POLYGON ((3.5 1, 3.75 2, 4 1, 3.5 1))");
String gmJsonRes3 = jedis.jsonSet("shape:3", new Path2("$"), shape3);
System.out.println(gmJsonRes3); // >>> OK
JSONObject shape4 = new JSONObject()
.put("name", "Purple Point")
.put("geom", "POINT (2 2)");
String gmJsonRes4 = jedis.jsonSet("shape:4", new Path2("$"), shape4);
System.out.println(gmJsonRes4); // >>> OK
SearchResult geomResult = jedis.ftSearch("geomidx",
"(-@name:(Green Square) @geom:[WITHIN $qshape])",
FTSearchParams.searchParams()
.addParam("qshape", "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))")
.dialect(4)
.limit(0, 1)
);
System.out.println(geomResult.getTotalResults()); // >>> 1
for (Document doc: geomResult.getDocuments()) {
System.out.println(doc.getId());
}
// shape:4
jedis.close();
}
}
package example_commands_test
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
func ExampleClient_geoindex() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
Protocol: 2,
})
geoCreateResult, err := rdb.FTCreate(ctx,
"productidx",
&redis.FTCreateOptions{
OnJSON: true,
Prefix: []interface{}{"product:"},
},
&redis.FieldSchema{
FieldName: "$.location",
As: "location",
FieldType: redis.SearchFieldTypeGeo,
},
).Result()
if err != nil {
panic(err)
}
fmt.Println(geoCreateResult) // >>> OK
prd46885 := map[string]interface{}{
"description": "Navy Blue Slippers",
"price": 45.99,
"city": "Denver",
"location": "-104.991531, 39.742043",
}
gjResult1, err := rdb.JSONSet(ctx, "product:46885", "$", prd46885).Result()
if err != nil {
panic(err)
}
fmt.Println(gjResult1) // >>> OK
prd46886 := map[string]interface{}{
"description": "Bright Green Socks",
"price": 25.50,
"city": "Fort Collins",
"location": "-105.0618814,40.5150098",
}
gjResult2, err := rdb.JSONSet(ctx, "product:46886", "$", prd46886).Result()
if err != nil {
panic(err)
}
fmt.Println(gjResult2) // >>> OK
geoQueryResult, err := rdb.FTSearch(ctx, "productidx",
"@location:[-104.800644 38.846127 100 mi]",
).Result()
if err != nil {
panic(err)
}
fmt.Println(geoQueryResult)
// >>> {1 [{product:46885...
geomCreateResult, err := rdb.FTCreate(ctx, "geomidx",
&redis.FTCreateOptions{
OnJSON: true,
Prefix: []interface{}{"shape:"},
},
&redis.FieldSchema{
FieldName: "$.name",
As: "name",
FieldType: redis.SearchFieldTypeText,
},
&redis.FieldSchema{
FieldName: "$.geom",
As: "geom",
FieldType: redis.SearchFieldTypeGeoShape,
GeoShapeFieldType: "FLAT",
},
).Result()
if err != nil {
panic(err)
}
fmt.Println(geomCreateResult) // >>> OK
shape1 := map[string]interface{}{
"name": "Green Square",
"geom": "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))",
}
gmjResult1, err := rdb.JSONSet(ctx, "shape:1", "$", shape1).Result()
if err != nil {
panic(err)
}
fmt.Println(gmjResult1) // >>> OK
shape2 := map[string]interface{}{
"name": "Red Rectangle",
"geom": "POLYGON ((2 2.5, 2 3.5, 3.5 3.5, 3.5 2.5, 2 2.5))",
}
gmjResult2, err := rdb.JSONSet(ctx, "shape:2", "$", shape2).Result()
if err != nil {
panic(err)
}
fmt.Println(gmjResult2) // >>> OK
shape3 := map[string]interface{}{
"name": "Blue Triangle",
"geom": "POLYGON ((3.5 1, 3.75 2, 4 1, 3.5 1))",
}
gmjResult3, err := rdb.JSONSet(ctx, "shape:3", "$", shape3).Result()
if err != nil {
panic(err)
}
fmt.Println(gmjResult3) // >>> OK
shape4 := map[string]interface{}{
"name": "Purple Point",
"geom": "POINT (2 2)",
}
gmjResult4, err := rdb.JSONSet(ctx, "shape:4", "$", shape4).Result()
if err != nil {
panic(err)
}
fmt.Println(gmjResult4) // >>> OK
geomQueryResult, err := rdb.FTSearchWithArgs(ctx, "geomidx",
"(-@name:(Green Square) @geom:[WITHIN $qshape])",
&redis.FTSearchOptions{
Params: map[string]interface{}{
"qshape": "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))",
},
DialectVersion: 4,
Limit: 1,
},
).Result()
if err != nil {
panic(err)
}
fmt.Println(geomQueryResult)
// >>> {1 [{shape:4...
}
See Geospatial queries for more information about the available options.
GEOSHAPE
The following command creates an index for JSON objects that include
geospatial data in a field called geom
. The FLAT
option at the end
of the field definition specifies Cartesian coordinates instead of
the default spherical geographical coordinates. Use SPHERICAL
in
place of FLAT
to choose the coordinate space explicitly.
> FT.CREATE geomidx ON JSON PREFIX 1 shape: SCHEMA $.name AS name TEXT $.geom AS geom GEOSHAPE FLAT
OK
import org.json.JSONObject;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.json.Path2;
import redis.clients.jedis.search.Document;
import redis.clients.jedis.search.FTCreateParams;
import redis.clients.jedis.search.FTSearchParams;
import redis.clients.jedis.search.IndexDataType;
import redis.clients.jedis.search.schemafields.*;
import redis.clients.jedis.search.schemafields.GeoShapeField.CoordinateSystem;
import redis.clients.jedis.search.SearchResult;
import redis.clients.jedis.exceptions.JedisDataException;
public class GeoIndexExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
SchemaField[] geoSchema = {
GeoField.of("$.location").as("location")
};
String geoIdxCreateResult = jedis.ftCreate("productidx",
FTCreateParams.createParams()
.on(IndexDataType.JSON)
.addPrefix("product:"),
geoSchema
);
JSONObject prd46885 = new JSONObject()
.put("description", "Navy Blue Slippers")
.put("price", 45.99)
.put("city", "Denver")
.put("location", "-104.991531, 39.742043");
String jsonAddResult1 = jedis.jsonSet("product:46885", new Path2("$"), prd46885);
System.out.println(jsonAddResult1); // >>> OK
JSONObject prd46886 = new JSONObject()
.put("description", "Bright Green Socks")
.put("price", 25.50)
.put("city", "Fort Collins")
.put("location", "-105.0618814,40.5150098");
String jsonAddResult2 = jedis.jsonSet("product:46886", new Path2("$"), prd46886);
System.out.println(jsonAddResult2); // >>> OK
SearchResult geoResult = jedis.ftSearch("productidx",
"@location:[-104.800644 38.846127 100 mi]"
);
System.out.println(geoResult.getTotalResults()); // >>> 1
for (Document doc: geoResult.getDocuments()) {
System.out.println(doc.getId());
}
// >>> product:46885
SchemaField[] geomSchema = {
TextField.of("$.name").as("name"),
GeoShapeField.of("$.geom", CoordinateSystem.FLAT).as("geom")
};
String geomIndexCreateResult = jedis.ftCreate("geomidx",
FTCreateParams.createParams()
.on(IndexDataType.JSON)
.addPrefix("shape"),
geomSchema
);
System.out.println(geomIndexCreateResult); // >>> OK
JSONObject shape1 = new JSONObject()
.put("name", "Green Square")
.put("geom", "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))");
String gmJsonRes1 = jedis.jsonSet("shape:1", new Path2("$"), shape1);
System.out.println(gmJsonRes1); // >>> OK
JSONObject shape2 = new JSONObject()
.put("name", "Red Rectangle")
.put("geom", "POLYGON ((2 2.5, 2 3.5, 3.5 3.5, 3.5 2.5, 2 2.5))");
String gmJsonRes2 = jedis.jsonSet("shape:2", new Path2("$"), shape2);
System.out.println(gmJsonRes2); // >>> OK
JSONObject shape3 = new JSONObject()
.put("name", "Blue Triangle")
.put("geom", "POLYGON ((3.5 1, 3.75 2, 4 1, 3.5 1))");
String gmJsonRes3 = jedis.jsonSet("shape:3", new Path2("$"), shape3);
System.out.println(gmJsonRes3); // >>> OK
JSONObject shape4 = new JSONObject()
.put("name", "Purple Point")
.put("geom", "POINT (2 2)");
String gmJsonRes4 = jedis.jsonSet("shape:4", new Path2("$"), shape4);
System.out.println(gmJsonRes4); // >>> OK
SearchResult geomResult = jedis.ftSearch("geomidx",
"(-@name:(Green Square) @geom:[WITHIN $qshape])",
FTSearchParams.searchParams()
.addParam("qshape", "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))")
.dialect(4)
.limit(0, 1)
);
System.out.println(geomResult.getTotalResults()); // >>> 1
for (Document doc: geomResult.getDocuments()) {
System.out.println(doc.getId());
}
// shape:4
jedis.close();
}
}
package example_commands_test
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
func ExampleClient_geoindex() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
Protocol: 2,
})
geoCreateResult, err := rdb.FTCreate(ctx,
"productidx",
&redis.FTCreateOptions{
OnJSON: true,
Prefix: []interface{}{"product:"},
},
&redis.FieldSchema{
FieldName: "$.location",
As: "location",
FieldType: redis.SearchFieldTypeGeo,
},
).Result()
if err != nil {
panic(err)
}
fmt.Println(geoCreateResult) // >>> OK
prd46885 := map[string]interface{}{
"description": "Navy Blue Slippers",
"price": 45.99,
"city": "Denver",
"location": "-104.991531, 39.742043",
}
gjResult1, err := rdb.JSONSet(ctx, "product:46885", "$", prd46885).Result()
if err != nil {
panic(err)
}
fmt.Println(gjResult1) // >>> OK
prd46886 := map[string]interface{}{
"description": "Bright Green Socks",
"price": 25.50,
"city": "Fort Collins",
"location": "-105.0618814,40.5150098",
}
gjResult2, err := rdb.JSONSet(ctx, "product:46886", "$", prd46886).Result()
if err != nil {
panic(err)
}
fmt.Println(gjResult2) // >>> OK
geoQueryResult, err := rdb.FTSearch(ctx, "productidx",
"@location:[-104.800644 38.846127 100 mi]",
).Result()
if err != nil {
panic(err)
}
fmt.Println(geoQueryResult)
// >>> {1 [{product:46885...
geomCreateResult, err := rdb.FTCreate(ctx, "geomidx",
&redis.FTCreateOptions{
OnJSON: true,
Prefix: []interface{}{"shape:"},
},
&redis.FieldSchema{
FieldName: "$.name",
As: "name",
FieldType: redis.SearchFieldTypeText,
},
&redis.FieldSchema{
FieldName: "$.geom",
As: "geom",
FieldType: redis.SearchFieldTypeGeoShape,
GeoShapeFieldType: "FLAT",
},
).Result()
if err != nil {
panic(err)
}
fmt.Println(geomCreateResult) // >>> OK
shape1 := map[string]interface{}{
"name": "Green Square",
"geom": "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))",
}
gmjResult1, err := rdb.JSONSet(ctx, "shape:1", "$", shape1).Result()
if err != nil {
panic(err)
}
fmt.Println(gmjResult1) // >>> OK
shape2 := map[string]interface{}{
"name": "Red Rectangle",
"geom": "POLYGON ((2 2.5, 2 3.5, 3.5 3.5, 3.5 2.5, 2 2.5))",
}
gmjResult2, err := rdb.JSONSet(ctx, "shape:2", "$", shape2).Result()
if err != nil {
panic(err)
}
fmt.Println(gmjResult2) // >>> OK
shape3 := map[string]interface{}{
"name": "Blue Triangle",
"geom": "POLYGON ((3.5 1, 3.75 2, 4 1, 3.5 1))",
}
gmjResult3, err := rdb.JSONSet(ctx, "shape:3", "$", shape3).Result()
if err != nil {
panic(err)
}
fmt.Println(gmjResult3) // >>> OK
shape4 := map[string]interface{}{
"name": "Purple Point",
"geom": "POINT (2 2)",
}
gmjResult4, err := rdb.JSONSet(ctx, "shape:4", "$", shape4).Result()
if err != nil {
panic(err)
}
fmt.Println(gmjResult4) // >>> OK
geomQueryResult, err := rdb.FTSearchWithArgs(ctx, "geomidx",
"(-@name:(Green Square) @geom:[WITHIN $qshape])",
&redis.FTSearchOptions{
Params: map[string]interface{}{
"qshape": "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))",
},
DialectVersion: 4,
Limit: 1,
},
).Result()
if err != nil {
panic(err)
}
fmt.Println(geomQueryResult)
// >>> {1 [{shape:4...
}
Use the shape:
prefix for the JSON objects to add them to the index:
> JSON.SET shape:1 $ '{"name": "Green Square", "geom": "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))"}'
OK
> JSON.SET shape:2 $ '{"name": "Red Rectangle", "geom": "POLYGON ((2 2.5, 2 3.5, 3.5 3.5, 3.5 2.5, 2 2.5))"}'
OK
> JSON.SET shape:3 $ '{"name": "Blue Triangle", "geom": "POLYGON ((3.5 1, 3.75 2, 4 1, 3.5 1))"}'
OK
> JSON.SET shape:4 $ '{"name": "Purple Point", "geom": "POINT (2 2)"}'
OK
import org.json.JSONObject;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.json.Path2;
import redis.clients.jedis.search.Document;
import redis.clients.jedis.search.FTCreateParams;
import redis.clients.jedis.search.FTSearchParams;
import redis.clients.jedis.search.IndexDataType;
import redis.clients.jedis.search.schemafields.*;
import redis.clients.jedis.search.schemafields.GeoShapeField.CoordinateSystem;
import redis.clients.jedis.search.SearchResult;
import redis.clients.jedis.exceptions.JedisDataException;
public class GeoIndexExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
SchemaField[] geoSchema = {
GeoField.of("$.location").as("location")
};
String geoIdxCreateResult = jedis.ftCreate("productidx",
FTCreateParams.createParams()
.on(IndexDataType.JSON)
.addPrefix("product:"),
geoSchema
);
JSONObject prd46885 = new JSONObject()
.put("description", "Navy Blue Slippers")
.put("price", 45.99)
.put("city", "Denver")
.put("location", "-104.991531, 39.742043");
String jsonAddResult1 = jedis.jsonSet("product:46885", new Path2("$"), prd46885);
System.out.println(jsonAddResult1); // >>> OK
JSONObject prd46886 = new JSONObject()
.put("description", "Bright Green Socks")
.put("price", 25.50)
.put("city", "Fort Collins")
.put("location", "-105.0618814,40.5150098");
String jsonAddResult2 = jedis.jsonSet("product:46886", new Path2("$"), prd46886);
System.out.println(jsonAddResult2); // >>> OK
SearchResult geoResult = jedis.ftSearch("productidx",
"@location:[-104.800644 38.846127 100 mi]"
);
System.out.println(geoResult.getTotalResults()); // >>> 1
for (Document doc: geoResult.getDocuments()) {
System.out.println(doc.getId());
}
// >>> product:46885
SchemaField[] geomSchema = {
TextField.of("$.name").as("name"),
GeoShapeField.of("$.geom", CoordinateSystem.FLAT).as("geom")
};
String geomIndexCreateResult = jedis.ftCreate("geomidx",
FTCreateParams.createParams()
.on(IndexDataType.JSON)
.addPrefix("shape"),
geomSchema
);
System.out.println(geomIndexCreateResult); // >>> OK
JSONObject shape1 = new JSONObject()
.put("name", "Green Square")
.put("geom", "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))");
String gmJsonRes1 = jedis.jsonSet("shape:1", new Path2("$"), shape1);
System.out.println(gmJsonRes1); // >>> OK
JSONObject shape2 = new JSONObject()
.put("name", "Red Rectangle")
.put("geom", "POLYGON ((2 2.5, 2 3.5, 3.5 3.5, 3.5 2.5, 2 2.5))");
String gmJsonRes2 = jedis.jsonSet("shape:2", new Path2("$"), shape2);
System.out.println(gmJsonRes2); // >>> OK
JSONObject shape3 = new JSONObject()
.put("name", "Blue Triangle")
.put("geom", "POLYGON ((3.5 1, 3.75 2, 4 1, 3.5 1))");
String gmJsonRes3 = jedis.jsonSet("shape:3", new Path2("$"), shape3);
System.out.println(gmJsonRes3); // >>> OK
JSONObject shape4 = new JSONObject()
.put("name", "Purple Point")
.put("geom", "POINT (2 2)");
String gmJsonRes4 = jedis.jsonSet("shape:4", new Path2("$"), shape4);
System.out.println(gmJsonRes4); // >>> OK
SearchResult geomResult = jedis.ftSearch("geomidx",
"(-@name:(Green Square) @geom:[WITHIN $qshape])",
FTSearchParams.searchParams()
.addParam("qshape", "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))")
.dialect(4)
.limit(0, 1)
);
System.out.println(geomResult.getTotalResults()); // >>> 1
for (Document doc: geomResult.getDocuments()) {
System.out.println(doc.getId());
}
// shape:4
jedis.close();
}
}
package example_commands_test
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
func ExampleClient_geoindex() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
Protocol: 2,
})
geoCreateResult, err := rdb.FTCreate(ctx,
"productidx",
&redis.FTCreateOptions{
OnJSON: true,
Prefix: []interface{}{"product:"},
},
&redis.FieldSchema{
FieldName: "$.location",
As: "location",
FieldType: redis.SearchFieldTypeGeo,
},
).Result()
if err != nil {
panic(err)
}
fmt.Println(geoCreateResult) // >>> OK
prd46885 := map[string]interface{}{
"description": "Navy Blue Slippers",
"price": 45.99,
"city": "Denver",
"location": "-104.991531, 39.742043",
}
gjResult1, err := rdb.JSONSet(ctx, "product:46885", "$", prd46885).Result()
if err != nil {
panic(err)
}
fmt.Println(gjResult1) // >>> OK
prd46886 := map[string]interface{}{
"description": "Bright Green Socks",
"price": 25.50,
"city": "Fort Collins",
"location": "-105.0618814,40.5150098",
}
gjResult2, err := rdb.JSONSet(ctx, "product:46886", "$", prd46886).Result()
if err != nil {
panic(err)
}
fmt.Println(gjResult2) // >>> OK
geoQueryResult, err := rdb.FTSearch(ctx, "productidx",
"@location:[-104.800644 38.846127 100 mi]",
).Result()
if err != nil {
panic(err)
}
fmt.Println(geoQueryResult)
// >>> {1 [{product:46885...
geomCreateResult, err := rdb.FTCreate(ctx, "geomidx",
&redis.FTCreateOptions{
OnJSON: true,
Prefix: []interface{}{"shape:"},
},
&redis.FieldSchema{
FieldName: "$.name",
As: "name",
FieldType: redis.SearchFieldTypeText,
},
&redis.FieldSchema{
FieldName: "$.geom",
As: "geom",
FieldType: redis.SearchFieldTypeGeoShape,
GeoShapeFieldType: "FLAT",
},
).Result()
if err != nil {
panic(err)
}
fmt.Println(geomCreateResult) // >>> OK
shape1 := map[string]interface{}{
"name": "Green Square",
"geom": "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))",
}
gmjResult1, err := rdb.JSONSet(ctx, "shape:1", "$", shape1).Result()
if err != nil {
panic(err)
}
fmt.Println(gmjResult1) // >>> OK
shape2 := map[string]interface{}{
"name": "Red Rectangle",
"geom": "POLYGON ((2 2.5, 2 3.5, 3.5 3.5, 3.5 2.5, 2 2.5))",
}
gmjResult2, err := rdb.JSONSet(ctx, "shape:2", "$", shape2).Result()
if err != nil {
panic(err)
}
fmt.Println(gmjResult2) // >>> OK
shape3 := map[string]interface{}{
"name": "Blue Triangle",
"geom": "POLYGON ((3.5 1, 3.75 2, 4 1, 3.5 1))",
}
gmjResult3, err := rdb.JSONSet(ctx, "shape:3", "$", shape3).Result()
if err != nil {
panic(err)
}
fmt.Println(gmjResult3) // >>> OK
shape4 := map[string]interface{}{
"name": "Purple Point",
"geom": "POINT (2 2)",
}
gmjResult4, err := rdb.JSONSet(ctx, "shape:4", "$", shape4).Result()
if err != nil {
panic(err)
}
fmt.Println(gmjResult4) // >>> OK
geomQueryResult, err := rdb.FTSearchWithArgs(ctx, "geomidx",
"(-@name:(Green Square) @geom:[WITHIN $qshape])",
&redis.FTSearchOptions{
Params: map[string]interface{}{
"qshape": "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))",
},
DialectVersion: 4,
Limit: 1,
},
).Result()
if err != nil {
panic(err)
}
fmt.Println(geomQueryResult)
// >>> {1 [{shape:4...
}
You can now run various geospatial queries against the index. For example, the query below returns any shapes within the boundary of the green square but omits the green square itself:
> FT.SEARCH geomidx "(-@name:(Green Square) @geom:[WITHIN $qshape])" PARAMS 2 qshape "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))" RETURN 1 name DIALECT 4
1) (integer) 1
2) "shape:4"
3) 1) "name"
2) "[\"Purple Point\"]"
import org.json.JSONObject;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.json.Path2;
import redis.clients.jedis.search.Document;
import redis.clients.jedis.search.FTCreateParams;
import redis.clients.jedis.search.FTSearchParams;
import redis.clients.jedis.search.IndexDataType;
import redis.clients.jedis.search.schemafields.*;
import redis.clients.jedis.search.schemafields.GeoShapeField.CoordinateSystem;
import redis.clients.jedis.search.SearchResult;
import redis.clients.jedis.exceptions.JedisDataException;
public class GeoIndexExample {
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
SchemaField[] geoSchema = {
GeoField.of("$.location").as("location")
};
String geoIdxCreateResult = jedis.ftCreate("productidx",
FTCreateParams.createParams()
.on(IndexDataType.JSON)
.addPrefix("product:"),
geoSchema
);
JSONObject prd46885 = new JSONObject()
.put("description", "Navy Blue Slippers")
.put("price", 45.99)
.put("city", "Denver")
.put("location", "-104.991531, 39.742043");
String jsonAddResult1 = jedis.jsonSet("product:46885", new Path2("$"), prd46885);
System.out.println(jsonAddResult1); // >>> OK
JSONObject prd46886 = new JSONObject()
.put("description", "Bright Green Socks")
.put("price", 25.50)
.put("city", "Fort Collins")
.put("location", "-105.0618814,40.5150098");
String jsonAddResult2 = jedis.jsonSet("product:46886", new Path2("$"), prd46886);
System.out.println(jsonAddResult2); // >>> OK
SearchResult geoResult = jedis.ftSearch("productidx",
"@location:[-104.800644 38.846127 100 mi]"
);
System.out.println(geoResult.getTotalResults()); // >>> 1
for (Document doc: geoResult.getDocuments()) {
System.out.println(doc.getId());
}
// >>> product:46885
SchemaField[] geomSchema = {
TextField.of("$.name").as("name"),
GeoShapeField.of("$.geom", CoordinateSystem.FLAT).as("geom")
};
String geomIndexCreateResult = jedis.ftCreate("geomidx",
FTCreateParams.createParams()
.on(IndexDataType.JSON)
.addPrefix("shape"),
geomSchema
);
System.out.println(geomIndexCreateResult); // >>> OK
JSONObject shape1 = new JSONObject()
.put("name", "Green Square")
.put("geom", "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))");
String gmJsonRes1 = jedis.jsonSet("shape:1", new Path2("$"), shape1);
System.out.println(gmJsonRes1); // >>> OK
JSONObject shape2 = new JSONObject()
.put("name", "Red Rectangle")
.put("geom", "POLYGON ((2 2.5, 2 3.5, 3.5 3.5, 3.5 2.5, 2 2.5))");
String gmJsonRes2 = jedis.jsonSet("shape:2", new Path2("$"), shape2);
System.out.println(gmJsonRes2); // >>> OK
JSONObject shape3 = new JSONObject()
.put("name", "Blue Triangle")
.put("geom", "POLYGON ((3.5 1, 3.75 2, 4 1, 3.5 1))");
String gmJsonRes3 = jedis.jsonSet("shape:3", new Path2("$"), shape3);
System.out.println(gmJsonRes3); // >>> OK
JSONObject shape4 = new JSONObject()
.put("name", "Purple Point")
.put("geom", "POINT (2 2)");
String gmJsonRes4 = jedis.jsonSet("shape:4", new Path2("$"), shape4);
System.out.println(gmJsonRes4); // >>> OK
SearchResult geomResult = jedis.ftSearch("geomidx",
"(-@name:(Green Square) @geom:[WITHIN $qshape])",
FTSearchParams.searchParams()
.addParam("qshape", "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))")
.dialect(4)
.limit(0, 1)
);
System.out.println(geomResult.getTotalResults()); // >>> 1
for (Document doc: geomResult.getDocuments()) {
System.out.println(doc.getId());
}
// shape:4
jedis.close();
}
}
package example_commands_test
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
func ExampleClient_geoindex() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password docs
DB: 0, // use default DB
Protocol: 2,
})
geoCreateResult, err := rdb.FTCreate(ctx,
"productidx",
&redis.FTCreateOptions{
OnJSON: true,
Prefix: []interface{}{"product:"},
},
&redis.FieldSchema{
FieldName: "$.location",
As: "location",
FieldType: redis.SearchFieldTypeGeo,
},
).Result()
if err != nil {
panic(err)
}
fmt.Println(geoCreateResult) // >>> OK
prd46885 := map[string]interface{}{
"description": "Navy Blue Slippers",
"price": 45.99,
"city": "Denver",
"location": "-104.991531, 39.742043",
}
gjResult1, err := rdb.JSONSet(ctx, "product:46885", "$", prd46885).Result()
if err != nil {
panic(err)
}
fmt.Println(gjResult1) // >>> OK
prd46886 := map[string]interface{}{
"description": "Bright Green Socks",
"price": 25.50,
"city": "Fort Collins",
"location": "-105.0618814,40.5150098",
}
gjResult2, err := rdb.JSONSet(ctx, "product:46886", "$", prd46886).Result()
if err != nil {
panic(err)
}
fmt.Println(gjResult2) // >>> OK
geoQueryResult, err := rdb.FTSearch(ctx, "productidx",
"@location:[-104.800644 38.846127 100 mi]",
).Result()
if err != nil {
panic(err)
}
fmt.Println(geoQueryResult)
// >>> {1 [{product:46885...
geomCreateResult, err := rdb.FTCreate(ctx, "geomidx",
&redis.FTCreateOptions{
OnJSON: true,
Prefix: []interface{}{"shape:"},
},
&redis.FieldSchema{
FieldName: "$.name",
As: "name",
FieldType: redis.SearchFieldTypeText,
},
&redis.FieldSchema{
FieldName: "$.geom",
As: "geom",
FieldType: redis.SearchFieldTypeGeoShape,
GeoShapeFieldType: "FLAT",
},
).Result()
if err != nil {
panic(err)
}
fmt.Println(geomCreateResult) // >>> OK
shape1 := map[string]interface{}{
"name": "Green Square",
"geom": "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))",
}
gmjResult1, err := rdb.JSONSet(ctx, "shape:1", "$", shape1).Result()
if err != nil {
panic(err)
}
fmt.Println(gmjResult1) // >>> OK
shape2 := map[string]interface{}{
"name": "Red Rectangle",
"geom": "POLYGON ((2 2.5, 2 3.5, 3.5 3.5, 3.5 2.5, 2 2.5))",
}
gmjResult2, err := rdb.JSONSet(ctx, "shape:2", "$", shape2).Result()
if err != nil {
panic(err)
}
fmt.Println(gmjResult2) // >>> OK
shape3 := map[string]interface{}{
"name": "Blue Triangle",
"geom": "POLYGON ((3.5 1, 3.75 2, 4 1, 3.5 1))",
}
gmjResult3, err := rdb.JSONSet(ctx, "shape:3", "$", shape3).Result()
if err != nil {
panic(err)
}
fmt.Println(gmjResult3) // >>> OK
shape4 := map[string]interface{}{
"name": "Purple Point",
"geom": "POINT (2 2)",
}
gmjResult4, err := rdb.JSONSet(ctx, "shape:4", "$", shape4).Result()
if err != nil {
panic(err)
}
fmt.Println(gmjResult4) // >>> OK
geomQueryResult, err := rdb.FTSearchWithArgs(ctx, "geomidx",
"(-@name:(Green Square) @geom:[WITHIN $qshape])",
&redis.FTSearchOptions{
Params: map[string]interface{}{
"qshape": "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))",
},
DialectVersion: 4,
Limit: 1,
},
).Result()
if err != nil {
panic(err)
}
fmt.Println(geomQueryResult)
// >>> {1 [{shape:4...
}
You can also run queries to find whether shapes in the index completely contain or overlap each other. See Geospatial queries for more information.