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
C++ algoliadatastoreelasticsearchenterprise-searchfacetingfull-text-searchfuzzy-searchgeosearchin-memoryinstantsearchmerchandisingpineconesearchsearch-enginesemantic-searchsimilarity-searchsite-searchsynonymstypo-tolerancevector-search
Handle reference to an id field from an object array field. (#1721)
Harpreet Sangar8d7def3eb995
diff --git a/src/collection.cpp b/src/collection.cppindex f2d61c13..3b36af6f 100644--- a/src/collection.cpp+++ b/src/collection.cpp@@ -154,9 +154,57 @@ Option<bool> Collection::add_reference_helper_fields(nlohmann::json& document, cauto const reference_helper_field = field_name + fields::REFERENCE_HELPER_FIELD_SUFFIX;+ bool is_object_reference_field = flat_fields.count(field_name) != 0;+ std::string object_key;+ bool is_object_array = false;+ if (is_object_reference_field) {+ object_reference_helper_fields.insert(reference_helper_field);++ std::vector<std::string> tokens;+ StringUtils::split(field_name, tokens, ".");+ if (schema.count(tokens[0]) == 0) {+ return Option<bool>(400, "Could not find `" + tokens[0] + "` object/object[] field in the schema.");+ }+ object_key = tokens[0];+ is_object_array = schema.at(object_key).is_array();+ }+if (reference_field_name == "id") {auto id_field_type_error_op = Option<bool>(400, "Field `" + field_name + "` must have string value.");- if (document[field_name].is_array()) {+ if (is_object_array) {+ if (!document[field_name].is_array()) {+ return Option<bool>(400, "Expected `" + field_name + "` to be an array.");+ }++ document[reference_helper_field] = nlohmann::json::array();+ document[fields::reference_helper_fields] += reference_helper_field;++ std::vector<std::string> keys;+ StringUtils::split(field_name, keys, ".");+ auto const& object_array = document[keys[0]];++ for (uint32_t i = 0; i < object_array.size(); i++) {+ if (optional && object_array[i].count(keys[1]) == 0) {+ continue;+ } else if (object_array[i].count(keys[1]) == 0) {+ return Option<bool>(400, "Object at index `" + std::to_string(i) + "` is missing `" + field_name + "`.");+ } else if (!object_array[i].at(keys[1]).is_string()) {+ return id_field_type_error_op;+ }++ auto id = object_array[i].at(keys[1]).get<std::string>();+ auto ref_doc_id_op = ref_collection->doc_id_to_seq_id_with_lock(id);+ if (!ref_doc_id_op.ok()) {+ return Option<bool>(400, "Referenced document having `id: " + id ++ "` not found in the collection `" ++ reference_collection_name + "`." );+ }++ // Adding the index of the object along with referenced doc id to account for the scenario where a+ // reference field of an object array might be optional and missing.+ document[reference_helper_field] += nlohmann::json::array({i, ref_doc_id_op.get()});+ }+ } else if (document[field_name].is_array()) {document[reference_helper_field] = nlohmann::json::array();document[fields::reference_helper_fields] += reference_helper_field;@@ -219,22 +267,7 @@ Option<bool> Collection::add_reference_helper_fields(nlohmann::json& document, c"` of type `" + ref_field.type + "`.");}- bool is_object_reference_field = flat_fields.count(field_name) != 0;- std::string object_key;- bool is_object_array;- if (is_object_reference_field) {- object_reference_helper_fields.insert(reference_helper_field);-- std::vector<std::string> tokens;- StringUtils::split(field_name, tokens, ".");- if (schema.count(tokens[0]) == 0) {- return Option<bool>(400, "Could not find `" + tokens[0] + "` object/object[] field in the schema.");- }- object_key = tokens[0];- is_object_array = schema.at(object_key).is_array();- }-- if (is_object_reference_field && is_object_array) {+ if (is_object_array) {if (!document[field_name].is_array()) {return Option<bool>(400, "Expected `" + field_name + "` to be an array.");}diff --git a/test/collection_join_test.cpp b/test/collection_join_test.cppindex 9f07c4ad..960799b5 100644--- a/test/collection_join_test.cpp+++ b/test/collection_join_test.cpp@@ -3801,6 +3801,89 @@ TEST_F(CollectionJoinTest, FilterByObjectReferenceField) {ASSERT_EQ(1, res_obj["hits"][1]["document"]["coll2"][0].count("coll_id"));ASSERT_EQ("b", res_obj["hits"][1]["document"]["coll2"][0]["coll_id"]);+ schema_json =+ R"({+ "name": "coll3",+ "fields": [+ {"name": "coll_id", "type": "string"},+ {"name": "object.reference_array", "type": "string[]", "reference": "Products.id", "optional": true},+ {"name": "object", "type": "object"}+ ],+ "enable_nested_fields": true+ })"_json;+ documents = {+ R"({+ "coll_id": "a",+ "object": {}+ })"_json,+ R"({+ "coll_id": "b",+ "object": {+ "reference_array": ["0", "1"]+ }+ })"_json+ };++ collection_create_op = collectionManager.create_collection(schema_json);+ ASSERT_TRUE(collection_create_op.ok());+ for (auto const &json: documents) {+ auto add_op = collection_create_op.get()->add(json.dump());+ if (!add_op.ok()) {+ LOG(INFO) << add_op.error();+ }+ ASSERT_TRUE(add_op.ok());+ }++ req_params = {+ {"collection", "coll3"},+ {"q", "*"},+ {"include_fields", "$Products(product_id)"}+ };+ search_op_bool = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);+ ASSERT_TRUE(search_op_bool.ok());++ res_obj = nlohmann::json::parse(json_res);+ ASSERT_EQ(2, res_obj["found"].get<size_t>());+ ASSERT_EQ(2, res_obj["hits"].size());+ ASSERT_EQ(3, res_obj["hits"][0]["document"].size());+ ASSERT_EQ("b", res_obj["hits"][0]["document"]["coll_id"]);+ ASSERT_EQ(2, res_obj["hits"][0]["document"]["object"].size());+ ASSERT_EQ("0", res_obj["hits"][0]["document"]["object"]["reference_array"][0]);+ ASSERT_EQ("1", res_obj["hits"][0]["document"]["object"]["reference_array"][1]);+ ASSERT_EQ(1, res_obj["hits"][0]["document"]["object"].count("Products"));+ ASSERT_EQ(2, res_obj["hits"][0]["document"]["object"]["Products"].size());+ ASSERT_EQ(1, res_obj["hits"][0]["document"]["object"]["Products"][0].count("product_id"));+ ASSERT_EQ("product_a", res_obj["hits"][0]["document"]["object"]["Products"][0]["product_id"]);+ ASSERT_EQ(1, res_obj["hits"][0]["document"]["object"]["Products"][1].count("product_id"));+ ASSERT_EQ("product_b", res_obj["hits"][0]["document"]["object"]["Products"][1]["product_id"]);+ ASSERT_EQ(3, res_obj["hits"][1]["document"].size());+ ASSERT_EQ("a", res_obj["hits"][1]["document"]["coll_id"]);+ ASSERT_EQ(0, res_obj["hits"][1]["document"]["object"].size());++ req_params = {+ {"collection", "Products"},+ {"q", "*"},+ {"filter_by", "$coll3(id: *)"},+ {"include_fields", "$coll3(coll_id)"}+ };+ search_op_bool = collectionManager.do_search(req_params, embedded_params, json_res, now_ts);+ ASSERT_TRUE(search_op_bool.ok());++ res_obj = nlohmann::json::parse(json_res);+ ASSERT_EQ(2, res_obj["found"].get<size_t>());+ ASSERT_EQ(2, res_obj["hits"].size());+ ASSERT_EQ(5, res_obj["hits"][0]["document"].size());+ ASSERT_EQ("product_b", res_obj["hits"][0]["document"]["product_id"]);+ ASSERT_EQ(1, res_obj["hits"][0]["document"].count("coll3"));+ ASSERT_EQ(1, res_obj["hits"][0]["document"]["coll3"].size());+ ASSERT_EQ(1, res_obj["hits"][0]["document"]["coll3"][0].count("coll_id"));+ ASSERT_EQ("b", res_obj["hits"][0]["document"]["coll3"][0]["coll_id"]);+ ASSERT_EQ("product_a", res_obj["hits"][1]["document"]["product_id"]);+ ASSERT_EQ(1, res_obj["hits"][1]["document"].count("coll3"));+ ASSERT_EQ(1, res_obj["hits"][1]["document"]["coll3"].size());+ ASSERT_EQ(1, res_obj["hits"][1]["document"]["coll3"][0].count("coll_id"));+ ASSERT_EQ("b", res_obj["hits"][1]["document"]["coll3"][0]["coll_id"]);+schema_json =R"({"name": "Portions",