mirror of
https://github.com/typesense/typesense.git
synced 2025-05-21 06:02:26 +08:00
refactor
This commit is contained in:
parent
7b90c8afc3
commit
d2e8b437b6
@ -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&);
|
||||
};
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user