Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Polygon points used for filtering can be either CW or CCW.

Kishore Nallan1ad6619267a6
diff --git a/src/index.cpp b/src/index.cpp
index b603845f..3d76c41f 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -1371,6 +1371,8 @@ void Index::do_filtering(uint32_t*& filter_ids, uint32_t& filter_ids_length,
if(is_polygon) {
const int num_verts = int(filter_value_parts.size()) / 2;
std::vector<S2Point> vertices;
+ double sum = 0.0;
+
for(size_t point_index = 0; point_index < size_t(num_verts); point_index++) {
double lat = std::stod(filter_value_parts[point_index * 2]);
double lon = std::stod(filter_value_parts[point_index * 2 + 1]);
@@ -1378,6 +1380,18 @@ void Index::do_filtering(uint32_t*& filter_ids, uint32_t& filter_ids_length,
vertices.emplace_back(vertex);
}
+ for(size_t vi = 0; vi < vertices.size(); vi++) {
+ auto& v1 = vertices[vi];
+ auto& v2 = vertices[(vi + 1) % vertices.size()];
+ sum += (v2.x() - v1.x()) * (v2.y() + v1.y());
+ }
+
+ bool is_clockwise = (sum > 0.0);
+
+ if(is_clockwise) {
+ std::reverse(vertices.begin(), vertices.end());
+ }
+
query_region = new S2Loop(vertices);
} else {
double radius = std::stof(filter_value_parts[2]);
diff --git a/test/collection_filtering_test.cpp b/test/collection_filtering_test.cpp
index c0a669c7..c12abe8a 100644
--- a/test/collection_filtering_test.cpp
+++ b/test/collection_filtering_test.cpp
@@ -1319,6 +1319,18 @@ TEST_F(CollectionFilteringTest, GeoPolygonFiltering) {
ASSERT_STREQ("4", results["hits"][1]["document"]["id"].get<std::string>().c_str());
ASSERT_STREQ("0", results["hits"][2]["document"]["id"].get<std::string>().c_str());
+ // should work even if points of polygon are clockwise
+
+ results = coll1->search("*",
+ {}, "loc: (48.87756059389807, 2.3443610121873206, "
+ "48.859636574404355,2.351469427048221, "
+ "48.85745408145392, 2.3267084486160856, "
+ "48.875223042424125,2.323509661928681)",
+ {}, {}, {0}, 10, 1, FREQUENCY).get();
+
+ ASSERT_EQ(3, results["found"].get<size_t>());
+ ASSERT_EQ(3, results["hits"].size());
+
collectionManager.drop_collection("coll1");
}