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): apply alias filters on _cat/count too (#451 class C)
xerj-org6d3fc1118ddf
diff --git a/engine/crates/xerj-api/src/es_compat.rs b/engine/crates/xerj-api/src/es_compat.rsindex 2f0b4ead0..53324c3f5 100644--- a/engine/crates/xerj-api/src/es_compat.rs+++ b/engine/crates/xerj-api/src/es_compat.rs@@ -25166,10 +25166,43 @@ pub async fn cat_count(// is scoped to wildcard resolution, not to making a bad literal name a// hard error the way the JSON `_count` endpoint does).let names = resolve_index_selector(&state, &index).await;++ // A filtered alias must count only the slice its filter admits (#451 class+ // C) — `stats().doc_count` is the whole backing index, so a filtered alias+ // leaked a count including the documents it exists to hide. Gathered keyed+ // on the written alias name, as the other read paths do.+ let mut alias_filters: Vec<Value> = Vec::new();+ for part in index.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);+ }+ }+ }+ }+ }+let mut count: u64 = 0;for name in &names {if let Ok(idx) = state.engine.get_index(name) {- count += idx.stats().await.doc_count;+ if alias_filters.is_empty() {+ count += idx.stats().await.doc_count;+ } else {+ // Count only the filter-admitted documents.+ let filter_query = if alias_filters.len() == 1 {+ alias_filters[0].clone()+ } else {+ json!({ "bool": { "filter": alias_filters.clone() } })+ };+ let body = json!({ "query": filter_query, "size": 0, "track_total_hits": true });+ if let Ok(req) = xerj_query::parse_request(&body) {+ if let Ok(res) = idx.search(&req).await {+ count += res.total.value;+ }+ }+ }}}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.rsindex 27347e14c..5dbbeb18b 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@@ -201,3 +201,54 @@ async fn scroll_through_a_filtered_alias_honours_the_filter() {"scroll first page must return only the in-slice doc: {body}");}++/// `_cat/count` through a filtered alias must count only the in-slice docs.+#[tokio::test]+async fn cat_count_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 (status, body) = call(&app, "GET", "/_cat/count/hot?format=json", Value::Null).await;+ assert_eq!(status, StatusCode::OK, "_cat/count status: {body}");+ let count: u64 = body+ .as_array()+ .and_then(|a| a.first())+ .and_then(|r| r.get("count"))+ .and_then(|c| {+ c.as_str()+ .and_then(|s| s.parse().ok())+ .or_else(|| c.as_u64())+ })+ .unwrap_or(u64::MAX);+ assert_eq!(+ count, 1,+ "_cat/count through a filtered alias must count only the in-slice (active) doc, \+ not the whole backing index (#451 class C): {body}"+ );+}