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

Address multi search quirks

Jason Boscoc8b4d5fbed6f
diff --git a/include/auth_manager.h b/include/auth_manager.h
index dbadbdec..2cb10262 100644
--- a/include/auth_manager.h
+++ b/include/auth_manager.h
@@ -127,4 +127,7 @@ public:
bool auth_against_key(const std::string &collection, const std::string& action,
const api_key_t &api_key, const bool search_only) const;
+
+ static bool populate_req_params(std::map<std::string, std::string> &req_params,
+ nlohmann::detail::iteration_proxy_value<nlohmann::json::iterator>& item);
};
\ No newline at end of file
diff --git a/include/http_data.h b/include/http_data.h
index 6d3d1d28..dafcf0ae 100644
--- a/include/http_data.h
+++ b/include/http_data.h
@@ -339,6 +339,11 @@ struct route_path {
resource = path_parts[resource_index];
+ // special case to maintain semantics and backward compatibility
+ if(resource == "multi_search") {
+ return "documents:search";
+ }
+
if(resource_index != path_parts.size() - 1 && path_parts[resource_index+1][0] != ':') {
// e.g. /collections/:collection/documents/search
operation = path_parts[resource_index+1];
@@ -354,6 +359,8 @@ struct route_path {
operation = "upsert";
} else if(http_method == "DELETE") {
operation = "delete";
+ } else if(http_method == "PATCH") {
+ operation = "update";
} else {
operation = "unknown";
}
diff --git a/src/auth_manager.cpp b/src/auth_manager.cpp
index 4c8d1ba9..1665a4cb 100644
--- a/src/auth_manager.cpp
+++ b/src/auth_manager.cpp
@@ -145,17 +145,17 @@ bool AuthManager::authenticate(const std::string& req_api_key, const std::string
}
// enrich params with values from embedded_params
- for(auto it = embedded_params.begin(); it != embedded_params.end(); ++it) {
- if(it.key() == "expires_at") {
+ for(auto& item: embedded_params.items()) {
+ if(item.key() == "expires_at") {
continue;
}
- if(params.count(it.key()) == 0) {
- params[it.key()] = it.value();
- } else if(it.key() == "filter_by") {
- params[it.key()] = params[it.key()] + "&&" + it.value().get<std::string>();
+ 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 {
- params[it.key()] = it.value();
+ AuthManager::populate_req_params(params, item);
}
}
@@ -328,3 +328,22 @@ Option<uint32_t> api_key_t::validate(const nlohmann::json &key_obj) {
return Option<uint32_t>(200);
}
+
+bool AuthManager::populate_req_params(std::map<std::string, std::string>& req_params,
+ nlohmann::detail::iteration_proxy_value<nlohmann::json::iterator>& item) {
+ if(item.value().is_string()) {
+ req_params[item.key()] = item.value();
+ } else if(item.value().is_number_integer()) {
+ req_params[item.key()] = 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>());
+ } else if(item.value().is_boolean()) {
+ req_params[item.key()] = item.value().get<bool>() ? "true" : "false";
+ } else {
+ return false;
+ }
+
+ return true;
+}
+
+
diff --git a/src/core_api.cpp b/src/core_api.cpp
index 92cf8952..2c9ff9a4 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -303,7 +303,7 @@ bool post_multi_search(http_req& req, http_res& res) {
response["results"] = nlohmann::json::array();
// we have to ensure that `req_json` is a flat <string, string> map
- for(const auto& search: req_json["searches"]) {
+ for(auto& search: req_json["searches"]) {
if(!search.is_object()) {
res.set_400("The value of `searches` must be an array of objects.");
return false;
@@ -311,13 +311,12 @@ bool post_multi_search(http_req& req, http_res& res) {
req.params = orig_req_params;
- for(const auto& item: search.items()) {
- if(!item.value().is_string()) {
+ for(auto& item: search.items()) {
+ bool populated = AuthManager::populate_req_params(req.params, item);
+ if(!populated) {
res.set_400("One or more search parameters are malformed.");
return false;
}
-
- req.params[item.key()] = item.value();
}
std::string results_json_str;
diff --git a/src/http_server.cpp b/src/http_server.cpp
index 226bab15..a50e623c 100644
--- a/src/http_server.cpp
+++ b/src/http_server.cpp
@@ -517,12 +517,15 @@ int HttpServer::process_request(http_req* request, http_res* response, route_pat
const h2o_custom_req_handler_t *handler) {
//LOG(INFO) << "process_request called";
- bool operations_path = (rpath->path_parts.size() != 0 && rpath->path_parts[0] == "operations");
- //LOG(INFO) << "operations_path: " << operations_path;
+ // some end-points use POST but don't really need raft log persistence
+ bool write_free_request = (!rpath->path_parts.empty()) &&
+ (rpath->path_parts[0] == "operations" || rpath->path_parts[0] == "multi_search");
+
+ //LOG(INFO) << "write_free_request: " << write_free_request;
// for writes, we delegate to replication_state to handle response
- if(!operations_path &&
+ if(!write_free_request &&
(rpath->http_method == "POST" || rpath->http_method == "PUT" ||
rpath->http_method == "DELETE" || rpath->http_method == "PATCH")) {
handler->http_server->get_replication_state()->write(request, response);
diff --git a/src/main/typesense_server.cpp b/src/main/typesense_server.cpp
index b67521d4..c491d175 100644
--- a/src/main/typesense_server.cpp
+++ b/src/main/typesense_server.cpp
@@ -16,7 +16,7 @@ void master_server_routes() {
// collection operations
// NOTE: placing this first to score an immediate hit on O(N) route search
server->get("/collections/:collection/documents/search", get_search);
- server->post("/search", post_multi_search);
+ server->post("/multi_search", post_multi_search);
// document management
// NOTE:`/documents/:id` end-points must be placed last in the list
diff --git a/test/auth_manager_test.cpp b/test/auth_manager_test.cpp
index bbc9f34d..109d3f87 100644
--- a/test/auth_manager_test.cpp
+++ b/test/auth_manager_test.cpp
@@ -190,29 +190,25 @@ TEST_F(AuthManagerTest, VerifyAuthentication) {
}
TEST_F(AuthManagerTest, GenerationOfAPIAction) {
- std::map<int64_t, int64_t> int64map;
- int64map[1] = 1;
- int64map[2] = 2;
- int64map[3] = 3;
-
- std::cout << (int64map.lower_bound(5) == int64map.end());
- return ;
-
route_path rpath_search = route_path("GET", {"collections", ":collection", "documents", "search"}, nullptr, false, false);
+ route_path rpath_multi_search = route_path("POST", {"multi_search"}, nullptr, false, false);
route_path rpath_coll_create = route_path("POST", {"collections"}, nullptr, false, false);
route_path rpath_coll_get = route_path("GET", {"collections", ":collection"}, nullptr, false, false);
route_path rpath_coll_list = route_path("GET", {"collections"}, nullptr, false, false);
route_path rpath_keys_post = route_path("POST", {"keys"}, nullptr, false, false);
route_path rpath_doc_delete = route_path("DELETE", {"collections", ":collection", "documents", ":id"}, nullptr, false, false);
route_path rpath_override_upsert = route_path("PUT", {"collections", ":collection", "overrides", ":id"}, nullptr, false, false);
+ route_path rpath_doc_patch = route_path("PATCH", {"collections", ":collection", "documents", ":id"}, nullptr, false, false);
ASSERT_STREQ("documents:search", rpath_search._get_action().c_str());
+ ASSERT_STREQ("documents:search", rpath_multi_search._get_action().c_str());
ASSERT_STREQ("collections:create", rpath_coll_create._get_action().c_str());
ASSERT_STREQ("collections:get", rpath_coll_get._get_action().c_str());
ASSERT_STREQ("collections:list", rpath_coll_list._get_action().c_str());
ASSERT_STREQ("keys:create", rpath_keys_post._get_action().c_str());
ASSERT_STREQ("documents:delete", rpath_doc_delete._get_action().c_str());
ASSERT_STREQ("overrides:upsert", rpath_override_upsert._get_action().c_str());
+ ASSERT_STREQ("documents:update", rpath_doc_patch._get_action().c_str());
}
TEST_F(AuthManagerTest, ScopedAPIKeys) {