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.
Rust agent-memoryai-agentsbm25claude-codeclicode-searchcursordatabaseelasticsearchfull-text-searchhybrid-searchjsonllocal-firstmcpmcp-servermodel-context-protocolragrustsearch-enginevector-search
fix(es-compat): fan _eql/search out to every alias member (#451 _eql arm)
xerj-org3a493c52d9d7
diff --git a/engine/crates/xerj-api/src/es_compat.rs b/engine/crates/xerj-api/src/es_compat.rsindex 8a5794341..339fd08a6 100644--- a/engine/crates/xerj-api/src/es_compat.rs+++ b/engine/crates/xerj-api/src/es_compat.rs@@ -33198,45 +33198,50 @@ pub async fn eql_search(Err(e) => return ApiError::new(e).into_response(),};- let idx = match state.engine.get_index(&index) {- Ok(i) => i,- Err(e) => return ApiError::new(xerj_common::XerjError::from(e)).into_response(),+ // Fan out to every member of a multi-index alias (#451 `_eql` arm):+ // eql_search used Engine::get_index -> aliased.first(), silently returning+ // only the first member's events. Search each member and merge the event+ // lists, reporting the concrete member in `_index`.+ let (members, _filters) = match resolve_selector_with_filters(&state, &index).await {+ Ok(t) => t,+ Err(e) => return ApiError::new(e).into_response(),};-- match idx.search(&req).await {- Ok(result) => {- let took_ms = started.elapsed().as_millis() as u64;- let total = result.total.value;- let events: Vec<Value> = result- .hits- .into_iter()- .map(|h| {+ let mut events: Vec<Value> = Vec::new();+ let mut total: u64 = 0;+ for (mname, midx) in &members {+ match midx.search(&req).await {+ Ok(result) => {+ total += result.total.value;+ for h in result.hits {let source = if h.source.is_null() {Value::Null} else {h.source};- json!({- "_index": &index,+ events.push(json!({+ "_index": mname,"_id": h.id,"_source": source,- })- })- .collect();- Json(json!({- "is_partial": false,- "is_running": false,- "took": took_ms,- "timed_out": false,- "hits": {- "total": { "value": total, "relation": "eq" },- "events": events,- },- }))- .into_response()+ }));+ }+ }+ Err(e) => return ApiError::new(xerj_common::XerjError::from(e)).into_response(),}- Err(e) => ApiError::new(xerj_common::XerjError::from(e)).into_response(),}+ // Apply the EQL `size` limit across the merged member results.+ events.truncate(size as usize);+ let took_ms = started.elapsed().as_millis() as u64;+ Json(json!({+ "is_partial": false,+ "is_running": false,+ "took": took_ms,+ "timed_out": false,+ "hits": {+ "total": { "value": total, "relation": "eq" },+ "events": events,+ },+ }))+ .into_response()}// ─────────────────────────────────────────────────────────────────────────────diff --git a/engine/crates/xerj-api/tests/eql_through_alias_sees_every_member.rs b/engine/crates/xerj-api/tests/eql_through_alias_sees_every_member.rsnew file mode 100644index 000000000..43bed7f37--- /dev/null+++ b/engine/crates/xerj-api/tests/eql_through_alias_sees_every_member.rs@@ -0,0 +1,106 @@+//! Issue #451 (failure class B, `_eql/search` arm): `eql_search` resolves the+//! path index with `Engine::get_index`, which collapses an alias to+//! `aliased.first()` — one member. So an EQL search through a multi-index alias+//! silently returns only the first member's events, the same silent+//! wrong-answer as `_mget`/`_explain`/`_cat/segments`/`_segments` (fixed in+//! #555). `_eql/search` was left for last because a correct fan-out has to+//! merge the members' event lists.+//!+//! 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 call(app: &axum::Router, method: &str, path: &str, body: Value) -> (StatusCode, Value) {+ let mut req = Request::builder().method(method).uri(path);+ let body = if body.is_null() {+ Body::empty()+ } else {+ req = req.header("content-type", "application/json");+ Body::from(body.to_string())+ };+ let response = app.clone().oneshot(req.body(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),+ )+}++/// An EQL search through a 2-member alias must return matching events from every+/// member, not just the first member's.+#[tokio::test]+async fn eql_search_through_a_multi_index_alias_sees_every_member() {+ let (app, _dir) = app().await;++ for member in ["idx-a", "idx-b"] {+ let (st, _) = call(+ &app,+ "PUT",+ &format!("/{member}"),+ json!({"mappings": {"properties": {"status": {"type": "keyword"}}}}),+ )+ .await;+ assert_eq!(st, StatusCode::OK, "create {member}");+ // One matching event per member.+ let (st, _) = call(+ &app,+ "POST",+ &format!("/{member}/_doc/{member}"),+ json!({ "status": "active" }),+ )+ .await;+ assert_eq!(st, StatusCode::CREATED, "index into {member}");+ let (st, _) = call(&app, "POST", &format!("/{member}/_refresh"), Value::Null).await;+ assert_eq!(st, StatusCode::OK, "refresh {member}");+ }+ let (st, _) = call(+ &app,+ "POST",+ "/_aliases",+ json!({"actions": [+ {"add": {"index": "idx-a", "alias": "tri"}},+ {"add": {"index": "idx-b", "alias": "tri"}}+ ]}),+ )+ .await;+ assert_eq!(st, StatusCode::OK, "create alias tri");++ let (status, body) = call(+ &app,+ "POST",+ "/tri/_eql/search",+ json!({ "query": "any where status == \"active\"" }),+ )+ .await;+ assert_eq!(status, StatusCode::OK, "_eql/search status: {body}");++ let events = body+ .pointer("/hits/events")+ .and_then(Value::as_array)+ .map(|a| a.len())+ .unwrap_or(usize::MAX);+ assert_eq!(+ events, 2,+ "_eql/search through a 2-member alias must return the matching event from BOTH \+ members, not just the first member's — the alias collapsed to aliased.first() \+ (#451 _eql arm): {body}"+ );+}