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

Fix geo loop orientation normalization.

Kishore Nallan2534b1f7982b
diff --git a/src/index.cpp b/src/index.cpp
index 1843d700..beca7a44 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -1409,19 +1409,9 @@ 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());
- }
-
auto loop = new S2Loop(vertices, S2Debug::DISABLE);
+ loop->Normalize(); // if loop is not CCW but CW, change to CCW.
+
S2Error error;
if (loop->FindValidationError(&error)) {
LOG(ERROR) << "Query vertex is bad, skipping. Error: " << error;
diff --git a/test/collection_filtering_test.cpp b/test/collection_filtering_test.cpp
index cfe454a5..d1fbbaa4 100644
--- a/test/collection_filtering_test.cpp
+++ b/test/collection_filtering_test.cpp
@@ -1350,6 +1350,55 @@ TEST_F(CollectionFilteringTest, GeoPolygonFiltering) {
collectionManager.drop_collection("coll1");
}
+TEST_F(CollectionFilteringTest, GeoPolygonFilteringSouthAmerica) {
+ Collection *coll1;
+
+ std::vector<field> fields = {field("title", field_types::STRING, false),
+ field("loc", field_types::GEOPOINT, false),
+ field("points", field_types::INT32, false),};
+
+ coll1 = collectionManager.get_collection("coll1").get();
+ if(coll1 == nullptr) {
+ coll1 = collectionManager.create_collection("coll1", 1, fields, "points").get();
+ }
+
+ std::vector<std::vector<std::string>> records = {
+ {"North of Equator", "4.48615, -71.38049"},
+ {"South of Equator", "-8.48587, -71.02892"},
+ };
+
+ for(size_t i=0; i<records.size(); i++) {
+ nlohmann::json doc;
+
+ std::vector<std::string> lat_lng;
+ StringUtils::split(records[i][1], lat_lng, ", ");
+
+ double lat = std::stod(lat_lng[0]);
+ double lng = std::stod(lat_lng[1]);
+
+ doc["id"] = std::to_string(i);
+ doc["title"] = records[i][0];
+ doc["loc"] = {lat, lng};
+ doc["points"] = i;
+
+ ASSERT_TRUE(coll1->add(doc.dump()).ok());
+ }
+
+ // pick a polygon that covers both points
+
+ auto results = coll1->search("*",
+ {}, "loc: (13.3163, -82.3585, "
+ "-29.134, -82.3585, "
+ "-29.134, -59.8528, "
+ "13.3163, -59.8528)",
+ {}, {}, {0}, 10, 1, FREQUENCY).get();
+
+ ASSERT_EQ(2, results["found"].get<size_t>());
+ ASSERT_EQ(2, results["hits"].size());
+
+ collectionManager.drop_collection("coll1");
+}
+
TEST_F(CollectionFilteringTest, FilteringWithPrefixSearch) {
Collection *coll1;