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

refactor

krunald2e8b437b659
diff --git a/include/stopwords_manager.h b/include/stopwords_manager.h
index 050f4067..a8e22965 100644
--- a/include/stopwords_manager.h
+++ b/include/stopwords_manager.h
@@ -36,9 +36,7 @@ public:
Option<bool> get_stopword(const std::string&, spp::sparse_hash_set<std::string>&) const;
- Option<bool> upsert_stopword(const std::string&, const nlohmann::json&);
+ Option<bool> upsert_stopword(const std::string&, const nlohmann::json&, bool write_to_store=false);
Option<bool> delete_stopword(const std::string&);
-
- void load_stopword_config(const std::string&, const nlohmann::json&);
};
\ No newline at end of file
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index 6a55d44c..388b0d70 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -298,7 +298,7 @@ Option<bool> CollectionManager::load(const size_t collection_batch_size, const s
nlohmann::json stopword_obj = nlohmann::json::parse(iter->value().ToString(), nullptr, false);
if(!stopword_obj.is_discarded() && stopword_obj.is_object()) {
- StopwordsManager::get_instance().load_stopword_config(stopword_name, stopword_obj);
+ StopwordsManager::get_instance().upsert_stopword(stopword_name, stopword_obj);
} else {
LOG(INFO) << "Invalid object for stopword " << stopword_name;
}
diff --git a/src/core_api.cpp b/src/core_api.cpp
index 8aef1a3f..fbd1579d 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -1946,32 +1946,9 @@ bool put_upsert_stopword(const std::shared_ptr<http_req>& req, const std::shared
StopwordsManager& stopwordManager = StopwordsManager::get_instance();
const std::string & stopword_name = req->params["name"];
- const char* STOPWORD_VALUES = "stopwords";
- const char* STOPWORD_LOCALE = "locale";
-
- if(req_json.count(STOPWORD_VALUES) == 0){
- res->set_400(std::string("Parameter `") + STOPWORD_VALUES + "` is required");
- return false;
- }
-
- if((!req_json[STOPWORD_VALUES].is_array()) || (!req_json[STOPWORD_VALUES][0].is_string())) {
- res->set_400(std::string("Parameter `") + STOPWORD_VALUES + "` is required as string array value");
- return false;
- }
-
- if(req_json.count(STOPWORD_LOCALE) == 0) {
- res->set_400(std::string("Parameter `") + STOPWORD_LOCALE + "` is required");
- return false;
- }
-
- if(!req_json[STOPWORD_LOCALE].is_string()) {
- res->set_400(std::string("Parameter `") + STOPWORD_LOCALE + "` is required as string value");
- return false;
- }
-
- Option<bool> success_op = stopwordManager.upsert_stopword(stopword_name, req_json);
+ Option<bool> success_op = stopwordManager.upsert_stopword(stopword_name, req_json, true);
if(!success_op.ok()) {
- res->set_500(success_op.error());
+ res->set(success_op.code(), success_op.error());
return false;
}
diff --git a/src/stopwords_manager.cpp b/src/stopwords_manager.cpp
index 87f917c8..7a518edc 100644
--- a/src/stopwords_manager.cpp
+++ b/src/stopwords_manager.cpp
@@ -22,18 +22,40 @@ Option<bool> StopwordsManager::get_stopword(const std::string& stopword_name, sp
return Option<bool>(404, "Stopword `" + stopword_name +"` not found.");
}
-Option<bool> StopwordsManager::upsert_stopword(const std::string& stopword_name, const nlohmann::json& stopwords_json) {
+Option<bool> StopwordsManager::upsert_stopword(const std::string& stopword_name, const nlohmann::json& stopwords_json,
+ bool write_to_store) {
std::unique_lock lock(mutex);
- bool inserted = store->insert(get_stopword_key(stopword_name), stopwords_json.dump());
- if(!inserted) {
- return Option<bool>(500, "Unable to insert into store.");
+ const char* STOPWORD_VALUES = "stopwords";
+ const char* STOPWORD_LOCALE = "locale";
+
+ if(stopwords_json.count(STOPWORD_VALUES) == 0){
+ return Option<bool>(400, (std::string("Parameter `") + STOPWORD_VALUES + "` is required"));
+ }
+
+ if((!stopwords_json[STOPWORD_VALUES].is_array()) || (!stopwords_json[STOPWORD_VALUES][0].is_string())) {
+ return Option<bool>(400, (std::string("Parameter `") + STOPWORD_VALUES + "` is required as string array value"));
+ }
+
+ if(stopwords_json.count(STOPWORD_LOCALE) == 0) {
+ return Option<bool>(400, (std::string("Parameter `") + STOPWORD_LOCALE + "` is required"));
+ }
+
+ if(!stopwords_json[STOPWORD_LOCALE].is_string()) {
+ return Option<bool>(400, (std::string("Parameter `") + STOPWORD_LOCALE + "` is required as string value"));
+ }
+
+ if(write_to_store) {
+ bool inserted = store->insert(get_stopword_key(stopword_name), stopwords_json.dump());
+ if (!inserted) {
+ return Option<bool>(500, "Unable to insert into store.");
+ }
}
std::vector<std::string> tokens;
spp::sparse_hash_set<std::string> stopwords_set;
- const auto& stopwords = stopwords_json["stopwords"];
- const auto& locale = stopwords_json["locale"];
+ const auto& stopwords = stopwords_json[STOPWORD_VALUES];
+ const auto& locale = stopwords_json[STOPWORD_LOCALE];
for (const auto &stopword: stopwords.items()) {
const auto& val = stopword.value().get<std::string>();
@@ -66,24 +88,4 @@ Option<bool> StopwordsManager::delete_stopword(const std::string& stopword_name)
stopword_configs.erase(stopword_name);
return Option<bool>(true);
-}
-
-void StopwordsManager::load_stopword_config(const std::string& stopword_name, const nlohmann::json& stopwords_json) {
- std::unique_lock lock(mutex);
-
- std::vector<std::string> tokens;
- spp::sparse_hash_set<std::string> stopwords_set;
- const auto& stopwords = stopwords_json["stopwords"];
- const auto& locale = stopwords_json["locale"];
-
- for (const auto &stopword: stopwords.items()) {
- const auto& val = stopword.value().get<std::string>();
- Tokenizer(val, true, false, locale, {}, {}).tokenize(tokens);
-
- for(const auto& tok : tokens) {
- stopwords_set.emplace(tok);
- }
- tokens.clear();
- }
- stopword_configs[stopword_name] = stopwords_set;
}
\ No newline at end of file