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

return error response when stopword is not found (#1545)

Krunal Gandhic6b25a7964ec
diff --git a/include/stopwords_manager.h b/include/stopwords_manager.h
index 45f087e0..7c160532 100644
--- a/include/stopwords_manager.h
+++ b/include/stopwords_manager.h
@@ -62,4 +62,6 @@ public:
Option<bool> delete_stopword(const std::string&);
void dispose();
+
+ bool stopword_exists(const std::string&);
};
\ No newline at end of file
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index 12bf5077..e2918b38 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -1456,6 +1456,10 @@ Option<bool> CollectionManager::do_search(std::map<std::string, std::string>& re
if(stopword_it != req_params.end()) {
stopwords_set = stopword_it->second;
+
+ if(!StopwordsManager::get_instance().stopword_exists(stopwords_set)) {
+ return Option<bool>(404, "Could not find the stopword set named `" + stopwords_set + "`.");
+ }
}
CollectionManager & collectionManager = CollectionManager::get_instance();
diff --git a/src/stopwords_manager.cpp b/src/stopwords_manager.cpp
index 30744e46..4a147418 100644
--- a/src/stopwords_manager.cpp
+++ b/src/stopwords_manager.cpp
@@ -97,4 +97,10 @@ Option<bool> StopwordsManager::delete_stopword(const std::string& stopword_name)
void StopwordsManager::dispose() {
std::unique_lock lock(mutex);
stopword_configs.clear();
+}
+
+bool StopwordsManager::stopword_exists(const std::string &stopword) {
+ std::shared_lock lock(mutex);
+
+ return stopword_configs.find(stopword) != stopword_configs.end();
}
\ No newline at end of file
diff --git a/test/stopwords_manager_test.cpp b/test/stopwords_manager_test.cpp
index ee1a03ac..79742795 100644
--- a/test/stopwords_manager_test.cpp
+++ b/test/stopwords_manager_test.cpp
@@ -303,9 +303,21 @@ TEST_F(StopwordsManagerTest, StopwordsBasics) {
req->params["stopwords"] = "continents";
search_op = collectionManager.do_search(req->params, embedded_params, json_results, now_ts);
- ASSERT_TRUE(search_op.ok());
- results = nlohmann::json::parse(json_results);
- ASSERT_EQ(1, results["hits"].size());
+ ASSERT_FALSE(search_op.ok());
+ ASSERT_EQ("Could not find the stopword set named `continents`.", search_op.error());
+
+ req->params.clear();
+ json_results.clear();
+
+ //typo while searching with stopword
+ req->params["collection"] = "coll1";
+ req->params["name"] = "the";
+ req->params["query_by"] = "title";
+ req->params["stopwords"] = "article";
+
+ search_op = collectionManager.do_search(req->params, embedded_params, json_results, now_ts);
+ ASSERT_FALSE(search_op.ok());
+ ASSERT_EQ("Could not find the stopword set named `article`.", search_op.error());
collectionManager.drop_collection("coll1");
}