Skip to content

armgit / xerj

Public Imported from GitHub
0

AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.

fix(es-compat): _msearch reports the envelope-level `took` (#1288)

Vincenzo Lombardo57379ff7e5c2
diff --git a/engine/crates/xerj-api/src/es_compat.rs b/engine/crates/xerj-api/src/es_compat.rs
index 767a40351..29ca83166 100644
--- a/engine/crates/xerj-api/src/es_compat.rs
+++ b/engine/crates/xerj-api/src/es_compat.rs
@@ -25428,6 +25428,9 @@ async fn msearch_impl(
body: bytes::Bytes,
default_index: Option<String>,
) -> axum::response::Response {
+ // Envelope `took` (#1288): ES 8.x reports the wall time of the whole
+ // multi-search next to `responses`, as `msearch_template_impl` does.
+ let msearch_started = Instant::now();
let text = match std::str::from_utf8(&body) {
Ok(t) => t,
Err(_) => {
@@ -25815,7 +25818,11 @@ async fn msearch_impl(
responses.push(resp);
}
- Json(json!({ "responses": responses })).into_response()
+ Json(json!({
+ "took": msearch_started.elapsed().as_millis() as u64,
+ "responses": responses,
+ }))
+ .into_response()
}
// ─────────────────────────────────────────────────────────────────────────────
diff --git a/engine/crates/xerj-api/tests/msearch_reports_top_level_took.rs b/engine/crates/xerj-api/tests/msearch_reports_top_level_took.rs
new file mode 100644
index 000000000..42ccf3401
--- /dev/null
+++ b/engine/crates/xerj-api/tests/msearch_reports_top_level_took.rs
@@ -0,0 +1,94 @@
+//! Issue #1288: an ES 8.x `_msearch` response carries a top-level `took`
+//! (wall time of the whole multi-search, in ms) next to `responses`. XERJ
+//! returned only `responses`, so a dashboard reading request time from it got
+//! `undefined`. `_msearch/template` already emitted it; this pins `_msearch`
+//! on both its routes.
+//!
+//! Elasticsearch is referenced for wire semantics only; no ES code is
+//! reproduced here.
+
+use axum::body::Body;
+use axum::http::{Request, StatusCode};
+use serde_json::{json, Value};
+use tower::ServiceExt;
+
+async fn app() -> (axum::Router, tempfile::TempDir) {
+ let dir = tempfile::tempdir().expect("tempdir");
+ let mut config = xerj_common::config::Config::default();
+ config.server.data_dir = dir.path().to_string_lossy().into_owned();
+ config.storage.wal_sync = xerj_common::config::WalSync::Async;
+ let metrics = xerj_common::metrics::Metrics::new().expect("metrics");
+ let engine = xerj_engine::Engine::new(config.clone()).expect("engine");
+ let state = xerj_api::state::AppState::new(config, engine, metrics);
+ (xerj_api::router::build_es_compat_router(state), dir)
+}
+
+async fn send(
+ app: &axum::Router,
+ method: &str,
+ path: &str,
+ ctype: &str,
+ body: String,
+) -> (StatusCode, Value) {
+ let mut req = Request::builder().method(method).uri(path);
+ if !body.is_empty() {
+ req = req.header("content-type", ctype);
+ }
+ let response = app
+ .clone()
+ .oneshot(req.body(Body::from(body)).unwrap())
+ .await
+ .unwrap();
+ let status = response.status();
+ let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)
+ .await
+ .unwrap();
+ (
+ status,
+ serde_json::from_slice(&bytes).unwrap_or(Value::Null),
+ )
+}
+
+#[tokio::test]
+async fn msearch_response_has_top_level_took() {
+ let (app, _dir) = app().await;
+ let (st, _) = send(
+ &app,
+ "PUT",
+ "/repro",
+ "application/json",
+ json!({"mappings": {"properties": {"host": {"type": "keyword"}}}}).to_string(),
+ )
+ .await;
+ assert_eq!(st, StatusCode::OK, "create index");
+ let (st, _) = send(
+ &app,
+ "PUT",
+ "/repro/_doc/1?refresh=true",
+ "application/json",
+ json!({"host": "a"}).to_string(),
+ )
+ .await;
+ assert_eq!(st, StatusCode::CREATED, "index doc");
+
+ // One good item and one per-item error: `took` belongs to the envelope
+ // whatever the items carry.
+ let body = "{\"index\":\"repro\"}\n{\"size\":0,\"query\":{\"match_all\":{}}}\n\
+ {\"index\":\"missing-index\"}\n{\"size\":0}\n"
+ .to_string();
+ for path in ["/_msearch", "/repro/_msearch"] {
+ let (st, resp) = send(&app, "POST", path, "application/x-ndjson", body.clone()).await;
+ assert_eq!(st, StatusCode::OK, "{path}: {resp}");
+ assert!(
+ resp.get("took").map(Value::is_u64).unwrap_or(false),
+ "{path}: top-level `took` must be a non-negative integer, got: {resp}"
+ );
+ let items = resp["responses"].as_array().expect("responses array");
+ assert_eq!(items.len(), 2, "{path}: {resp}");
+ assert_eq!(items[0]["status"], 200, "{path}: {resp}");
+ assert!(
+ items[0]["took"].is_u64(),
+ "{path}: per-item took kept: {resp}"
+ );
+ }
+}