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
Support emplace action for ingestion.
Kishore Nallan3a8a2fcadbd7
diff --git a/include/index.h b/include/index.hindex ef85c1f5..7dbb3da3 100644--- a/include/index.h+++ b/include/index.h@@ -332,6 +332,7 @@ enum index_operation_t {CREATE,UPSERT,UPDATE,+ EMPLACE,DELETE};diff --git a/src/collection.cpp b/src/collection.cppindex 1cd40e34..e93e1c2b 100644--- a/src/collection.cpp+++ b/src/collection.cpp@@ -99,7 +99,7 @@ Option<doc_seq_id_t> Collection::to_doc(const std::string & json_str, nlohmann::if(operation == UPDATE) {return Option<doc_seq_id_t>(400, "For update, the `id` key must be provided.");}- // for UPSERT or CREATE, if a document does not have an ID, we will treat it as a new doc+ // for UPSERT, EMPLACE or CREATE, if a document does not have an ID, we will treat it as a new docuint32_t seq_id = get_next_seq_id();document["id"] = std::to_string(seq_id);return Option<doc_seq_id_t>(doc_seq_id_t{seq_id, true});@@ -123,7 +123,7 @@ Option<doc_seq_id_t> Collection::to_doc(const std::string & json_str, nlohmann::return Option<doc_seq_id_t>(409, std::string("A document with id ") + doc_id + " already exists.");}- // UPSERT or UPDATE+ // UPSERT, EMPLACE or UPDATEuint32_t seq_id = (uint32_t) std::stoul(seq_id_str);return Option<doc_seq_id_t>(doc_seq_id_t{seq_id, false});@@ -132,7 +132,7 @@ Option<doc_seq_id_t> Collection::to_doc(const std::string & json_str, nlohmann::// for UPDATE, a document with given ID must be foundreturn 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+ // for UPSERT, EMPLACE 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();return Option<doc_seq_id_t>(doc_seq_id_t{seq_id, true});}diff --git a/src/core_api.cpp b/src/core_api.cppindex 5dfd7d8c..10f8b6ff 100644--- a/src/core_api.cpp+++ b/src/core_api.cpp@@ -97,6 +97,8 @@ index_operation_t get_index_operation(const std::string& action) {return UPDATE;} else if(action == "upsert") {return UPSERT;+ } else if(action == "emplace") {+ return EMPLACE;}return CREATE;@@ -586,7 +588,8 @@ bool post_import_documents(const std::shared_ptr<http_req>& req, const std::sharreturn false;}- if(req->params[ACTION] != "create" && req->params[ACTION] != "update" && req->params[ACTION] != "upsert") {+ if(req->params[ACTION] != "create" && req->params[ACTION] != "update" && req->params[ACTION] != "upsert" &&+ req->params[ACTION] != "emplace") {res->final = true;res->set_400("Parameter `" + std::string(ACTION) + "` must be a create|update|upsert.");stream_response(req, res);@@ -703,7 +706,8 @@ bool post_add_document(const std::shared_ptr<http_req>& req, const std::shared_preq->params[ACTION] = "create";}- if(req->params[ACTION] != "create" && req->params[ACTION] != "update" && req->params[ACTION] != "upsert") {+ if(req->params[ACTION] != "create" && req->params[ACTION] != "update" && req->params[ACTION] != "upsert" &&+ req->params[ACTION] != "emplace") {res->set_400("Parameter `" + std::string(ACTION) + "` must be a create|update|upsert.");return false;}diff --git a/test/collection_test.cpp b/test/collection_test.cppindex 09229ed0..159ecec9 100644--- a/test/collection_test.cpp+++ b/test/collection_test.cpp@@ -1246,6 +1246,109 @@ TEST_F(CollectionTest, ImportDocumentsUpsert) {ASSERT_EQ(409, import_results[1]["code"].get<size_t>());}+TEST_F(CollectionTest, ImportDocumentsEmplace) {+ Collection* coll1;+ std::vector<field> fields = {+ field("title", field_types::STRING, false, false),+ field("points", field_types::INT32, false, true)+ };++ coll1 = collectionManager.get_collection("coll1").get();+ if (coll1 == nullptr) {+ coll1 = collectionManager.create_collection("coll1", 4, fields).get();+ }++ nlohmann::json document;+ std::vector<std::string> records = {R"({"id": "0", "title": "The Matrix", "points":0})",+ R"({"id": "1", "title": "Inception", "points":1})"};++ // use `emplace` mode for creating documents+ auto import_response = coll1->add_many(records, document, EMPLACE);++ ASSERT_TRUE(import_response["success"].get<bool>());+ ASSERT_EQ(2, import_response["num_imported"].get<int>());++ std::vector<nlohmann::json> import_results = import_res_to_json(records);+ ASSERT_EQ(2, import_results.size());++ for (size_t i = 0; i < 2; i++) {+ ASSERT_TRUE(import_results[i]["success"].get<bool>());+ ASSERT_EQ(1, import_results[i].size());+ }++ auto res = coll1->search("*", {}, "", {}, {}, {0}, 10, 1, token_ordering::FREQUENCY, {true}, 10).get();+ ASSERT_EQ(2, res["found"].get<size_t>());++ // emplace both update + create+ records = {R"({"id": "1", "title": "The Inception"})",+ R"({"id": "2", "title": "Spiderman", "points":2})"};++ import_response = coll1->add_many(records, document, EMPLACE);++ ASSERT_TRUE(import_response["success"].get<bool>());+ ASSERT_EQ(2, import_response["num_imported"].get<int>());++ import_results = import_res_to_json(records);+ ASSERT_EQ(2, import_results.size());++ for (size_t i = 0; i < 2; i++) {+ ASSERT_TRUE(import_results[i]["success"].get<bool>());+ ASSERT_EQ(1, import_results[i].size());+ }++ res = coll1->search("*", {}, "", {}, {}, {0}, 10, 1, token_ordering::FREQUENCY, {true}, 10).get();+ ASSERT_EQ(3, res["found"].get<size_t>());++ ASSERT_EQ("2", res["hits"][0]["document"]["id"].get<std::string>());+ ASSERT_EQ(2, res["hits"][0]["document"]["points"].get<size_t>());++ ASSERT_EQ("1", res["hits"][1]["document"]["id"].get<std::string>());+ ASSERT_EQ(1, res["hits"][1]["document"]["points"].get<size_t>());+ ASSERT_EQ("The Inception", res["hits"][1]["document"]["title"].get<std::string>());++ ASSERT_EQ("0", res["hits"][2]["document"]["id"].get<std::string>());+ ASSERT_EQ(0, res["hits"][2]["document"]["points"].get<size_t>());++ // emplace with an error due to bad data+ records = {R"({"id": "2", "points": "abcd"})",+ R"({"id": "3", "title": "Superman", "points":3})"};++ import_response = coll1->add_many(records, document, EMPLACE);++ ASSERT_FALSE(import_response["success"].get<bool>());+ ASSERT_EQ(1, import_response["num_imported"].get<int>());++ import_results = import_res_to_json(records);++ ASSERT_EQ(2, import_results.size());++ ASSERT_FALSE(import_results[0]["success"].get<bool>());++ ASSERT_TRUE(import_results[1]["success"].get<bool>());+ ASSERT_EQ(1, import_results[1].size());+ ASSERT_EQ(1, import_results[1].size());++ // can update individual document via "emplace"+ std::string doc_3_update = R"({"id": "3", "title": "The Superman"})";+ auto add_op = coll1->add(doc_3_update, EMPLACE);+ ASSERT_TRUE(add_op.ok());++ res = coll1->search("superman", {"title"}, "", {}, {}, {0}, 10, 1, token_ordering::FREQUENCY, {true}, 10).get();+ ASSERT_EQ(1, res["found"].get<size_t>());++ ASSERT_EQ("3", res["hits"][0]["document"]["id"].get<std::string>());+ ASSERT_EQ(3, res["hits"][0]["document"]["points"].get<size_t>());+ ASSERT_EQ("The Superman", res["hits"][0]["document"]["title"].get<std::string>());++ // can create individual document via "emplace"+ std::string doc_4_create = R"({"id": "4", "title": "The Avengers", "points": 4})";+ add_op = coll1->add(doc_4_create, EMPLACE);+ ASSERT_TRUE(add_op.ok());++ res = coll1->search("*", {}, "", {}, {}, {0}, 10, 1, token_ordering::FREQUENCY, {true}, 10).get();+ ASSERT_EQ(5, res["found"].get<size_t>());+}+TEST_F(CollectionTest, DISABLED_CrashTroubleshooting) {Collection *coll1;std::vector<field> fields = {