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

Support presets on GET search endpoint as well.

Kishore Nallanbf475ad55fbd
diff --git a/include/auth_manager.h b/include/auth_manager.h
index 6f7ef394..1aa28fca 100644
--- a/include/auth_manager.h
+++ b/include/auth_manager.h
@@ -128,6 +128,6 @@ public:
const std::vector<std::string>& collections, std::map<std::string, std::string>& params) const;
static bool add_item_to_params(std::map<std::string, std::string> &req_params,
- nlohmann::detail::iteration_proxy_value<nlohmann::json::iterator>& item,
+ const nlohmann::detail::iteration_proxy_value<nlohmann::json::iterator>& item,
bool overwrite);
};
\ No newline at end of file
diff --git a/include/collection_manager.h b/include/collection_manager.h
index d50b442c..f38b1c3f 100644
--- a/include/collection_manager.h
+++ b/include/collection_manager.h
@@ -186,7 +186,7 @@ public:
// presets
spp::sparse_hash_map<std::string, nlohmann::json> get_presets() const;
- Option<nlohmann::json> get_preset(const std::string & preset_name) const;
+ Option<bool> get_preset(const std::string & preset_name, nlohmann::json& preset) const;
Option<bool> upsert_preset(const std::string & preset_name, const nlohmann::json& preset_config);
diff --git a/src/auth_manager.cpp b/src/auth_manager.cpp
index 482ba50a..cb24b6f4 100644
--- a/src/auth_manager.cpp
+++ b/src/auth_manager.cpp
@@ -366,7 +366,7 @@ Option<uint32_t> api_key_t::validate(const nlohmann::json &key_obj) {
bool AuthManager::add_item_to_params(std::map<std::string, std::string>& req_params,
- nlohmann::detail::iteration_proxy_value<nlohmann::json::iterator>& item,
+ const nlohmann::detail::iteration_proxy_value<nlohmann::json::iterator>& item,
bool overwrite) {
std::string str_value;
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index 19e8113c..5d1d7f4d 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -1259,15 +1259,16 @@ spp::sparse_hash_map<std::string, nlohmann::json> CollectionManager::get_presets
return preset_configs;
}
-Option<nlohmann::json> CollectionManager::get_preset(const string& preset_name) const {
+Option<bool> CollectionManager::get_preset(const string& preset_name, nlohmann::json& preset) const {
std::shared_lock lock(mutex);
const auto& it = preset_configs.find(preset_name);
if(it != preset_configs.end()) {
- return Option<nlohmann::json>(it->second);
+ preset = it->second;
+ return Option<bool>(true);
}
- return Option<nlohmann::json>(404, "Not found.");
+ return Option<bool>(404, "Not found.");
}
Option<bool> CollectionManager::upsert_preset(const string& preset_name, const nlohmann::json& preset_config) {
diff --git a/src/core_api.cpp b/src/core_api.cpp
index 9dc225d6..9b976d3d 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -263,6 +263,29 @@ bool get_search(const std::shared_ptr<http_req>& req, const std::shared_ptr<http
}
}
+ const auto preset_it = req->params.find("preset");
+
+ if(preset_it != req->params.end()) {
+ nlohmann::json preset;
+ const auto& preset_op = CollectionManager::get_instance().get_preset(preset_it->second, preset);
+
+ if(preset_op.ok()) {
+ if(!preset.is_object()) {
+ res->set_400("Search preset is not an object.");
+ return false;
+ }
+
+ for(const auto& search_item: preset.items()) {
+ // overwrite = false since req params will contain embedded params and so has higher priority
+ bool populated = AuthManager::add_item_to_params(req->params, search_item, false);
+ if(!populated) {
+ res->set_400("One or more search parameters are malformed.");
+ return false;
+ }
+ }
+ }
+ }
+
std::string results_json_str;
Option<bool> search_op = CollectionManager::do_search(req->params, results_json_str);
@@ -326,10 +349,7 @@ bool post_multi_search(const std::shared_ptr<http_req>& req, const std::shared_p
const auto preset_it = req->params.find("preset");
if(preset_it != req->params.end()) {
- const auto preset_op = CollectionManager::get_instance().get_preset(preset_it->second);
- if(preset_op.ok()) {
- req_json = preset_op.get();
- }
+ CollectionManager::get_instance().get_preset(preset_it->second, req_json);
}
if(req_json.empty()) {
@@ -1517,16 +1537,18 @@ bool get_presets(const std::shared_ptr<http_req>& req, const std::shared_ptr<htt
bool get_preset(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {
const std::string & preset_name = req->params["preset_name"];
CollectionManager & collectionManager = CollectionManager::get_instance();
- Option<nlohmann::json> preset_op = collectionManager.get_preset(preset_name);
+
+ nlohmann::json preset;
+ Option<bool> preset_op = collectionManager.get_preset(preset_name, preset);
if(!preset_op.ok()) {
- res->set_404();
+ res->set(preset_op.code(), preset_op.error());
return false;
}
nlohmann::json res_json;
res_json["name"] = preset_name;
- res_json["value"] = preset_op.get();
+ res_json["value"] = preset;
res->set_200(res_json.dump());
return true;
@@ -1569,9 +1591,10 @@ bool del_preset(const std::shared_ptr<http_req>& req, const std::shared_ptr<http
const std::string & preset_name = req->params["name"];
CollectionManager & collectionManager = CollectionManager::get_instance();
- Option<nlohmann::json> preset_op = collectionManager.get_preset(preset_name);
+ nlohmann::json preset;
+ Option<bool> preset_op = collectionManager.get_preset(preset_name, preset);
if(!preset_op.ok()) {
- res->set_404();
+ res->set(preset_op.code(), preset_op.error());
return false;
}
@@ -1584,7 +1607,7 @@ bool del_preset(const std::shared_ptr<http_req>& req, const std::shared_ptr<http
nlohmann::json res_json;
res_json["name"] = preset_name;
- res_json["value"] = preset_op.get();
+ res_json["value"] = preset;
res->set_200(res_json.dump());
return true;
}
diff --git a/test/collection_manager_test.cpp b/test/collection_manager_test.cpp
index 0534d7c3..931ca6f5 100644
--- a/test/collection_manager_test.cpp
+++ b/test/collection_manager_test.cpp
@@ -691,15 +691,17 @@ TEST_F(CollectionManagerTest, Presets) {
ASSERT_EQ(2, collectionManager.get_presets().size());
// try fetching individual presets
- auto preset_op = collectionManager.get_preset("preset1");
+ nlohmann::json preset;
+ auto preset_op = collectionManager.get_preset("preset1", preset);
ASSERT_TRUE(preset_op.ok());
- ASSERT_EQ(1, preset_op.get().size());
- ASSERT_EQ("foo", preset_op.get()["query_by"]);
+ ASSERT_EQ(1, preset.size());
+ ASSERT_EQ("foo", preset["query_by"]);
- preset_op = collectionManager.get_preset("preset2");
+ preset.clear();
+ preset_op = collectionManager.get_preset("preset2", preset);
ASSERT_TRUE(preset_op.ok());
- ASSERT_EQ(1, preset_op.get().size());
- ASSERT_EQ("bar", preset_op.get()["query_by"]);
+ ASSERT_EQ(1, preset.size());
+ ASSERT_EQ("bar", preset["query_by"]);
// delete a preset
auto del_op = collectionManager.delete_preset("preset2");
@@ -710,7 +712,8 @@ TEST_F(CollectionManagerTest, Presets) {
ASSERT_EQ(StoreStatus::NOT_FOUND, status);
ASSERT_EQ(1, collectionManager.get_presets().size());
- preset_op = collectionManager.get_preset("preset2");
+ preset.clear();
+ preset_op = collectionManager.get_preset("preset2", preset);
ASSERT_FALSE(preset_op.ok());
ASSERT_EQ(404, preset_op.code());
@@ -723,6 +726,7 @@ TEST_F(CollectionManagerTest, Presets) {
collectionManager.load(8, 1000);
ASSERT_EQ(1, collectionManager.get_presets().size());
- preset_op = collectionManager.get_preset("preset1");
+ preset.clear();
+ preset_op = collectionManager.get_preset("preset1", preset);
ASSERT_TRUE(preset_op.ok());
}