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): apply alias filters on _msearch (#451 class C)

xerj-org5d5d2acae425
diff --git a/engine/crates/xerj-api/src/es_compat.rs b/engine/crates/xerj-api/src/es_compat.rs
index a1829081f..2f0b4ead0 100644
--- a/engine/crates/xerj-api/src/es_compat.rs
+++ b/engine/crates/xerj-api/src/es_compat.rs
@@ -22518,6 +22518,44 @@ async fn msearch_impl(
}
}
+ // AND in any alias `filter` before parsing (#451 class C). msearch
+ // resolved the alias to its members and searched them directly, never
+ // reading `index_alias_metadata`, so a filtered alias — the standard
+ // poor-man's document boundary — leaked the documents it exists to hide
+ // (`_search` through the same alias honours it). Gathered keyed on the
+ // written alias name, exactly as `search_impl` and
+ // `resolve_by_query_targets` do.
+ {
+ let mut alias_filters: Vec<Value> = Vec::new();
+ for part in index_name
+ .split(',')
+ .map(str::trim)
+ .filter(|s| !s.is_empty())
+ {
+ if let Some(entry) = state.engine.aliases.get(part) {
+ for backing in entry.value().iter() {
+ if let Some(meta) = state.engine.index_alias_metadata.get(backing) {
+ if let Some(filter) =
+ meta.get(part).and_then(|v| v.get("filter")).cloned()
+ {
+ alias_filters.push(filter);
+ }
+ }
+ }
+ }
+ }
+ if !alias_filters.is_empty() {
+ let existing = effective_body.get("query").cloned();
+ let merged = match existing {
+ None => json!({ "bool": { "filter": alias_filters } }),
+ Some(q) => json!({ "bool": { "must": [q], "filter": alias_filters } }),
+ };
+ if let Some(obj) = effective_body.as_object_mut() {
+ obj.insert("query".to_string(), merged);
+ }
+ }
+ }
+
// Strip _index constraints BEFORE parsing so downstream FTS
// doesn't try to score on a metadata field.
let mut idx_constraints: Vec<String> = Vec::new();
diff --git a/engine/crates/xerj-api/tests/filtered_alias_filter_applies_on_msearch.rs b/engine/crates/xerj-api/tests/filtered_alias_filter_applies_on_msearch.rs
new file mode 100644
index 000000000..7440cbf20
--- /dev/null
+++ b/engine/crates/xerj-api/tests/filtered_alias_filter_applies_on_msearch.rs
@@ -0,0 +1,142 @@
+//! Issue #451 (failure class C): `msearch` (and `search_with_scroll`,
+//! `msearch_template`, `_cat/count`) resolve an alias to its members and search
+//! them directly, but never read `index_alias_metadata`, so an alias's `filter`
+//! is dropped. A filtered alias is the standard poor-man's document-level
+//! boundary, so these paths return document bodies the alias exists to hide —
+//! while `_search` through the same alias honours the filter.
+//!
+//! This test pins the divergence on `_msearch`: a filtered alias that shows one
+//! document via `_search` must show the same one document via `_msearch`, not
+//! leak the out-of-slice document.
+//!
+//! 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),
+ )
+}
+
+async fn ndjson(app: &axum::Router, path: &str, body: String) -> (StatusCode, Value) {
+ let response = app
+ .clone()
+ .oneshot(
+ Request::builder()
+ .method("POST")
+ .uri(path)
+ .header("content-type", "application/x-ndjson")
+ .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),
+ )
+}
+
+/// A filtered alias must apply its filter on `_msearch`, not just `_search`.
+#[tokio::test]
+async fn msearch_through_a_filtered_alias_honours_the_filter() {
+ let (app, _dir) = app().await;
+ let (st, _) = call(
+ &app,
+ "PUT",
+ "/idx-a",
+ json!({"mappings": {"properties": {"status": {"type": "keyword"}}}}),
+ )
+ .await;
+ assert_eq!(st, StatusCode::OK, "create index");
+ for (id, status) in [("active", "active"), ("archived", "archived")] {
+ let (st, _) = call(
+ &app,
+ "PUT",
+ &format!("/idx-a/_doc/{id}"),
+ json!({ "status": status }),
+ )
+ .await;
+ assert_eq!(st, StatusCode::CREATED, "index {id}");
+ }
+ let (_s, _b) = call(&app, "POST", "/idx-a/_refresh", Value::Null).await;
+
+ // Filtered alias: only status=active is in-slice.
+ let (st, body) = call(
+ &app,
+ "PUT",
+ "/idx-a/_alias/hot",
+ json!({ "filter": { "term": { "status": "active" } } }),
+ )
+ .await;
+ assert_eq!(st, StatusCode::OK, "create filtered alias: {body}");
+
+ // Baseline: `_search` through the alias honours the filter -> 1 hit.
+ let (_s, search) = call(
+ &app,
+ "POST",
+ "/hot/_search",
+ json!({ "query": { "match_all": {} }, "track_total_hits": true }),
+ )
+ .await;
+ let search_total = search
+ .pointer("/hits/total/value")
+ .and_then(Value::as_u64)
+ .unwrap_or(u64::MAX);
+ assert_eq!(
+ search_total, 1,
+ "precondition: _search through the filtered alias should see only the in-slice doc: {search}"
+ );
+
+ // `_msearch` through the same alias must ALSO honour the filter.
+ let nd = format!(
+ "{}\n{}\n",
+ json!({ "index": "hot" }),
+ json!({ "query": { "match_all": {} }, "track_total_hits": true })
+ );
+ let (status, body) = ndjson(&app, "/_msearch", nd).await;
+ assert_eq!(status, StatusCode::OK, "_msearch status: {body}");
+ let msearch_total = body
+ .pointer("/responses/0/hits/total/value")
+ .and_then(Value::as_u64)
+ .unwrap_or(u64::MAX);
+ assert_eq!(
+ msearch_total, search_total,
+ "_msearch through a filtered alias leaked the out-of-slice document — it returned \
+ {msearch_total} hits vs {search_total} for _search; the alias filter (a document \
+ boundary) was dropped on the msearch path (#451 class C): {body}"
+ );
+}