From 20da958d6e941e15a22f4e028c9fa20ba3be431b Mon Sep 17 00:00:00 2001 From: ofekshenawa Date: Sun, 14 Jul 2024 20:05:32 +0300 Subject: [PATCH] Add tests for search GEO --- search_test.go | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/search_test.go b/search_test.go index 60888ef5..5931f538 100644 --- a/search_test.go +++ b/search_test.go @@ -1043,8 +1043,86 @@ var _ = Describe("RediSearch commands", Label("search"), func() { Expect(err).NotTo(HaveOccurred()) Expect(res2.Total).To(BeEquivalentTo(int64(2))) }) + + It("should test geoshapes query intersects and disjoint", func() { + _, err := client.FTCreate(ctx, "idx1", &redis.FTCreateOptions{}, &redis.FieldSchema{ + FieldName: "g", + FieldType: redis.SearchFieldTypeGeoShape, + GeoShapeFieldType: "FLAT", + }).Result() + Expect(err).NotTo(HaveOccurred()) + + client.HSet(ctx, "doc_point1", "g", "POINT (10 10)") + client.HSet(ctx, "doc_point2", "g", "POINT (50 50)") + client.HSet(ctx, "doc_polygon1", "g", "POLYGON ((20 20, 25 35, 35 25, 20 20))") + client.HSet(ctx, "doc_polygon2", "g", "POLYGON ((60 60, 65 75, 70 70, 65 55, 60 60))") + + intersection, err := client.FTSearchWithArgs(ctx, "idx1", "@g:[intersects $shape]", + &redis.FTSearchOptions{ + DialectVersion: 3, + Params: map[string]interface{}{"shape": "POLYGON((15 15, 75 15, 50 70, 20 40, 15 15))"}, + }).Result() + Expect(err).NotTo(HaveOccurred()) + _assert_geosearch_result(client, &intersection, []string{"doc_point2", "doc_polygon1"}) + + disjunction, err := client.FTSearchWithArgs(ctx, "idx1", "@g:[disjoint $shape]", + &redis.FTSearchOptions{ + DialectVersion: 3, + Params: map[string]interface{}{"shape": "POLYGON((15 15, 75 15, 50 70, 20 40, 15 15))"}, + }).Result() + Expect(err).NotTo(HaveOccurred()) + _assert_geosearch_result(client, &disjunction, []string{"doc_point1", "doc_polygon2"}) + }) + + It("should test geoshapes query contains and within", func() { + _, err := client.FTCreate(ctx, "idx2", &redis.FTCreateOptions{}, &redis.FieldSchema{ + FieldName: "g", + FieldType: redis.SearchFieldTypeGeoShape, + GeoShapeFieldType: "FLAT", + }).Result() + Expect(err).NotTo(HaveOccurred()) + + client.HSet(ctx, "doc_point1", "g", "POINT (10 10)") + client.HSet(ctx, "doc_point2", "g", "POINT (50 50)") + client.HSet(ctx, "doc_polygon1", "g", "POLYGON ((20 20, 25 35, 35 25, 20 20))") + client.HSet(ctx, "doc_polygon2", "g", "POLYGON ((60 60, 65 75, 70 70, 65 55, 60 60))") + + containsA, err := client.FTSearchWithArgs(ctx, "idx2", "@g:[contains $shape]", + &redis.FTSearchOptions{ + DialectVersion: 3, + Params: map[string]interface{}{"shape": "POINT(25 25)"}, + }).Result() + Expect(err).NotTo(HaveOccurred()) + _assert_geosearch_result(client, &containsA, []string{"doc_polygon1"}) + + containsB, err := client.FTSearchWithArgs(ctx, "idx2", "@g:[contains $shape]", + &redis.FTSearchOptions{ + DialectVersion: 3, + Params: map[string]interface{}{"shape": "POLYGON((24 24, 24 26, 25 25, 24 24))"}, + }).Result() + Expect(err).NotTo(HaveOccurred()) + _assert_geosearch_result(client, &containsB, []string{"doc_polygon1"}) + + within, err := client.FTSearchWithArgs(ctx, "idx2", "@g:[within $shape]", + &redis.FTSearchOptions{ + DialectVersion: 3, + Params: map[string]interface{}{"shape": "POLYGON((15 15, 75 15, 50 70, 20 40, 15 15))"}, + }).Result() + Expect(err).NotTo(HaveOccurred()) + _assert_geosearch_result(client, &within, []string{"doc_point2", "doc_polygon1"}) + }) + }) +func _assert_geosearch_result(client *redis.Client, result *redis.FTSearchResult, expectedDocIDs []string) { + ids := make([]string, len(result.Docs)) + for i, doc := range result.Docs { + ids[i] = doc.ID + } + Expect(ids).To(ConsistOf(expectedDocIDs)) + Expect(result.Total).To(BeEquivalentTo(len(expectedDocIDs))) +} + // It("should FTProfile Search and Aggregate", Label("search", "ftprofile"), func() { // val, err := client.FTCreate(ctx, "idx1", &redis.FTCreateOptions{}, &redis.FieldSchema{FieldName: "t", FieldType: redis.SearchFieldTypeText}).Result() // Expect(err).NotTo(HaveOccurred())