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/template too — completes #451 class C

xerj-org170f41f27db5
diff --git a/engine/crates/xerj-api/src/es_compat.rs b/engine/crates/xerj-api/src/es_compat.rs
index 53324c3f5..8a5794341 100644
--- a/engine/crates/xerj-api/src/es_compat.rs
+++ b/engine/crates/xerj-api/src/es_compat.rs
@@ -30631,7 +30631,7 @@ async fn msearch_template_impl(
.unwrap_or_else(|| "*".to_string());
let params = tmpl_body.params.unwrap_or_default();
- let search_body_val: Value = if let Some(source_val) = tmpl_body.source {
+ let mut search_body_val: Value = if let Some(source_val) = tmpl_body.source {
let source_str = match &source_val {
Value::String(s) => s.clone(),
other => serde_json::to_string(other).unwrap_or_default(),
@@ -30668,6 +30668,40 @@ async fn msearch_template_impl(
continue;
};
+ // AND in any alias filter before parsing (#451 class C), same as
+ // `_msearch`: a filtered alias's document boundary must apply to the
+ // rendered template search too.
+ {
+ 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 = search_body_val.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) = search_body_val.as_object_mut() {
+ obj.insert("query".to_string(), merged);
+ }
+ }
+ }
+
// Same request-time script guard as `_search`/`_msearch`, over the
// same `GuardedField` set; the rendered template is user input and
// this path skips `build_search_request` too.
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
index 5dbbeb18b..e3e029cf4 100644
--- 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
@@ -252,3 +252,53 @@ async fn cat_count_through_a_filtered_alias_honours_the_filter() {
not the whole backing index (#451 class C): {body}"
);
}
+
+/// `_msearch/template` through a filtered alias must honour the filter.
+#[tokio::test]
+async fn msearch_template_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;
+ let (st, _) = call(
+ &app,
+ "PUT",
+ "/idx-a/_alias/hot",
+ json!({ "filter": { "term": { "status": "active" } } }),
+ )
+ .await;
+ assert_eq!(st, StatusCode::OK, "create filtered alias");
+
+ let nd = format!(
+ "{}\n{}\n",
+ json!({ "index": "hot" }),
+ json!({ "source": "{\"query\":{\"match_all\":{}},\"track_total_hits\":true}", "params": {} })
+ );
+ let (status, body) = ndjson(&app, "/_msearch/template", nd).await;
+ assert_eq!(status, StatusCode::OK, "_msearch/template status: {body}");
+ let total = body
+ .pointer("/responses/0/hits/total/value")
+ .and_then(Value::as_u64)
+ .unwrap_or(u64::MAX);
+ assert_eq!(
+ total, 1,
+ "_msearch/template through a filtered alias must honour the filter, not leak the \
+ out-of-slice doc (#451 class C): {body}"
+ );
+}