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

Update for naming conventions and fix hashing for strings

ozanarmagand61abab259f3
diff --git a/include/core_api.h b/include/core_api.h
index cd8cdff8..780d3d1b 100644
--- a/include/core_api.h
+++ b/include/core_api.h
@@ -169,4 +169,4 @@ Option<std::pair<std::string,std::string>> get_api_key_and_ip(const std::string&
void init_api(uint32_t cache_num_entries);
-bool proxy_embedding(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);
+bool post_proxy(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res);
diff --git a/include/http_proxy.h b/include/http_proxy.h
index e116f0af..4b8315b3 100644
--- a/include/http_proxy.h
+++ b/include/http_proxy.h
@@ -39,5 +39,5 @@ class HttpProxy {
// lru cache for http requests
- LRU::TimedCache<std::string, http_proxy_res_t> cache;
+ LRU::TimedCache<uint64_t, http_proxy_res_t> cache;
};
\ No newline at end of file
diff --git a/include/text_embedder_remote.h b/include/text_embedder_remote.h
index 131973d6..99f9146f 100644
--- a/include/text_embedder_remote.h
+++ b/include/text_embedder_remote.h
@@ -18,7 +18,7 @@ class RemoteEmbedder {
public:
virtual Option<std::vector<float>> Embed(const std::string& text) = 0;
virtual Option<std::vector<std::vector<float>>> batch_embed(const std::vector<std::string>& inputs) = 0;
- static void set_raft_server(ReplicationState* rs) {
+ static void init(ReplicationState* rs) {
raft_server = rs;
}
diff --git a/src/core_api.cpp b/src/core_api.cpp
index b97a2c62..4add2b2d 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -2139,7 +2139,7 @@ bool del_analytics_rules(const std::shared_ptr<http_req>& req, const std::shared
}
-bool proxy_embedding(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {
+bool post_proxy(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {
HttpProxy& proxy = HttpProxy::get_instance();
nlohmann::json req_json;
@@ -2159,9 +2159,7 @@ bool proxy_embedding(const std::shared_ptr<http_req>& req, const std::shared_ptr
body = req_json["body"].get<std::string>();
url = req_json["url"].get<std::string>();
method = req_json["method"].get<std::string>();
- for(auto& header: req_json["headers"].items()) {
- headers[header.key()] = header.value().get<std::string>();
- }
+ headers = req_json["headers"].get<std::unordered_map<std::string, std::string>>();
} catch(const std::exception& e) {
LOG(ERROR) << "JSON error: " << e.what();
res->set_400("Bad JSON.");
@@ -2171,7 +2169,8 @@ bool proxy_embedding(const std::shared_ptr<http_req>& req, const std::shared_ptr
auto response = proxy.send(url, method, body, headers);
if(response.status_code != 200) {
- res->set(response.status_code, response.body);
+ int code = response.status_code;
+ res->set_body(code, response.body);
return false;
}
diff --git a/src/http_proxy.cpp b/src/http_proxy.cpp
index 19e8b61c..1ff4b826 100644
--- a/src/http_proxy.cpp
+++ b/src/http_proxy.cpp
@@ -10,13 +10,12 @@ HttpProxy::HttpProxy() : cache(30s){
http_proxy_res_t HttpProxy::send(const std::string& url, const std::string& method, const std::string& body, const std::unordered_map<std::string, std::string>& headers) {
// check if url is in cache
- std::string key;
- key += url;
- key += method;
- key += body;
+ uint64_t key = StringUtils::hash_wy(url.c_str(), url.size());
+ key = StringUtils::hash_combine(key, StringUtils::hash_wy(method.c_str(), method.size()));
+ key = StringUtils::hash_combine(key, StringUtils::hash_wy(body.c_str(), body.size()));
for (auto& header : headers) {
- key += header.first;
- key += header.second;
+ key = StringUtils::hash_combine(key, StringUtils::hash_wy(header.first.c_str(), header.first.size()));
+ key = StringUtils::hash_combine(key, StringUtils::hash_wy(header.second.c_str(), header.second.size()));
}
if (cache.contains(key)) {
return cache[key];
@@ -25,6 +24,10 @@ http_proxy_res_t HttpProxy::send(const std::string& url, const std::string& meth
HttpClient& client = HttpClient::get_instance();
http_proxy_res_t res;
+ for (auto& header : headers) {
+ LOG(INFO) << header.first << ": " << header.second;
+ }
+
if(method == "GET") {
res.status_code = client.get_response(url, res.body, res.headers, headers);
} else if(method == "POST") {
diff --git a/src/index.cpp b/src/index.cpp
index ff4bdd03..4dda00c3 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -500,7 +500,7 @@ void Index::validate_and_preprocess(Index *index, std::vector<index_record>& ite
index_rec.index_failure(400, e.what());
}
}
-
+
auto embed_op = batch_embed_fields(docs_to_embed, embedding_fields, search_schema);
if(!embed_op.ok()) {
for(size_t i = 0; i < batch_size; i++) {
@@ -6489,8 +6489,15 @@ Option<bool> Index::batch_embed_fields(std::vector<nlohmann::json*>& documents,
}
}
}
- texts_to_embed.push_back(std::make_pair(document, text));
+ if(!text.empty()) {
+ texts_to_embed.push_back(std::make_pair(document, text));
+ }
+ }
+
+ if(texts_to_embed.empty()) {
+ continue;
}
+
TextEmbedderManager& embedder_manager = TextEmbedderManager::get_instance();
auto embedder_op = embedder_manager.get_text_embedder(field.embed[fields::model_config]);
@@ -6512,7 +6519,6 @@ Option<bool> Index::batch_embed_fields(std::vector<nlohmann::json*>& documents,
}
auto embedding_op = embedder_op.get()->batch_embed(texts);
-
if(!embedding_op.ok()) {
return Option<bool>(400, embedding_op.error());
}
diff --git a/src/main/typesense_server.cpp b/src/main/typesense_server.cpp
index f57f15c4..937c00e5 100644
--- a/src/main/typesense_server.cpp
+++ b/src/main/typesense_server.cpp
@@ -100,7 +100,7 @@ void master_server_routes() {
server->post("/config", post_config, false, false);
// for proxying remote embedders
- server->post("/proxy", proxy_embedding);
+ server->post("/proxy", post_proxy);
}
void (*backward::SignalHandling::_callback)(int sig, backward::StackTrace&) = nullptr;
diff --git a/src/raft_server.cpp b/src/raft_server.cpp
index d5912fd1..920b6b34 100644
--- a/src/raft_server.cpp
+++ b/src/raft_server.cpp
@@ -219,15 +219,6 @@ void ReplicationState::write(const std::shared_ptr<http_req>& request, const std
return write_to_leader(request, response);
}
- route_path* rpath;
- bool found = server->get_route(request->route_hash, &rpath);
- found = found && rpath->handler == proxy_embedding;
- if(found && !node->is_leader()) {
- response->set_500("Proxy embedding is only supported on leader.");
- auto req_res = new async_req_res_t(request, response, true);
- return message_dispatcher->send_message(HttpServer::STREAM_RESPONSE_MESSAGE, req_res);
- }
-
// Serialize request to replicated WAL so that all the nodes in the group receive it as well.
// NOTE: actual write must be done only on the `on_apply` method to maintain consistency.
diff --git a/src/text_embedder_remote.cpp b/src/text_embedder_remote.cpp
index cb0fe240..423377b0 100644
--- a/src/text_embedder_remote.cpp
+++ b/src/text_embedder_remote.cpp
@@ -19,10 +19,7 @@ long RemoteEmbedder::call_remote_api(const std::string& method, const std::strin
req_body["method"] = method;
req_body["url"] = url;
req_body["body"] = body;
- req_body["headers"] = nlohmann::json::object();
- for(auto& header : req_headers) {
- req_body["headers"][header.first] = header.second;
- }
+ req_body["headers"] = req_headers;
return HttpClient::get_instance().post_response(leader_url, req_body.dump(), res_body, headers, {}, 10000, true);
}
diff --git a/src/typesense_server_utils.cpp b/src/typesense_server_utils.cpp
index 8d7acad7..c628c569 100644
--- a/src/typesense_server_utils.cpp
+++ b/src/typesense_server_utils.cpp
@@ -453,7 +453,7 @@ int run_server(const Config & config, const std::string & version, void (*master
AnalyticsManager::get_instance().run(&replication_state);
});
- RemoteEmbedder::set_raft_server(&replication_state);
+ RemoteEmbedder::init(&replication_state);
std::string path_to_nodes = config.get_nodes();
start_raft_server(replication_state, state_dir, path_to_nodes,