From bf8cc0b91e6084cbb2711f51cf7982881e34cafa Mon Sep 17 00:00:00 2001 From: Kishore Nallan Date: Thu, 15 Aug 2024 22:56:55 +0530 Subject: [PATCH] During import return_id should work for failures. --- src/collection.cpp | 3 +++ test/collection_test.cpp | 9 ++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/collection.cpp b/src/collection.cpp index 079368dd..b2be4cbb 100644 --- a/src/collection.cpp +++ b/src/collection.cpp @@ -897,6 +897,9 @@ void Collection::batch_index(std::vector& index_records, std::vect res["embedding_error"] = index_record.embedding_res; } res["code"] = index_record.indexed.code(); + if (return_id && index_record.doc.contains("id")) { + res["id"] = index_record.doc["id"]; + } } json_out[index_record.position] = res.dump(-1, ' ', false, diff --git a/test/collection_test.cpp b/test/collection_test.cpp index 48656ee9..2d6610a6 100644 --- a/test/collection_test.cpp +++ b/test/collection_test.cpp @@ -1192,7 +1192,9 @@ TEST_F(CollectionTest, ImportDocumentsUpsert) { R"({"id": "5", "points": 60, "cast":["Logan Lerman","Alexandra Daddario"],"starring":"Ron Perlman","starring_facet":"Ron Perlman","title":"Percy Jackson: Sea of Monsters"})", R"({"id": "24", "starring": "John", "cast": ["John Kim"], "points": 11})"}; // missing fields - import_response = coll_mul_fields->add_many(more_records, document, UPSERT); + bool return_id = true; + import_response = coll_mul_fields->add_many(more_records, document, UPSERT, "", + DIRTY_VALUES::COERCE_OR_REJECT, false, return_id); ASSERT_FALSE(import_response["success"].get()); ASSERT_EQ(2, import_response["num_imported"].get()); @@ -1203,6 +1205,11 @@ TEST_F(CollectionTest, ImportDocumentsUpsert) { ASSERT_STREQ("Field `points` has been declared as a default sorting field, but is not found in the document.", import_results[1]["error"].get().c_str()); ASSERT_STREQ("Field `title` has been declared in the schema, but is not found in the document.", import_results[3]["error"].get().c_str()); + ASSERT_EQ("1", import_results[0]["id"].get()); + ASSERT_EQ("90", import_results[1]["id"].get()); + ASSERT_EQ("5", import_results[2]["id"].get()); + ASSERT_EQ("24", import_results[3]["id"].get()); + // try to duplicate records without upsert option more_records = {R"({"id": "1", "title": "Wake up, Harry"})",