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

Handle bad geo polygon vertices.

Kishore Nallanda9a42f3f14e
diff --git a/src/index.cpp b/src/index.cpp
index cf09d922..825e7ae6 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -1396,7 +1396,15 @@ void Index::do_filtering(uint32_t*& filter_ids, uint32_t& filter_ids_length,
std::reverse(vertices.begin(), vertices.end());
}
- query_region = new S2Loop(vertices);
+ auto loop = new S2Loop(vertices, S2Debug::DISABLE);
+ S2Error error;
+ if (loop->FindValidationError(&error)) {
+ LOG(ERROR) << "Query vertex is bad, skipping. Error: " << error;
+ delete loop;
+ continue;
+ } else {
+ query_region = loop;
+ }
} else {
double radius = std::stof(filter_value_parts[2]);
const auto& unit = filter_value_parts[3];
diff --git a/test/collection_manager_test.cpp b/test/collection_manager_test.cpp
index 26cfe464..1ad89b21 100644
--- a/test/collection_manager_test.cpp
+++ b/test/collection_manager_test.cpp
@@ -632,6 +632,12 @@ TEST_F(CollectionManagerTest, ParseSortByClause) {
ASSERT_EQ("location(48.853, 2.344, exclude_radius: 2mi)", sort_fields[0].name);
ASSERT_STREQ("ASC", sort_fields[0].order.c_str());
+ sort_fields.clear();
+ sort_by_parsed = CollectionManager::parse_sort_by_str(" location(48.853, 2.344, precision: 2mi):asc,popularity:desc", sort_fields);
+ ASSERT_TRUE(sort_by_parsed);
+ ASSERT_EQ("location(48.853, 2.344, precision: 2mi)", sort_fields[0].name);
+ ASSERT_STREQ("ASC", sort_fields[0].order.c_str());
+
sort_fields.clear();
sort_by_parsed = CollectionManager::parse_sort_by_str(" _text_match(buckets: 10):ASC, points:desc ", sort_fields);
ASSERT_TRUE(sort_by_parsed);
diff --git a/test/collection_sorting_test.cpp b/test/collection_sorting_test.cpp
index 2b1e179d..0f186fa0 100644
--- a/test/collection_sorting_test.cpp
+++ b/test/collection_sorting_test.cpp
@@ -778,10 +778,23 @@ TEST_F(CollectionSortingTest, GeoPointSortingWithExcludeRadius) {
ASSERT_STREQ(expected_ids[i].c_str(), results["hits"][i]["document"]["id"].get<std::string>().c_str());
}
+ geo_sort_fields = { sort_by("loc(32.24348, 77.1893, precision: 2mi)", "ASC") };
+ auto res_op = coll1->search("*", {}, "loc: (32.24348, 77.1893, 20 km)",
+ {}, geo_sort_fields, {0}, 10, 1, FREQUENCY);
+ ASSERT_TRUE(res_op.ok());
+
+ // bad vertex -- Edge 0 is degenerate (duplicate vertex)
+ geo_sort_fields = { sort_by("loc(28.7040592, 77.10249019999999)", "ASC") };
+ res_op = coll1->search("*", {}, "loc: (28.7040592, 77.10249019999999, 28.7040592, "
+ "77.10249019999999, 28.7040592, 77.10249019999999, 28.7040592, 77.10249019999999)",
+ {}, geo_sort_fields, {0}, 10, 1, FREQUENCY);
+ ASSERT_TRUE(res_op.ok());
+ ASSERT_EQ(0, res_op.get()["found"].get<size_t>());
+
// badly formatted exclusion filter
geo_sort_fields = { sort_by("loc(32.24348, 77.1893, exclude_radius 1 km)", "ASC") };
- auto res_op = coll1->search("*", {}, "loc: (32.24348, 77.1893, 20 km)",
+ res_op = coll1->search("*", {}, "loc: (32.24348, 77.1893, 20 km)",
{}, geo_sort_fields, {0}, 10, 1, FREQUENCY);
ASSERT_FALSE(res_op.ok());