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 limit_multi_searches.

Kishore Nallande3f0f4ddc7d
diff --git a/src/core_api.cpp b/src/core_api.cpp
index 766b6dad..08e9567b 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -402,11 +402,17 @@ bool post_multi_search(const std::shared_ptr<http_req>& req, const std::shared_p
return false;
}
+ if(req->embedded_params_vec.empty()) {
+ res->set_400("Missing embedded params array.");
+ return false;
+ }
+
const char* LIMIT_MULTI_SEARCHES = "limit_multi_searches";
size_t limit_multi_searches = 50;
- if(req->params.count(LIMIT_MULTI_SEARCHES) != 0 && StringUtils::is_uint32_t(req->params[LIMIT_MULTI_SEARCHES])) {
- limit_multi_searches = std::stoi(req->params[LIMIT_MULTI_SEARCHES]);
+ const auto& first_embedded_param = req->embedded_params_vec[0];
+ if(first_embedded_param.count(LIMIT_MULTI_SEARCHES) != 0 && first_embedded_param[LIMIT_MULTI_SEARCHES].is_number_integer()) {
+ limit_multi_searches = first_embedded_param[LIMIT_MULTI_SEARCHES].get<size_t>();
}
if(req_json["searches"].size() > limit_multi_searches) {
diff --git a/test/core_api_utils_test.cpp b/test/core_api_utils_test.cpp
index e9685924..73dd887b 100644
--- a/test/core_api_utils_test.cpp
+++ b/test/core_api_utils_test.cpp
@@ -199,6 +199,14 @@ TEST_F(CoreAPIUtilsTest, MultiSearchEmbeddedKeys) {
// ensure that req params are appended to (embedded params are also rolled into req params)
ASSERT_EQ("user_id: 100&&age: > 100&&foo: bar", req->params["filter_by"]);
+
+ // try setting max search limit
+ req->embedded_params_vec[0]["limit_multi_searches"] = 0;
+ ASSERT_FALSE(post_multi_search(req, res));
+ ASSERT_EQ("{\"message\": \"Number of multi searches exceeds `limit_multi_searches` parameter.\"}", res->body);
+
+ req->embedded_params_vec[0]["limit_multi_searches"] = 1;
+ ASSERT_TRUE(post_multi_search(req, res));
}
TEST_F(CoreAPIUtilsTest, ExtractCollectionsFromRequestBody) {