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: holt_winters moving_fn + closed-index ignore_unavailable (conformance round 4)

xerj-org1a74c81cc545
diff --git a/engine/crates/xerj-api/src/es_compat.rs b/engine/crates/xerj-api/src/es_compat.rs
index 708c0e3bf..d5b160b6c 100644
--- a/engine/crates/xerj-api/src/es_compat.rs
+++ b/engine/crates/xerj-api/src/es_compat.rs
@@ -3889,12 +3889,8 @@ pub async fn search(
body: OptionalJson<EsSearchBody>,
) -> impl IntoResponse {
let started = Instant::now();
- // A closed index rejects read/search ops with ES index_closed_exception.
- // Strict membership only — frozen/open indices are unaffected (ES allows
- // search on frozen indices).
- if state.engine.closed_indices.contains_key(&index) {
- return closed_index_error(&index);
- }
+ // Closed-index handling is deferred to after index resolution below so
+ // `ignore_unavailable` / `expand_wildcards` are honored per ES semantics.
// Strict: empty body → defaults (ES match-all). Malformed body → 400
// via OptionalJsonRejection, NOT a silent fallback to match-all that
@@ -4189,6 +4185,55 @@ pub async fn search(
}).collect()
};
let index_names: Vec<&str> = index_names.iter().map(|s| s.as_str()).collect();
+ // ── Closed-index resolution (ES expand_wildcards / ignore_unavailable) ──
+ // A closed index cannot be searched. ES rules:
+ // * named explicitly → index_closed_exception (400), unless
+ // ignore_unavailable=true (then skip → empty).
+ // * matched by wildcard → silently dropped unless expand_wildcards
+ // includes "closed"/"all" (default "open"); when
+ // explicitly included, surface the exception.
+ let index_names: Vec<&str> = {
+ let iu = params.ignore_unavailable.as_deref() == Some("true");
+ let ew = params.expand_wildcards.as_deref().unwrap_or("open");
+ let include_closed = ew.split(',').any(|t| {
+ let t = t.trim();
+ t == "closed" || t == "all"
+ });
+ let concrete_tokens: Vec<&str> = raw_names
+ .iter()
+ .copied()
+ .filter(|p| *p != "_all" && !p.contains('*'))
+ .collect();
+ let mut kept: Vec<&str> = Vec::with_capacity(index_names.len());
+ for name in &index_names {
+ if !state.engine.closed_indices.contains_key(*name) {
+ kept.push(*name);
+ continue;
+ }
+ if concrete_tokens.iter().any(|t| t == name) {
+ if iu {
+ continue;
+ } // explicit + ignore_unavailable → skip
+ return closed_index_error(name); // explicit closed → 400
+ } else if include_closed {
+ return closed_index_error(name); // wildcard + expand_wildcards=closed → 400
+ }
+ // wildcard + default open → drop silently
+ }
+ kept
+ };
+ // allow_no_indices=false with nothing left after dropping closed → 404
+ if index_names.is_empty() {
+ let allow_no = params
+ .allow_no_indices
+ .as_deref()
+ .map(|v| v == "true")
+ .unwrap_or(true);
+ let iu = params.ignore_unavailable.as_deref() == Some("true");
+ if !allow_no && !iu {
+ return ApiError::new(xerj_common::XerjError::index_not_found(&index)).into_response();
+ }
+ }
// PIT index_filter: AND it with body.query so every PIT search
// respects the filter chosen at PIT-open time.
diff --git a/engine/crates/xerj-engine/src/aggs.rs b/engine/crates/xerj-engine/src/aggs.rs
index 2670b778a..bfbec4711 100644
--- a/engine/crates/xerj-engine/src/aggs.rs
+++ b/engine/crates/xerj-engine/src/aggs.rs
@@ -1556,31 +1556,44 @@ fn holt_winters(values: &[f64], alpha: f64, beta: f64, gamma: f64, period: usize
}
let n = values.len();
let padding = if multiplicative { 1e-10 } else { 0.0 };
- let s1: f64 = values[..period].iter().sum::<f64>() / period as f64;
- let s2: f64 = values[period..2 * period].iter().sum::<f64>() / period as f64;
- let mut s = s1;
- let mut b = (s2 - s1) / period as f64;
- let mut seasonal: Vec<f64> = vec![0.0; n];
+ // Faithful port of ES HoltWintersModel: vs[i] = values[i] + padding.
+ let vs: Vec<f64> = values.iter().map(|v| v + padding).collect();
+ // Initial level = mean of first season; initial trend is computed but ES
+ // keeps `last_b` at 0, so the seed trend is discarded on the first step.
+ let mut s = 0.0f64;
+ let mut b = 0.0f64;
for i in 0..period {
- seasonal[i] = if multiplicative { values[i] / s1 } else { values[i] - s1 };
+ s += vs[i];
+ b += (vs[i + period] - vs[i]) / period as f64;
+ }
+ s /= period as f64;
+ b /= period as f64;
+ let mut last_s = s;
+ let mut last_b = 0.0f64;
+ // First-season seasonal indices: ES ALWAYS divides here (even additive).
+ let mut seasonal: Vec<f64> = vec![0.0; n];
+ if s != 0.0 {
+ for i in 0..period {
+ seasonal[i] = vs[i] / s;
+ }
}
for i in period..n {
- let s_prev = s;
if multiplicative {
- s = alpha * (values[i] / (seasonal[i - period] + padding))
- + (1.0 - alpha) * (s_prev + b);
- seasonal[i] = gamma * (values[i] / (s + padding))
- + (1.0 - gamma) * seasonal[i - period];
+ s = alpha * (vs[i] / seasonal[i - period]) + (1.0 - alpha) * (last_s + last_b);
+ } else {
+ s = alpha * (vs[i] - seasonal[i - period]) + (1.0 - alpha) * (last_s + last_b);
+ }
+ b = beta * (s - last_s) + (1.0 - beta) * last_b;
+ if multiplicative {
+ seasonal[i] = gamma * (vs[i] / (last_s + last_b)) + (1.0 - gamma) * seasonal[i - period];
} else {
- s = alpha * (values[i] - seasonal[i - period])
- + (1.0 - alpha) * (s_prev + b);
- seasonal[i] = gamma * (values[i] - s)
- + (1.0 - gamma) * seasonal[i - period];
+ seasonal[i] = gamma * (vs[i] - (last_s - last_b)) + (1.0 - gamma) * seasonal[i - period];
}
- b = beta * (s - s_prev) + (1.0 - beta) * b;
+ last_s = s;
+ last_b = b;
}
- let next_season = seasonal[n - period];
- let out = if multiplicative { (s + b) * next_season } else { (s + b) + next_season };
+ let idx = n - period;
+ let out = if multiplicative { (s + b) * seasonal[idx] } else { s + b + seasonal[idx] };
Some(out)
}