Skip to content

web search done right

warmup search caches so the first human searches are faster

Mikkel Denker0f8c074da83e
diff --git a/crates/core/src/api/mod.rs b/crates/core/src/api/mod.rs
index e6bcfc03..841b710c 100644
--- a/crates/core/src/api/mod.rs
+++ b/crates/core/src/api/mod.rs
@@ -61,6 +61,8 @@ pub mod search;
pub mod user_count;
mod webgraph;
+const WARMUP_QUERIES: usize = 100;
+
pub struct Counters {
pub search_counter_success: crate::metrics::Counter,
pub search_counter_fail: crate::metrics::Counter,
@@ -189,6 +191,7 @@ pub async fn router(
log::info!("Search nodes joined the cluster");
}
+ log::info!("Building autosuggest");
let autosuggest = Autosuggest::from_key_phrases(
dist_searcher
.top_key_phrases(config.top_phrases_for_autosuggest)
@@ -217,6 +220,16 @@ pub async fn router(
searcher = searcher.with_dual_encoder(dual_encoder_model);
}
+ if cluster.members().await.into_iter().all(|m| {
+ !matches!(m.service, crate::distributed::member::Service::Api { .. })
+ && cluster.self_node().unwrap().id < m.id
+ }) {
+ log::info!("Warming up searchers");
+ searcher
+ .warmup(autosuggest.scores().keys().take(WARMUP_QUERIES).cloned())
+ .await;
+ }
+
let host_webgraph = Arc::new(host_webgraph);
let page_webgraph = Arc::new(page_webgraph);
diff --git a/crates/core/src/searcher/api/mod.rs b/crates/core/src/searcher/api/mod.rs
index 1ee3d739..fb2fe0ec 100644
--- a/crates/core/src/searcher/api/mod.rs
+++ b/crates/core/src/searcher/api/mod.rs
@@ -684,4 +684,15 @@ where
.get_entity_image(image_id, max_height, max_width)
.await
}
+
+ pub async fn warmup(&self, queries: impl Iterator<Item = String>) {
+ for query in queries {
+ self.search_websites(&SearchQuery {
+ query,
+ ..Default::default()
+ })
+ .await
+ .ok();
+ }
+ }
}