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

Ensure that geopoint is validated before indexing.

Kishore Nallandfb164234f9a
diff --git a/include/index.h b/include/index.h
index b4c1d396..f8d09971 100644
--- a/include/index.h
+++ b/include/index.h
@@ -281,6 +281,10 @@ private:
const std::string &field_name,
nlohmann::json::iterator& array_iter, bool is_array, bool& array_ele_erased);
+ static Option<uint32_t> coerce_geopoint(const DIRTY_VALUES& dirty_values, const field& a_field, nlohmann::json &document,
+ const std::string &field_name,
+ nlohmann::json::iterator& array_iter, bool is_array, bool& array_ele_erased);
+
public:
// for limiting number of results on multiple candidates / query rewrites
enum {TYPO_TOKENS_THRESHOLD = 100};
diff --git a/src/index.cpp b/src/index.cpp
index 257f91bb..2b91cb70 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -366,6 +366,18 @@ Option<uint32_t> Index::validate_index_in_memory(nlohmann::json& document, uint3
if(!coerce_op.ok()) {
return coerce_op;
}
+ } else if(a_field.type == field_types::GEOPOINT) {
+ if(!document[field_name].is_array() || document[field_name].size() != 2) {
+ return Option<>(400, "Field `" + field_name + "` must be a 2 element array: [lat, lng].");
+ }
+
+ if(!(document[field_name][0].is_number() && document[field_name][1].is_number())) {
+ // one or more elements is not an number, try to coerce
+ Option<uint32_t> coerce_op = coerce_geopoint(dirty_values, a_field, document, field_name, dummy_iter, false, array_ele_erased);
+ if(!coerce_op.ok()) {
+ return coerce_op;
+ }
+ }
} else if(a_field.is_array()) {
if(!document[field_name].is_array()) {
if(a_field.optional && (dirty_values == DIRTY_VALUES::DROP ||
@@ -2843,6 +2855,65 @@ Option<uint32_t> Index::coerce_bool(const DIRTY_VALUES& dirty_values, const fiel
return Option<uint32_t>(200);
}
+Option<uint32_t> Index::coerce_geopoint(const DIRTY_VALUES& dirty_values, const field& a_field, nlohmann::json &document,
+ const std::string &field_name,
+ nlohmann::json::iterator& array_iter, bool is_array, bool& array_ele_erased) {
+ std::string suffix = is_array ? "a array of" : "a";
+ auto& item = is_array ? array_iter.value() : document[field_name];
+
+ if(dirty_values == DIRTY_VALUES::REJECT) {
+ return Option<>(400, "Field `" + field_name + "` must be " + suffix + " geopoint.");
+ }
+
+ if(dirty_values == DIRTY_VALUES::DROP) {
+ if(!a_field.optional) {
+ return Option<>(400, "Field `" + field_name + "` must be " + suffix + " geopoint.");
+ }
+
+ if(!is_array) {
+ document.erase(field_name);
+ } else {
+ array_iter = document[field_name].erase(array_iter);
+ array_ele_erased = true;
+ }
+ return Option<uint32_t>(200);
+ }
+
+ // try to value coerce into a geopoint
+
+ if(!document[field_name][0].is_number() && document[field_name][0].is_string()) {
+ if(StringUtils::is_float(document[field_name][0])) {
+ document[field_name][0] = std::stof(document[field_name][0].get<std::string>());
+ }
+ }
+
+ if(!document[field_name][1].is_number() && document[field_name][1].is_string()) {
+ if(StringUtils::is_float(document[field_name][1])) {
+ document[field_name][1] = std::stof(document[field_name][1].get<std::string>());
+ }
+ }
+
+ if(!document[field_name][0].is_number() || !document[field_name][1].is_number()) {
+ if(dirty_values == DIRTY_VALUES::COERCE_OR_DROP) {
+ if(!a_field.optional) {
+ return Option<>(400, "Field `" + field_name + "` must be " + suffix + " geopoint.");
+ }
+
+ if(!is_array) {
+ document.erase(field_name);
+ } else {
+ array_iter = document[field_name].erase(array_iter);
+ array_ele_erased = true;
+ }
+ } else {
+ // COERCE_OR_REJECT / non-optional + DROP
+ return Option<>(400, "Field `" + field_name + "` must be " + suffix + " geopoint.");
+ }
+ }
+
+ return Option<uint32_t>(200);
+}
+
Option<uint32_t> Index::coerce_float(const DIRTY_VALUES& dirty_values, const field& a_field, nlohmann::json &document,
const std::string &field_name,
nlohmann::json::iterator& array_iter, bool is_array, bool& array_ele_erased) {
diff --git a/test/collection_filtering_test.cpp b/test/collection_filtering_test.cpp
index 5b8819ac..175a4c91 100644
--- a/test/collection_filtering_test.cpp
+++ b/test/collection_filtering_test.cpp
@@ -1030,6 +1030,42 @@ TEST_F(CollectionFilteringTest, GeoPointFiltering) {
ASSERT_STREQ("5", results["hits"][1]["document"]["id"].get<std::string>().c_str());
ASSERT_STREQ("3", results["hits"][2]["document"]["id"].get<std::string>().c_str());
+ // when geo field is formatted as string, show meaningful error
+ nlohmann::json bad_doc;
+ bad_doc["id"] = "1000";
+ bad_doc["title"] = "Test record";
+ bad_doc["loc"] = {"48.91", "2.33"};
+ bad_doc["points"] = 1000;
+
+ auto add_op = coll1->add(bad_doc.dump(), CREATE, "", DIRTY_VALUES::REJECT);
+ ASSERT_FALSE(add_op.ok());
+ ASSERT_EQ("Field `loc` must be a geopoint.", add_op.error());
+
+ bad_doc["loc"] = "foobar";
+ add_op = coll1->add(bad_doc.dump(), CREATE, "", DIRTY_VALUES::REJECT);
+ ASSERT_FALSE(add_op.ok());
+ ASSERT_EQ("Field `loc` must be a 2 element array: [lat, lng].", add_op.error());
+
+ bad_doc["loc"] = {"foo", "bar"};
+ add_op = coll1->add(bad_doc.dump(), CREATE, "", DIRTY_VALUES::COERCE_OR_REJECT);
+ ASSERT_FALSE(add_op.ok());
+ ASSERT_EQ("Field `loc` must be a geopoint.", add_op.error());
+
+ bad_doc["loc"] = {"2.33", "bar"};
+ add_op = coll1->add(bad_doc.dump(), CREATE, "", DIRTY_VALUES::COERCE_OR_REJECT);
+ ASSERT_FALSE(add_op.ok());
+ ASSERT_EQ("Field `loc` must be a geopoint.", add_op.error());
+
+ bad_doc["loc"] = {"foo", "2.33"};
+ add_op = coll1->add(bad_doc.dump(), CREATE, "", DIRTY_VALUES::COERCE_OR_REJECT);
+ ASSERT_FALSE(add_op.ok());
+ ASSERT_EQ("Field `loc` must be a geopoint.", add_op.error());
+
+ // under corcion mode, it should work
+ bad_doc["loc"] = {"48.91", "2.33"};
+ add_op = coll1->add(bad_doc.dump(), CREATE, "", DIRTY_VALUES::COERCE_OR_REJECT);
+ ASSERT_TRUE(add_op.ok());
+
collectionManager.drop_collection("coll1");
}