Fix upsert not fetching document correctly.

This commit is contained in:
Kishore Nallan 2020-10-25 07:42:44 +05:30
parent 7d75f3f43d
commit bdbb097ad2
2 changed files with 12 additions and 3 deletions

View File

@ -153,7 +153,7 @@ Option<doc_seq_id_t> 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<doc_seq_id_t>(400, "Could not find a document with id: " + doc_id);
return Option<doc_seq_id_t>(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<std::vector<index_record>> &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<std::vector<index_record>> &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);

View File

@ -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<std::string>().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<bool>());
ASSERT_TRUE(import_results[1]["success"].get<bool>());
ASSERT_STREQ("Could not find a document with id: 20", import_results[0]["error"].get<std::string>().c_str());
ASSERT_EQ(404, import_results[0]["code"].get<size_t>());
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<uint32_t>());
@ -1459,6 +1465,9 @@ TEST_F(CollectionTest, ImportDocumentsUpsert) {
ASSERT_FALSE(import_results[1]["success"].get<bool>());
ASSERT_STREQ("A document with id 2 already exists.", import_results[0]["error"].get<std::string>().c_str());
ASSERT_STREQ("A document with id 1 already exists.", import_results[1]["error"].get<std::string>().c_str());
ASSERT_EQ(409, import_results[0]["code"].get<size_t>());
ASSERT_EQ(409, import_results[1]["code"].get<size_t>());
}