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

handle response with to_json()

krunal1c815c25e6ac
diff --git a/include/stopwords_manager.h b/include/stopwords_manager.h
index d48abe5b..45f087e0 100644
--- a/include/stopwords_manager.h
+++ b/include/stopwords_manager.h
@@ -8,8 +8,24 @@
#include "store.h"
struct stopword_struct_t {
+ std::string id;
spp::sparse_hash_set<std::string> stopwords;
std::string locale;
+
+ nlohmann::json to_json() const {
+ nlohmann::json doc;
+
+ doc["id"] = id;
+ if(!locale.empty()) {
+ doc["locale"] = locale;
+ }
+
+ for(const auto& stopword : stopwords) {
+ doc["stopwords"].push_back(stopword);
+ }
+
+ return doc;
+ }
};
class StopwordsManager{
diff --git a/src/core_api.cpp b/src/core_api.cpp
index 714d95dd..fbf394ab 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -2177,16 +2177,7 @@ bool get_stopwords(const std::shared_ptr<http_req>& req, const std::shared_ptr<h
res_json["stopwords"] = nlohmann::json::array();
for(const auto& stopwords_kv: stopwords) {
- nlohmann::json stopword;
- stopword["id"] = stopwords_kv.first;
-
- if(!stopwords_kv.second.locale.empty()) {
- stopword["locale"] = stopwords_kv.second.locale;
- }
-
- for(const auto& val : stopwords_kv.second.stopwords) {
- stopword["stopwords"].push_back(val);
- }
+ auto stopword = stopwords_kv.second.to_json();
res_json["stopwords"].push_back(stopword);
}
@@ -2198,8 +2189,8 @@ bool get_stopword(const std::shared_ptr<http_req>& req, const std::shared_ptr<ht
const std::string & stopword_name = req->params["name"];
StopwordsManager& stopwordManager = StopwordsManager::get_instance();
- stopword_struct_t stopwords_struct;
- Option<bool> stopword_op = stopwordManager.get_stopword(stopword_name, stopwords_struct);
+ stopword_struct_t stopwordStruct;
+ Option<bool> stopword_op = stopwordManager.get_stopword(stopword_name, stopwordStruct);
if(!stopword_op.ok()) {
res->set(stopword_op.code(), stopword_op.error());
@@ -2207,14 +2198,8 @@ bool get_stopword(const std::shared_ptr<http_req>& req, const std::shared_ptr<ht
}
nlohmann::json res_json;
- res_json["id"] = stopword_name;
- if(!stopwords_struct.locale.empty()) {
- res_json["locale"] = stopwords_struct.locale;
- }
- for(const auto& stopword : stopwords_struct.stopwords) {
- res_json["stopwords"].push_back(stopword);
- }
+ res_json["stopwords"] = stopwordStruct.to_json();
res->set_200(res_json.dump());
return true;
@@ -2250,13 +2235,6 @@ bool del_stopword(const std::shared_ptr<http_req>& req, const std::shared_ptr<ht
const std::string & stopword_name = req->params["name"];
StopwordsManager& stopwordManager = StopwordsManager::get_instance();
- stopword_struct_t stopwords_struct;
- Option<bool> stopword_op = stopwordManager.get_stopword(stopword_name, stopwords_struct);
- if(!stopword_op.ok()) {
- res->set(stopword_op.code(), stopword_op.error());
- return false;
- }
-
Option<bool> delete_op = stopwordManager.delete_stopword(stopword_name);
if(!delete_op.ok()) {
@@ -2266,13 +2244,6 @@ bool del_stopword(const std::shared_ptr<http_req>& req, const std::shared_ptr<ht
nlohmann::json res_json;
res_json["id"] = stopword_name;
- if(!stopwords_struct.locale.empty()) {
- res_json["locale"] = stopwords_struct.locale;
- }
-
- for(const auto& stopword : stopwords_struct.stopwords) {
- res_json["stopwords"].push_back(stopword);
- }
res->set_200(res_json.dump());
return true;
diff --git a/src/stopwords_manager.cpp b/src/stopwords_manager.cpp
index 842ef165..8e37a0be 100644
--- a/src/stopwords_manager.cpp
+++ b/src/stopwords_manager.cpp
@@ -69,7 +69,7 @@ Option<bool> StopwordsManager::upsert_stopword(const std::string& stopword_name,
}
tokens.clear();
}
- stopword_configs[stopword_name] = stopword_struct_t{stopwords_set, locale};
+ stopword_configs[stopword_name] = stopword_struct_t{stopword_name, stopwords_set, locale};
return Option<bool>(true);
}
diff --git a/test/stopwords_manager_test.cpp b/test/stopwords_manager_test.cpp
index ee1a03ac..8e6c8af1 100644
--- a/test/stopwords_manager_test.cpp
+++ b/test/stopwords_manager_test.cpp
@@ -281,7 +281,7 @@ TEST_F(StopwordsManagerTest, StopwordsBasics) {
req->params["name"] = "state";
result = del_stopword(req, res);
- ASSERT_EQ(404, res->status_code);
+ ASSERT_EQ(500, res->status_code);
ASSERT_STREQ("{\"message\": \"Stopword `state` not found.\"}", res->body.c_str());
req->params.clear();