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 preset in embedded key.

Kishore Nallan9f7800979631
diff --git a/src/collection_manager.cpp b/src/collection_manager.cpp
index 5a3468b2..b0c76f69 100644
--- a/src/collection_manager.cpp
+++ b/src/collection_manager.cpp
@@ -731,6 +731,27 @@ Option<bool> CollectionManager::do_search(std::map<std::string, std::string>& re
AuthManager::add_item_to_params(req_params, item, true);
}
+ 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()) {
+ return Option<bool>(400, "Search preset is not an object.");
+ }
+
+ 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) {
+ return Option<bool>(400, "One or more search parameters are malformed.");
+ }
+ }
+ }
+ }
+
CollectionManager & collectionManager = CollectionManager::get_instance();
const std::string& orig_coll_name = req_params["collection"];
auto collection = collectionManager.get_collection(orig_coll_name);
diff --git a/src/core_api.cpp b/src/core_api.cpp
index 6b9534e9..8967b160 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -368,29 +368,6 @@ 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;
- }
- }
- }
- }
-
if(req->embedded_params_vec.empty()) {
res->set_500("Embedded params is empty.");
return false;
@@ -569,27 +546,6 @@ bool post_multi_search(const std::shared_ptr<http_req>& req, const std::shared_p
}
}
- if(search_params.count("preset") != 0) {
- nlohmann::json preset;
- auto preset_op = CollectionManager::get_instance().get_preset(search_params["preset"].get<std::string>(),
- preset);
- if(preset_op.ok()) {
- if(!search_params.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, req->embedded_params_vec[i],
results_json_str, req->conn_ts);
diff --git a/test/core_api_utils_test.cpp b/test/core_api_utils_test.cpp
index 362ef643..57391357 100644
--- a/test/core_api_utils_test.cpp
+++ b/test/core_api_utils_test.cpp
@@ -253,6 +253,40 @@ TEST_F(CoreAPIUtilsTest, MultiSearchEmbeddedKeys) {
}
+TEST_F(CoreAPIUtilsTest, SearchEmbeddedPresetKey) {
+ nlohmann::json preset_value = R"(
+ {"per_page": 100}
+ )"_json;
+
+ Option<bool> success_op = collectionManager.upsert_preset("apple", preset_value);
+ ASSERT_TRUE(success_op.ok());
+
+ std::shared_ptr<http_req> req = std::make_shared<http_req>();
+ std::shared_ptr<http_res> res = std::make_shared<http_res>(nullptr);
+
+ nlohmann::json embedded_params;
+ embedded_params["preset"] = "apple";
+ req->embedded_params_vec.push_back(embedded_params);
+ req->params["collection"] = "foo";
+
+ get_search(req, res);
+ ASSERT_EQ("100", req->params["per_page"]);
+
+ // with multi search
+
+ req->params.clear();
+ nlohmann::json body;
+ body["searches"] = nlohmann::json::array();
+ nlohmann::json search;
+ search["collection"] = "users";
+ search["filter_by"] = "age: > 100";
+ body["searches"].push_back(search);
+ req->body = body.dump();
+
+ post_multi_search(req, res);
+ ASSERT_EQ("100", req->params["per_page"]);
+}
+
TEST_F(CoreAPIUtilsTest, ExtractCollectionsFromRequestBody) {
std::map<std::string, std::string> req_params;
std::string body = R"(