From 946f4d1d48c7112e4484b457af15b8e953b833df Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Mon, 20 Jan 2025 11:25:36 +0000 Subject: [PATCH 1/5] DOC-4733 added geo indexing examples --- tests/Doc/GeoIndexExample.cs | 192 +++++++++++++++++++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100644 tests/Doc/GeoIndexExample.cs diff --git a/tests/Doc/GeoIndexExample.cs b/tests/Doc/GeoIndexExample.cs new file mode 100644 index 00000000..27351a62 --- /dev/null +++ b/tests/Doc/GeoIndexExample.cs @@ -0,0 +1,192 @@ +// EXAMPLE: geoindex + +// STEP_START import +using NRedisStack.RedisStackCommands; +using NRedisStack.Search; +using NRedisStack.Search.Literals.Enums; +using StackExchange.Redis; +// STEP_END + +// REMOVE_START +using NRedisStack.Tests; +using NetTopologySuite.Geometries; + +namespace Doc; +[Collection("DocsTests")] +// REMOVE_END + +// HIDE_START +public class GeoIndexExample +{ + + [SkipIfRedis(Is.OSSCluster)] + public void run() + { + var muxer = ConnectionMultiplexer.Connect("localhost:6379"); + var db = muxer.GetDatabase(); + //REMOVE_START + // Clear any keys here before using them in tests. + db.KeyDelete(new RedisKey[] { "product:46885", "product:46886", "shape:1", "shape:2", "shape:3", "shape:4" }); + try { db.FT().DropIndex("productidx"); } catch { } + try { db.FT().DropIndex("geomidx"); } catch { } + //REMOVE_END + // HIDE_END + + // STEP_START create_geo_idx + Schema geoSchema = new Schema() + .AddGeoField(new FieldName("$.location", "location")); + + bool geoCreateResult = db.FT().Create( + "productidx", + new FTCreateParams() + .On(IndexDataType.JSON) + .Prefix("product:"), + geoSchema + ); + Console.WriteLine(geoCreateResult); // >>> True + // STEP_END + // REMOVE_START + Assert.True(geoCreateResult); + // REMOVE_END + + // STEP_START add_geo_json + var product46885 = new + { + description = "Navy Blue Slippers", + price = 45.99, + city = "Denver", + location = "-104.991531, 39.742043" + }; + + bool gjAddResult1 = db.JSON().Set("product:46885", "$", product46885); + Console.WriteLine(gjAddResult1); // >>> True + + var product46886 = new + { + description = "Bright Green Socks", + price = 25.50, + city = "Fort Collins", + location = "-105.0618814,40.5150098" + }; + + bool gjAddResult2 = db.JSON().Set("product:46886", "$", product46886); + Console.WriteLine(gjAddResult2); // >>> True + // STEP_END + // REMOVE_START + Assert.True(gjAddResult1); + Assert.True(gjAddResult2); + // REMOVE_END + + // STEP_START geo_query + SearchResult geoQueryResult = db.FT().Search( + "productidx", + new Query("@location:[-104.800644 38.846127 100 mi]") + ); + Console.WriteLine(geoQueryResult.Documents.Count); // >>> 1 + + Console.WriteLine( + string.Join(", ", geoQueryResult.Documents.Select(x => x["json"])) + ); + // >>> {"description":"Navy Blue Slippers","price":45.99,"city":"Denver"... + // STEP_END + // REMOVE_START + Assert.Single(geoQueryResult.Documents); + Assert.Equal( + "{\"description\":\"Navy Blue Slippers\",\"price\":45.99,\"city\":\"Denver\",\"location\":\"-104.991531, 39.742043\"}", + string.Join(", ", geoQueryResult.Documents.Select(x => x["json"])) + ); + // REMOVE_END + + // STEP_START create_gshape_idx + Schema geomSchema = new Schema() + .AddGeoShapeField( + new FieldName("$.geom", "geom"), + Schema.GeoShapeField.CoordinateSystem.FLAT + ) + .AddTextField(new FieldName("$.name", "name")); + + bool geomCreateResult = db.FT().Create( + "geomidx", + new FTCreateParams() + .On(IndexDataType.JSON) + .Prefix("shape:"), + geomSchema + ); + Console.WriteLine(geomCreateResult); // >>> True + // STEP_END + // REMOVE_START + Assert.True(geomCreateResult); + // REMOVE_END + + // STEP_START add_gshape_json + var shape1 = new + { + name = "Green Square", + geom = "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))" + }; + + bool gmJsonRes1 = db.JSON().Set("shape:1", "$", shape1); + Console.WriteLine(gmJsonRes1); // >>> True + + var shape2 = new + { + name = "Red Rectangle", + geom = "POLYGON ((2 2.5, 2 3.5, 3.5 3.5, 3.5 2.5, 2 2.5))" + }; + + bool gmJsonRes2 = db.JSON().Set("shape:2", "$", shape2); + Console.WriteLine(gmJsonRes1); // >>> True + + var shape3 = new + { + name = "Blue Triangle", + geom = "POLYGON ((3.5 1, 3.75 2, 4 1, 3.5 1))" + }; + + bool gmJsonRes3 = db.JSON().Set("shape:3", "$", shape3); + Console.WriteLine(gmJsonRes3); // >>> True + + var shape4 = new + { + name = "Purple Point", + geom = "POINT (2 2)" + }; + + bool gmJsonRes4 = db.JSON().Set("shape:4", "$", shape4); + Console.WriteLine(gmJsonRes3); // >>> True + // STEP_END + // REMOVE_START + Assert.True(gmJsonRes1); + Assert.True(gmJsonRes2); + Assert.True(gmJsonRes3); + Assert.True(gmJsonRes4); + // REMOVE_END + + // STEP_START gshape_query + SearchResult geomQueryResult = db.FT().Search( + "geomidx", + new Query("(-@name:(Green Square) @geom:[WITHIN $qshape])") + .AddParam("qshape", "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))") + .Limit(0, 1) + .Dialect(4) + ); + + Console.WriteLine(geomQueryResult.Documents.Count); // >>> 1 + var res = string.Join(", ", geomQueryResult.Documents.Select(x => x["json"])); + + Console.WriteLine( + string.Join(", ", geomQueryResult.Documents.Select(x => x["json"])) + ); + // >>> [{"name":"Purple Point","geom":"POINT (2 2)"}] + // STEP_END + // REMOVE_START + Assert.Single(geomQueryResult.Documents); + Assert.Equal( + "[{\"name\":\"Purple Point\",\"geom\":\"POINT (2 2)\"}]", + string.Join(", ", geomQueryResult.Documents.Select(x => x["json"])) + ); + // REMOVE_END + // HIDE_START + } +} +// HIDE_END From 0cb82dc49609f55c282be66bf0fd2ff2106b7f63 Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Mon, 24 Feb 2025 10:45:04 +0000 Subject: [PATCH 2/5] DOC-4733 updated testing code --- tests/Doc/GeoIndexExample.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/Doc/GeoIndexExample.cs b/tests/Doc/GeoIndexExample.cs index 27351a62..e915fcb7 100644 --- a/tests/Doc/GeoIndexExample.cs +++ b/tests/Doc/GeoIndexExample.cs @@ -9,7 +9,6 @@ // REMOVE_START using NRedisStack.Tests; -using NetTopologySuite.Geometries; namespace Doc; [Collection("DocsTests")] @@ -17,11 +16,22 @@ namespace Doc; // HIDE_START public class GeoIndexExample +// REMOVE_START +: AbstractNRedisStackTest, IDisposable +// REMOVE_END { + // REMOVE_START + public GeoIndexExample(EndpointsFixture fixture) : base(fixture) { } - [SkipIfRedis(Is.OSSCluster)] + [SkippableFact] + // REMOVE_END public void run() { + //REMOVE_START + // This is needed because we're constructing ConfigurationOptions in the test before calling GetConnection + SkipIfTargetConnectionDoesNotExist(EndpointsFixture.Env.Standalone); + var _ = GetCleanDatabase(EndpointsFixture.Env.Standalone); + //REMOVE_END var muxer = ConnectionMultiplexer.Connect("localhost:6379"); var db = muxer.GetDatabase(); //REMOVE_START From ea94b10ec0743a2cb041b9fdb7f3a34dd267a00b Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Tue, 30 Sep 2025 11:46:06 +0100 Subject: [PATCH 3/5] DOC-4733 try version check, following feedback --- tests/Doc/GeoIndexExample.cs | 41 ++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/tests/Doc/GeoIndexExample.cs b/tests/Doc/GeoIndexExample.cs index e915fcb7..92694b19 100644 --- a/tests/Doc/GeoIndexExample.cs +++ b/tests/Doc/GeoIndexExample.cs @@ -108,25 +108,30 @@ public void run() // REMOVE_END // STEP_START create_gshape_idx - Schema geomSchema = new Schema() - .AddGeoShapeField( - new FieldName("$.geom", "geom"), - Schema.GeoShapeField.CoordinateSystem.FLAT - ) - .AddTextField(new FieldName("$.name", "name")); - - bool geomCreateResult = db.FT().Create( - "geomidx", - new FTCreateParams() - .On(IndexDataType.JSON) - .Prefix("shape:"), - geomSchema - ); - Console.WriteLine(geomCreateResult); // >>> True + Version version = muxer.GetServer("localhost:6379").Version; + if (version.Major >= 7) + { + Schema geomSchema = new Schema() + .AddGeoShapeField( + new FieldName("$.geom", "geom"), + Schema.GeoShapeField.CoordinateSystem.FLAT + ) + .AddTextField(new FieldName("$.name", "name")); + + bool geomCreateResult = db.FT().Create( + "geomidx", + new FTCreateParams() + .On(IndexDataType.JSON) + .Prefix("shape:"), + geomSchema + ); + // REMOVE_START + Assert.True(geomCreateResult); + // REMOVE_END + Console.WriteLine(geomCreateResult); // >>> True + } // STEP_END - // REMOVE_START - Assert.True(geomCreateResult); - // REMOVE_END + // STEP_START add_gshape_json var shape1 = new From bd1b64e3dcd003c4c6fce41b45427e063919521a Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Tue, 30 Sep 2025 11:53:06 +0100 Subject: [PATCH 4/5] DOC-4733 removed obsolete request for dialect 4 --- tests/Doc/GeoIndexExample.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Doc/GeoIndexExample.cs b/tests/Doc/GeoIndexExample.cs index 92694b19..0f1e180c 100644 --- a/tests/Doc/GeoIndexExample.cs +++ b/tests/Doc/GeoIndexExample.cs @@ -183,7 +183,6 @@ public void run() new Query("(-@name:(Green Square) @geom:[WITHIN $qshape])") .AddParam("qshape", "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))") .Limit(0, 1) - .Dialect(4) ); Console.WriteLine(geomQueryResult.Documents.Count); // >>> 1 @@ -197,7 +196,7 @@ public void run() // REMOVE_START Assert.Single(geomQueryResult.Documents); Assert.Equal( - "[{\"name\":\"Purple Point\",\"geom\":\"POINT (2 2)\"}]", + "{\"name\":\"Purple Point\",\"geom\":\"POINT (2 2)\"}", string.Join(", ", geomQueryResult.Documents.Select(x => x["json"])) ); // REMOVE_END From d44d68bd71b3566cc3bfeeaa452fe173a0f041b4 Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Tue, 30 Sep 2025 12:01:26 +0100 Subject: [PATCH 5/5] DOC-4733 skip another example < v7 --- tests/Doc/GeoIndexExample.cs | 41 +++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/tests/Doc/GeoIndexExample.cs b/tests/Doc/GeoIndexExample.cs index 0f1e180c..b7c78574 100644 --- a/tests/Doc/GeoIndexExample.cs +++ b/tests/Doc/GeoIndexExample.cs @@ -178,28 +178,31 @@ public void run() // REMOVE_END // STEP_START gshape_query - SearchResult geomQueryResult = db.FT().Search( - "geomidx", - new Query("(-@name:(Green Square) @geom:[WITHIN $qshape])") - .AddParam("qshape", "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))") - .Limit(0, 1) - ); + if (version.Major >= 7) + { + SearchResult geomQueryResult = db.FT().Search( + "geomidx", + new Query("(-@name:(Green Square) @geom:[WITHIN $qshape])") + .AddParam("qshape", "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))") + .Limit(0, 1) + ); - Console.WriteLine(geomQueryResult.Documents.Count); // >>> 1 - var res = string.Join(", ", geomQueryResult.Documents.Select(x => x["json"])); + Console.WriteLine(geomQueryResult.Documents.Count); // >>> 1 + var res = string.Join(", ", geomQueryResult.Documents.Select(x => x["json"])); - Console.WriteLine( - string.Join(", ", geomQueryResult.Documents.Select(x => x["json"])) - ); - // >>> [{"name":"Purple Point","geom":"POINT (2 2)"}] + Console.WriteLine( + string.Join(", ", geomQueryResult.Documents.Select(x => x["json"])) + ); + // >>> {"name":"Purple Point","geom":"POINT (2 2)"} + // REMOVE_START + Assert.Single(geomQueryResult.Documents); + Assert.Equal( + "{\"name\":\"Purple Point\",\"geom\":\"POINT (2 2)\"}", + string.Join(", ", geomQueryResult.Documents.Select(x => x["json"])) + ); + // REMOVE_END + } // STEP_END - // REMOVE_START - Assert.Single(geomQueryResult.Documents); - Assert.Equal( - "{\"name\":\"Purple Point\",\"geom\":\"POINT (2 2)\"}", - string.Join(", ", geomQueryResult.Documents.Select(x => x["json"])) - ); - // REMOVE_END // HIDE_START } }