Skip to content

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

Support single document upsert.

Kishore Nallana692553909a7
diff --git a/include/string_utils.h b/include/string_utils.h
index 3ff9e06c..46baf767 100644
--- a/include/string_utils.h
+++ b/include/string_utils.h
@@ -199,11 +199,12 @@ struct StringUtils {
return (*p == 0) && val >= std::numeric_limits<int32_t>::min() && val <= std::numeric_limits<int32_t>::max();
}
- static bool is_bool(const std::string &s) {
+ static bool is_bool(std::string &s) {
if(s.empty()) {
return false;
}
+ StringUtils::tolowercase(s);
return s == "true" || s == "false";
}
diff --git a/src/core_api.cpp b/src/core_api.cpp
index 80fd9620..c5a615cc 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -726,6 +726,23 @@ bool post_import_documents(http_req& req, http_res& res) {
}
bool post_add_document(http_req & req, http_res & res) {
+ const char *UPSERT = "upsert";
+ if(req.params.count(UPSERT) == 0) {
+ req.params[UPSERT] = "false";
+ }
+
+ if(!StringUtils::is_bool(req.params[UPSERT])) {
+ res.set_400("Parameter `" + std::string(UPSERT) + "` must be a boolean.");
+ return false;
+ }
+
+ std::string doc_id;
+ if(req.params.count("id") != 0) {
+ doc_id = req.params["id"];
+ }
+
+ const bool upsert = (req.params[UPSERT] == "true");
+
CollectionManager & collectionManager = CollectionManager::get_instance();
Collection* collection = collectionManager.get_collection(req.params["collection"]);
@@ -734,7 +751,7 @@ bool post_add_document(http_req & req, http_res & res) {
return false;
}
- Option<nlohmann::json> inserted_doc_op = collection->add(req.body);
+ Option<nlohmann::json> inserted_doc_op = collection->add(req.body, upsert, doc_id);
if(!inserted_doc_op.ok()) {
res.set(inserted_doc_op.code(), inserted_doc_op.error());
@@ -746,7 +763,10 @@ bool post_add_document(http_req & req, http_res & res) {
}
bool put_upsert_document(http_req & req, http_res & res) {
- std::string doc_id = req.params["id"];
+ std::string doc_id;
+ if(req.params.count("id") != 0) {
+ doc_id = req.params["id"];
+ }
CollectionManager & collectionManager = CollectionManager::get_instance();
Collection* collection = collectionManager.get_collection(req.params["collection"]);