diff --git a/src/core_api.cpp b/src/core_api.cpp index 60f8e870..8c640b82 100644 --- a/src/core_api.cpp +++ b/src/core_api.cpp @@ -2202,9 +2202,9 @@ bool get_stopword(const std::shared_ptr& req, const std::shared_ptrset_200(res_json.dump()); diff --git a/src/field.cpp b/src/field.cpp index 6fb57ed8..9d6a87a6 100644 --- a/src/field.cpp +++ b/src/field.cpp @@ -553,9 +553,11 @@ Option field::flatten_field(nlohmann::json& doc, nlohmann::json& obj, cons return flatten_field(doc, it.value(), the_field, path_parts, path_index + 1, has_array, has_obj_array, is_update, dyn_fields, flattened_fields); } - } { + } else if(!the_field.optional) { return Option(404, "Field `" + the_field.name + "` not found."); } + + return Option(true); } Option field::flatten_doc(nlohmann::json& document, diff --git a/test/collection_nested_fields_test.cpp b/test/collection_nested_fields_test.cpp index 0b882311..6572be2b 100644 --- a/test/collection_nested_fields_test.cpp +++ b/test/collection_nested_fields_test.cpp @@ -3105,6 +3105,81 @@ TEST_F(CollectionNestedFieldsTest, HighlightArrayOfObjects) { ASSERT_EQ(1, results["hits"][0]["highlight"]["details"][2].size()); } +TEST_F(CollectionNestedFieldsTest, DeepNestedOptionalArrayValue) { + nlohmann::json schema = R"({ + "name": "coll1", + "enable_nested_fields": true, + "fields": [ + { + "facet": false, + "index": true, + "infix": false, + "locale": "", + "name": "items.name", + "optional": true, + "sort": false, + "type": "string[]" + }, + { + "facet": false, + "index": true, + "infix": false, + "locale": "", + "name": "items.description", + "optional": true, + "sort": false, + "type": "string[]" + }, + { + "facet": false, + "index": true, + "infix": false, + "locale": "", + "name": "items.nested_items.name", + "optional": true, + "sort": false, + "type": "string[]" + } + ] + })"_json; + + auto op = collectionManager.create_collection(schema); + ASSERT_TRUE(op.ok()); + Collection* coll1 = op.get(); + + auto doc1 = R"({ + "items": [ + { + "description": "random description.", + "name": "foobar", + "nested_items": [ + { + "isAvailable": true + }, + { + "description": "nested description here", + "isAvailable": true, + "name": "naruto" + }, + { + "description": "description again", + "isAvailable": true, + "name": "dragon ball" + } + ] + } + ] + })"_json; + + auto add_op = coll1->add(doc1.dump(), CREATE); + ASSERT_TRUE(add_op.ok()); + + auto results = coll1->search("naruto", {"items.nested_items.name"}, "", {}, {}, {0}, 10, 1, FREQUENCY, + {true}, 1, spp::sparse_hash_set(), + spp::sparse_hash_set(), 10, "", 30, 4).get(); + ASSERT_EQ(1, results["found"].get()); +} + TEST_F(CollectionNestedFieldsTest, FloatInsideNestedObject) { nlohmann::json schema = R"({ "name": "coll1", diff --git a/test/collection_vector_search_test.cpp b/test/collection_vector_search_test.cpp index 755cb6ee..fde61f9e 100644 --- a/test/collection_vector_search_test.cpp +++ b/test/collection_vector_search_test.cpp @@ -3122,7 +3122,6 @@ TEST_F(CollectionVectorTest, TryAddingMultipleImageFieldToEmbedFrom) { ASSERT_EQ(collection_create_op.error(), "Only one field can be used in the `embed.from` property of an embed field when embedding from an image field."); } -<<<<<<< Updated upstream TEST_F(CollectionVectorTest, TestInvalidImage) { auto schema_json =