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
Fix upsert not fetching document correctly.
Kishore Nallanbdbb097ad25e
diff --git a/src/collection.cpp b/src/collection.cppindex 80ccc318..d8a58bdf 100644--- a/src/collection.cpp+++ b/src/collection.cpp@@ -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 docuint32_t seq_id = get_next_seq_id();@@ -309,7 +309,7 @@ void Collection::batch_index(std::vector<std::vector<index_record>> &index_batchnlohmann::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_batchindex_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.cppindex 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<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>());}