Skip to content

Commit 20da958

Browse files
committed
Add tests for search GEO
1 parent 67824eb commit 20da958

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

search_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,8 +1043,86 @@ var _ = Describe("RediSearch commands", Label("search"), func() {
10431043
Expect(err).NotTo(HaveOccurred())
10441044
Expect(res2.Total).To(BeEquivalentTo(int64(2)))
10451045
})
1046+
1047+
It("should test geoshapes query intersects and disjoint", func() {
1048+
_, err := client.FTCreate(ctx, "idx1", &redis.FTCreateOptions{}, &redis.FieldSchema{
1049+
FieldName: "g",
1050+
FieldType: redis.SearchFieldTypeGeoShape,
1051+
GeoShapeFieldType: "FLAT",
1052+
}).Result()
1053+
Expect(err).NotTo(HaveOccurred())
1054+
1055+
client.HSet(ctx, "doc_point1", "g", "POINT (10 10)")
1056+
client.HSet(ctx, "doc_point2", "g", "POINT (50 50)")
1057+
client.HSet(ctx, "doc_polygon1", "g", "POLYGON ((20 20, 25 35, 35 25, 20 20))")
1058+
client.HSet(ctx, "doc_polygon2", "g", "POLYGON ((60 60, 65 75, 70 70, 65 55, 60 60))")
1059+
1060+
intersection, err := client.FTSearchWithArgs(ctx, "idx1", "@g:[intersects $shape]",
1061+
&redis.FTSearchOptions{
1062+
DialectVersion: 3,
1063+
Params: map[string]interface{}{"shape": "POLYGON((15 15, 75 15, 50 70, 20 40, 15 15))"},
1064+
}).Result()
1065+
Expect(err).NotTo(HaveOccurred())
1066+
_assert_geosearch_result(client, &intersection, []string{"doc_point2", "doc_polygon1"})
1067+
1068+
disjunction, err := client.FTSearchWithArgs(ctx, "idx1", "@g:[disjoint $shape]",
1069+
&redis.FTSearchOptions{
1070+
DialectVersion: 3,
1071+
Params: map[string]interface{}{"shape": "POLYGON((15 15, 75 15, 50 70, 20 40, 15 15))"},
1072+
}).Result()
1073+
Expect(err).NotTo(HaveOccurred())
1074+
_assert_geosearch_result(client, &disjunction, []string{"doc_point1", "doc_polygon2"})
1075+
})
1076+
1077+
It("should test geoshapes query contains and within", func() {
1078+
_, err := client.FTCreate(ctx, "idx2", &redis.FTCreateOptions{}, &redis.FieldSchema{
1079+
FieldName: "g",
1080+
FieldType: redis.SearchFieldTypeGeoShape,
1081+
GeoShapeFieldType: "FLAT",
1082+
}).Result()
1083+
Expect(err).NotTo(HaveOccurred())
1084+
1085+
client.HSet(ctx, "doc_point1", "g", "POINT (10 10)")
1086+
client.HSet(ctx, "doc_point2", "g", "POINT (50 50)")
1087+
client.HSet(ctx, "doc_polygon1", "g", "POLYGON ((20 20, 25 35, 35 25, 20 20))")
1088+
client.HSet(ctx, "doc_polygon2", "g", "POLYGON ((60 60, 65 75, 70 70, 65 55, 60 60))")
1089+
1090+
containsA, err := client.FTSearchWithArgs(ctx, "idx2", "@g:[contains $shape]",
1091+
&redis.FTSearchOptions{
1092+
DialectVersion: 3,
1093+
Params: map[string]interface{}{"shape": "POINT(25 25)"},
1094+
}).Result()
1095+
Expect(err).NotTo(HaveOccurred())
1096+
_assert_geosearch_result(client, &containsA, []string{"doc_polygon1"})
1097+
1098+
containsB, err := client.FTSearchWithArgs(ctx, "idx2", "@g:[contains $shape]",
1099+
&redis.FTSearchOptions{
1100+
DialectVersion: 3,
1101+
Params: map[string]interface{}{"shape": "POLYGON((24 24, 24 26, 25 25, 24 24))"},
1102+
}).Result()
1103+
Expect(err).NotTo(HaveOccurred())
1104+
_assert_geosearch_result(client, &containsB, []string{"doc_polygon1"})
1105+
1106+
within, err := client.FTSearchWithArgs(ctx, "idx2", "@g:[within $shape]",
1107+
&redis.FTSearchOptions{
1108+
DialectVersion: 3,
1109+
Params: map[string]interface{}{"shape": "POLYGON((15 15, 75 15, 50 70, 20 40, 15 15))"},
1110+
}).Result()
1111+
Expect(err).NotTo(HaveOccurred())
1112+
_assert_geosearch_result(client, &within, []string{"doc_point2", "doc_polygon1"})
1113+
})
1114+
10461115
})
10471116

1117+
func _assert_geosearch_result(client *redis.Client, result *redis.FTSearchResult, expectedDocIDs []string) {
1118+
ids := make([]string, len(result.Docs))
1119+
for i, doc := range result.Docs {
1120+
ids[i] = doc.ID
1121+
}
1122+
Expect(ids).To(ConsistOf(expectedDocIDs))
1123+
Expect(result.Total).To(BeEquivalentTo(len(expectedDocIDs)))
1124+
}
1125+
10481126
// It("should FTProfile Search and Aggregate", Label("search", "ftprofile"), func() {
10491127
// val, err := client.FTCreate(ctx, "idx1", &redis.FTCreateOptions{}, &redis.FieldSchema{FieldName: "t", FieldType: redis.SearchFieldTypeText}).Result()
10501128
// Expect(err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)