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

Fix embedded params on multi_search end-point.

kishorenca11befe42b87
diff --git a/include/auth_manager.h b/include/auth_manager.h
index 79cd6825..873df7a2 100644
--- a/include/auth_manager.h
+++ b/include/auth_manager.h
@@ -125,6 +125,7 @@ public:
bool authenticate(const std::string& req_api_key, const std::string& action,
const std::vector<std::string>& collections, std::map<std::string, std::string>& params) const;
- static bool populate_req_params(std::map<std::string, std::string> &req_params,
- nlohmann::detail::iteration_proxy_value<nlohmann::json::iterator>& item);
+ static bool add_item_to_params(std::map<std::string, std::string> &req_params,
+ nlohmann::detail::iteration_proxy_value<nlohmann::json::iterator>& item,
+ bool overwrite);
};
\ No newline at end of file
diff --git a/src/auth_manager.cpp b/src/auth_manager.cpp
index 80aa0122..ee3f87bc 100644
--- a/src/auth_manager.cpp
+++ b/src/auth_manager.cpp
@@ -160,13 +160,8 @@ bool AuthManager::authenticate(const std::string& req_api_key, const std::string
continue;
}
- if(params.count(item.key()) == 0) {
- AuthManager::populate_req_params(params, item);
- } else if(item.key() == "filter_by") {
- params[item.key()] = params[item.key()] + "&&" + item.value().get<std::string>();
- } else {
- AuthManager::populate_req_params(params, item);
- }
+ // overwrite = true as embedded params have higher priority
+ AuthManager::add_item_to_params(params, item, true);
}
return true;
@@ -358,20 +353,32 @@ Option<uint32_t> api_key_t::validate(const nlohmann::json &key_obj) {
}
-bool AuthManager::populate_req_params(std::map<std::string, std::string>& req_params,
- nlohmann::detail::iteration_proxy_value<nlohmann::json::iterator>& item) {
+bool AuthManager::add_item_to_params(std::map<std::string, std::string>& req_params,
+ nlohmann::detail::iteration_proxy_value<nlohmann::json::iterator>& item,
+ bool overwrite) {
+
+ std::string str_value;
+
if(item.value().is_string()) {
- req_params[item.key()] = item.value();
+ str_value = item.value().get<std::string>();
} else if(item.value().is_number_integer()) {
- req_params[item.key()] = std::to_string(item.value().get<int64_t>());
+ str_value = std::to_string(item.value().get<int64_t>());
} else if(item.value().is_number_float()) {
- req_params[item.key()] = std::to_string(item.value().get<float>());
+ str_value = std::to_string(item.value().get<float>());
} else if(item.value().is_boolean()) {
- req_params[item.key()] = item.value().get<bool>() ? "true" : "false";
+ str_value = item.value().get<bool>() ? "true" : "false";
} else {
return false;
}
+ if(req_params.count(item.key()) == 0) {
+ req_params[item.key()] = str_value;
+ } else if(item.key() == "filter_by") {
+ req_params[item.key()] = req_params[item.key()] + "&&" + str_value;
+ } else if(overwrite) {
+ req_params[item.key()] = str_value;
+ }
+
return true;
}
diff --git a/src/core_api.cpp b/src/core_api.cpp
index 5dac7de0..6a59bf17 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -231,17 +231,19 @@ bool post_multi_search(const std::shared_ptr<http_req>& req, const std::shared_p
nlohmann::json response;
response["results"] = nlohmann::json::array();
- // we have to ensure that `req_json` is a flat <string, string> map
- for(auto& search: req_json["searches"]) {
- if(!search.is_object()) {
+ nlohmann::json& searches = req_json["searches"];
+
+ for(auto& search_params: searches) {
+ if(!search_params.is_object()) {
res->set_400("The value of `searches` must be an array of objects.");
return false;
}
req->params = orig_req_params;
- for(auto& item: search.items()) {
- bool populated = AuthManager::populate_req_params(req->params, item);
+ for(auto& search_item: search_params.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;