From fed1a6300b41bd3bb1e21186805490ffd7d5297b Mon Sep 17 00:00:00 2001 From: Kishore Nallan Date: Tue, 25 Jun 2024 14:11:55 +0530 Subject: [PATCH] Fix update of array reference field. --- src/index.cpp | 2 +- test/collection_join_test.cpp | 72 ++++++++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/src/index.cpp b/src/index.cpp index e655123c..31cd4056 100644 --- a/src/index.cpp +++ b/src/index.cpp @@ -6841,7 +6841,7 @@ void Index::remove_field(uint32_t seq_id, nlohmann::json& document, const std::s } if (reference_index.count(field_name) != 0) { - reference_index[field_name]->remove(value, seq_id); + reference_index[field_name]->remove(seq_id, value); } } diff --git a/test/collection_join_test.cpp b/test/collection_join_test.cpp index 81f17f19..4403a491 100644 --- a/test/collection_join_test.cpp +++ b/test/collection_join_test.cpp @@ -1294,6 +1294,76 @@ TEST_F(CollectionJoinTest, UpdateDocumentHavingReferenceField) { ASSERT_EQ("Dan", res_obj["hits"][0]["document"]["Users"][0]["name"]); } +TEST_F(CollectionJoinTest, JoinAfterUpdateOfArrayField) { + auto exercise_schema = + R"({ + "name": "exercises", + "enable_nested_fields": true, + "fields": [ + {"name":"bodyParts","reference":"bodyParts.uid","type":"string[]"}, + {"name":"name","type":"string"}] + })"_json; + + auto collection_create_op = collectionManager.create_collection(exercise_schema); + ASSERT_TRUE(collection_create_op.ok()); + auto exercise_coll = collection_create_op.get(); + + auto body_parts_schema = + R"({ + "name": "bodyParts", + "enable_nested_fields": true, + "fields": [ + {"name":"uid","type":"string"}, + {"name":"name","type":"string"}] + })"_json; + + collection_create_op = collectionManager.create_collection(body_parts_schema); + ASSERT_TRUE(collection_create_op.ok()); + auto part_coll = collection_create_op.get(); + + nlohmann::json body_part_doc; + + body_part_doc["name"] = "Part 1"; + body_part_doc["uid"] = "abcd1"; + part_coll->add(body_part_doc.dump()); + + body_part_doc["name"] = "Part 2"; + body_part_doc["uid"] = "abcd2"; + part_coll->add(body_part_doc.dump()); + + body_part_doc["name"] = "Part 3"; + body_part_doc["uid"] = "abcd3"; + ASSERT_TRUE(part_coll->add(body_part_doc.dump()).ok()); + + nlohmann::json exercise_doc; + exercise_doc["id"] = "0"; + exercise_doc["name"] = "Example 1"; + exercise_doc["bodyParts"] = {"abcd1", "abcd2", "abcd3"}; + ASSERT_TRUE(exercise_coll->add(exercise_doc.dump()).ok()); + + // now update document to remove an array element + exercise_doc["bodyParts"] = {"abcd1", "abcd3"}; + ASSERT_TRUE(exercise_coll->add(exercise_doc.dump(), UPDATE).ok()); + + // search for the document + std::map req_params = { + {"collection", "exercises"}, + {"q", "*"}, + {"include_fields", "$bodyParts(uid, name, strategy:nest) as parts"} + }; + nlohmann::json embedded_params; + std::string json_res; + auto now_ts = std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()).count(); + + auto search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts); + ASSERT_TRUE(search_op.ok()); + + auto res = nlohmann::json::parse(json_res); + ASSERT_EQ(2, res["hits"][0]["document"]["bodyParts"].size()); + ASSERT_EQ(2, res["hits"][0]["document"]["parts"].size()); +} + TEST_F(CollectionJoinTest, FilterByReference_SingleMatch) { auto schema_json = R"({ @@ -5987,4 +6057,4 @@ TEST_F(CollectionJoinTest, QueryByReference) { search_op = collectionManager.do_search(req_params, embedded_params, json_res, now_ts); ASSERT_FALSE(search_op.ok()); ASSERT_EQ("Could not find `$Customers(customer_name` field in the schema.", search_op.error()); -} \ No newline at end of file +}