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 geopoint indexing in nested fields.

Kishore Nallan52a78f276332
diff --git a/src/field.cpp b/src/field.cpp
index 3675d43c..0e9e8717 100644
--- a/src/field.cpp
+++ b/src/field.cpp
@@ -876,7 +876,9 @@ Option<bool> field::flatten_field(nlohmann::json& doc, nlohmann::json& obj, cons
(detected_type == field_types::INT64_ARRAY &&
(the_field.type == field_types::INT32_ARRAY || the_field.type == field_types::FLOAT_ARRAY)) ||
- (detected_type == field_types::FLOAT_ARRAY && the_field.type == field_types::GEOPOINT_ARRAY)
+ (detected_type == field_types::FLOAT_ARRAY && the_field.type == field_types::GEOPOINT_ARRAY) ||
+
+ (detected_type == field_types::FLOAT_ARRAY && the_field.type == field_types::GEOPOINT && !has_obj_array)
);
if(detected_type == the_field.type || is_numericaly_valid) {
diff --git a/test/collection_nested_fields_test.cpp b/test/collection_nested_fields_test.cpp
index 5baa6a15..bf7a5529 100644
--- a/test/collection_nested_fields_test.cpp
+++ b/test/collection_nested_fields_test.cpp
@@ -1847,6 +1847,62 @@ TEST_F(CollectionNestedFieldsTest, NestedFieldWithGeopointArray) {
ASSERT_EQ("Field `addresses.geoPoint` must be an array of geopoint.", create_op.error());
}
+TEST_F(CollectionNestedFieldsTest, NestedFieldWithGeopoint) {
+ nlohmann::json schema = R"({
+ "name": "coll1",
+ "enable_nested_fields": true,
+ "fields": [
+ {"name": "address.geoPoint", "type": "geopoint"}
+ ]
+ })"_json;
+
+ auto op = collectionManager.create_collection(schema);
+ ASSERT_TRUE(op.ok());
+ Collection* coll1 = op.get();
+
+ auto doc1 = R"({"address": {"geoPoint": [19.07283, 72.88261]}})"_json;
+ auto add_op = coll1->add(doc1.dump(), CREATE);
+ LOG(INFO) << add_op.error();
+ ASSERT_TRUE(add_op.ok());
+
+ auto results = coll1->search("*", {}, "", {}, {}, {0}, 10, 1, FREQUENCY, {false}, 0).get();
+ ASSERT_EQ(1, results["found"].get<size_t>());
+
+ results = coll1->search("*", {}, "address.geoPoint: (19.07, 72.882, 1 mi)",
+ {}, {}, {0}, 10, 1, FREQUENCY).get();
+ ASSERT_EQ(1, results["found"].get<size_t>());
+
+ // data validation
+ auto bad_doc = R"({
+ "address": {"geoPoint": [1.91, "x"]}
+ })"_json;
+
+ auto create_op = coll1->add(bad_doc.dump(), CREATE);
+ ASSERT_FALSE(create_op.ok());
+ ASSERT_EQ("Field `address.geoPoint` has an incorrect type.", create_op.error());
+
+ bad_doc = R"({
+ "address": {"geoPoint": [[1.91, "x"]]}
+ })"_json;
+
+ create_op = coll1->add(bad_doc.dump(), CREATE);
+ ASSERT_FALSE(create_op.ok());
+ ASSERT_EQ("Field `address.geoPoint` must be a 2 element array: [lat, lng].", create_op.error());
+
+ // with nested array field
+ bad_doc = R"({
+ "address": [
+ {"geoPoint": [1.91, 2.56]},
+ {"geoPoint": [2.91, 3.56]}
+ ]
+ })"_json;
+
+ create_op = coll1->add(bad_doc.dump(), CREATE);
+ ASSERT_FALSE(create_op.ok());
+ ASSERT_EQ("Field `address.geoPoint` has an incorrect type. "
+ "Hint: field inside an array of objects must be an array type as well.", create_op.error());
+}
+
TEST_F(CollectionNestedFieldsTest, GroupByOnNestedFieldsWithWildcardSchema) {
std::vector<field> fields = {field(".*", field_types::AUTO, false, true),
field("education.name", field_types::STRING_ARRAY, true, true),