diff --git a/src/collection.cpp b/src/collection.cpp index 80ccc318..d8a58bdf 100644 --- a/src/collection.cpp +++ b/src/collection.cpp @@ -153,7 +153,7 @@ Option Collection::to_doc(const std::string & json_str, nlohmann:: } else { if(operation == UPDATE) { // for UPDATE, a document with given ID must be found - return Option(400, "Could not find a document with id: " + doc_id); + return Option(404, "Could not find a document with id: " + doc_id); } else { // for UPSERT or CREATE, if a document with given ID is not found, we will treat it as a new doc uint32_t seq_id = get_next_seq_id(); @@ -309,7 +309,7 @@ void Collection::batch_index(std::vector> &index_batch nlohmann::json res; if(index_record.indexed.ok()) { - if(index_record.operation == UPDATE || index_record.operation == UPSERT) { + if(index_record.is_update) { const std::string& serialized_json = index_record.new_doc.dump(-1, ' ', false, nlohmann::detail::error_handler_t::ignore); bool write_ok = store->insert(get_seq_id_key(index_record.seq_id), serialized_json); @@ -323,7 +323,7 @@ void Collection::batch_index(std::vector> &index_batch index_record.index_success(); } - } else if(index_record.operation == CREATE) { + } else { const std::string& seq_id_str = std::to_string(index_record.seq_id); const std::string& serialized_json = index_record.doc.dump(-1, ' ', false, nlohmann::detail::error_handler_t::ignore); diff --git a/test/collection_test.cpp b/test/collection_test.cpp index 186e1ae6..bb79807d 100644 --- a/test/collection_test.cpp +++ b/test/collection_test.cpp @@ -1370,6 +1370,11 @@ TEST_F(CollectionTest, ImportDocumentsUpsert) { ASSERT_EQ(19, coll_mul_fields->get_num_documents()); + results = coll_mul_fields->search("back again forest", query_fields, "", {}, sort_fields, 0, 30, 1, FREQUENCY, false).get(); + ASSERT_EQ(1, results["hits"].size()); + + ASSERT_STREQ("Back Again Forest", coll_mul_fields->get("18").get()["title"].get().c_str()); + results = coll_mul_fields->search("fifth", query_fields, "", {}, sort_fields, 0, 10, 1, FREQUENCY, false).get(); ASSERT_EQ(2, results["hits"].size()); @@ -1442,6 +1447,7 @@ TEST_F(CollectionTest, ImportDocumentsUpsert) { ASSERT_FALSE(import_results[0]["success"].get()); ASSERT_TRUE(import_results[1]["success"].get()); ASSERT_STREQ("Could not find a document with id: 20", import_results[0]["error"].get().c_str()); + ASSERT_EQ(404, import_results[0]["code"].get()); results = coll_mul_fields->search("wake up harry", query_fields, "", {}, sort_fields, 0, 10, 1, FREQUENCY, false).get(); ASSERT_EQ(64, results["hits"][0]["document"]["points"].get()); @@ -1459,6 +1465,9 @@ TEST_F(CollectionTest, ImportDocumentsUpsert) { ASSERT_FALSE(import_results[1]["success"].get()); ASSERT_STREQ("A document with id 2 already exists.", import_results[0]["error"].get().c_str()); ASSERT_STREQ("A document with id 1 already exists.", import_results[1]["error"].get().c_str()); + + ASSERT_EQ(409, import_results[0]["code"].get()); + ASSERT_EQ(409, import_results[1]["code"].get()); }