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

remove error response when deleting non existing doc (#1532)

Krunal Gandhi562e152d9b5c
diff --git a/src/core_api.cpp b/src/core_api.cpp
index 8b88f34b..3d7a893b 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -1365,6 +1365,11 @@ bool get_fetch_document(const std::shared_ptr<http_req>& req, const std::shared_
bool del_remove_document(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {
std::string doc_id = req->params["id"];
+ bool ignore_not_found = false;
+ if((req->params.count("ignore_not_found") != 0) && (req->params["ignore_not_found"] == "true")) {
+ ignore_not_found = true;
+ }
+
CollectionManager & collectionManager = CollectionManager::get_instance();
auto collection = collectionManager.get_collection(req->params["collection"]);
if(collection == nullptr) {
@@ -1374,20 +1379,35 @@ bool del_remove_document(const std::shared_ptr<http_req>& req, const std::shared
Option<nlohmann::json> doc_option = collection->get(doc_id);
- if(!doc_option.ok()) {
+ if (!doc_option.ok()) {
+ if (ignore_not_found && doc_option.code() == 404) {
+ nlohmann::json resp;
+ resp["id"] = doc_id;
+ res->set_200(resp.dump());
+ return true;
+ }
+
res->set(doc_option.code(), doc_option.error());
return false;
}
Option<std::string> deleted_id_op = collection->remove(doc_id);
- if(!deleted_id_op.ok()) {
+ if (!deleted_id_op.ok()) {
+ if (ignore_not_found && doc_option.code() == 404) {
+ nlohmann::json resp;
+ resp["id"] = doc_id;
+ res->set_200(resp.dump());
+ return true;
+ }
+
res->set(deleted_id_op.code(), deleted_id_op.error());
return false;
}
nlohmann::json doc = doc_option.get();
res->set_200(doc.dump(-1, ' ', false, nlohmann::detail::error_handler_t::ignore));
+
return true;
}
diff --git a/test/core_api_utils_test.cpp b/test/core_api_utils_test.cpp
index bcb48f08..e610ccfc 100644
--- a/test/core_api_utils_test.cpp
+++ b/test/core_api_utils_test.cpp
@@ -1491,4 +1491,42 @@ TEST_F(CoreAPIUtilsTest, TestInvalidConversationModels) {
ASSERT_EQ(400, resp->status_code);
ASSERT_EQ("Property `model_name` is not provided or not a string.", nlohmann::json::parse(resp->body)["message"]);
+}
+
+TEST_F(CoreAPIUtilsTest, DeleteNonExistingDoc) {
+ Collection *coll1;
+
+ std::vector<field> fields = {field("title", field_types::STRING, false),
+ field("points", field_types::INT32, false),};
+
+ coll1 = collectionManager.get_collection("coll1").get();
+ if(coll1 == nullptr) {
+ coll1 = collectionManager.create_collection("coll1", 2, fields, "points").get();
+ }
+
+ for(size_t i=0; i<10; i++) {
+ nlohmann::json doc;
+
+ doc["id"] = std::to_string(i);
+ doc["title"] = "Title " + std::to_string(i);
+ doc["points"] = i;
+
+ coll1->add(doc.dump());
+ }
+
+ std::shared_ptr<http_req> req = std::make_shared<http_req>();
+ std::shared_ptr<http_res> res = std::make_shared<http_res>(nullptr);
+
+ req->params["collection"] = "coll1";
+ req->params["id"] = "9";
+ del_remove_document(req, res);
+ ASSERT_EQ(200, res->status_code);
+
+ req->params["id"] = "10";
+ del_remove_document(req, res);
+ ASSERT_EQ(404, res->status_code);
+
+ req->params["ignore_not_found"] = "true";
+ del_remove_document(req, res);
+ ASSERT_EQ(200, res->status_code);
}
\ No newline at end of file