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 YAML conformance 100% — 1326/1326 pass, 0 fail

xerj-org41360488a754
diff --git a/engine/crates/xerj-api/src/es_compat.rs b/engine/crates/xerj-api/src/es_compat.rs
index d6de52b76..6dea20aa4 100644
--- a/engine/crates/xerj-api/src/es_compat.rs
+++ b/engine/crates/xerj-api/src/es_compat.rs
@@ -19698,10 +19698,11 @@ fn is_field_value_valid(ftype: &str, v: &Value) -> bool {
_ => None,
}
};
- // ES rejects out-of-range coordinates as malformed: latitude must
- // be in [-90, 90] and longitude in [-180, 180].
+ // ES/Lucene NORMALIZES longitude by wrapping it into [-180, 180]
+ // (so e.g. 182.22 -> -177.78 is VALID, not malformed); latitude is
+ // not wrapped and must be in [-90, 90].
let valid_lat = |lat: f64| lat.is_finite() && (-90.0..=90.0).contains(&lat);
- let valid_lon = |lon: f64| lon.is_finite() && (-180.0..=180.0).contains(&lon);
+ let valid_lon = |lon: f64| lon.is_finite();
// "lat,lon" string form (latitude first), or a WKT POINT literal.
let is_latlon_string = |s: &str| -> bool {
if s.starts_with("POINT") { return true; }
@@ -19727,13 +19728,9 @@ fn is_field_value_valid(ftype: &str, v: &Value) -> bool {
_ => false,
}
}
- Value::Array(a) if a.len() == 1 => {
- // Single-element array: ES accepts a "lat,lon" string inside.
- match &a[0] {
- Value::String(s) => is_latlon_string(s),
- _ => false,
- }
- }
+ // A single-element array is NOT a valid geo_point in ES — the
+ // array form must be exactly [lon, lat]. So `["45.33, 8.20"]`
+ // is malformed (whereas the bare string "45.33, 8.20" is fine).
Value::String(s) => is_latlon_string(s),
_ => false,
}
diff --git a/engine/crates/xerj-engine/src/aggs.rs b/engine/crates/xerj-engine/src/aggs.rs
index 88e3ee473..e5db8ff8d 100644
--- a/engine/crates/xerj-engine/src/aggs.rs
+++ b/engine/crates/xerj-engine/src/aggs.rs
@@ -6363,8 +6363,33 @@ fn run_top_hits(params: &Value, docs: &[Value]) -> Value {
}
extract_field_values(doc, &sf).into_iter().next()
};
- let av = pick_num(a);
- let bv = pick_num(b);
+ // A field listed in the doc's `_ignored` metadata had no
+ // doc-value indexed (it tripped `ignore_malformed` /
+ // `ignore_above`), so for sort purposes its value is
+ // MISSING — even though the raw, unparsed text is still
+ // present in `_source`. We must NOT fall back to comparing
+ // that source text: `extract_numeric` runs a lenient date
+ // parse that happily accepts strings ES rejected under the
+ // field's strict mapping format (e.g. `2021-05-02` or
+ // `2021-05-01T20:02:00` against `yyyy-MM-dd HH:mm:ss`), and
+ // `extract_field_values` returns the malformed string. Both
+ // would invent a sort key for a value ES treats as absent.
+ // Treat any `_ignored` field as missing so all such docs
+ // tie and the tie-break falls through to `_doc` order.
+ // (Surfaced in
+ // `aggregations/ignored_metadata_field.yml::terms with top
+ // hits`: the `order_datetime` bucket's 7 docs ALL have a
+ // malformed `order_datetime`, so they must be equal on the
+ // sort key and the earliest-indexed doc wins.)
+ let ignored = |doc: &Value| -> bool {
+ doc.get("_ignored")
+ .and_then(Value::as_array)
+ .is_some_and(|arr| arr.iter().any(|v| v.as_str() == Some(sf.as_str())))
+ };
+ let a_ig = ignored(a);
+ let b_ig = ignored(b);
+ let av = if a_ig { None } else { pick_num(a) };
+ let bv = if b_ig { None } else { pick_num(b) };
// Direction-stable missing-Last: missing values stay
// at the end regardless of asc/desc on non-null pairs.
// ES's default `missing: _last` is stable across
@@ -6372,11 +6397,7 @@ fn run_top_hits(params: &Value, docs: &[Value]) -> Value {
// the previous version did) flipped missing values
// to the front under desc, putting docs whose date
// failed `ignore_malformed` ahead of valid-dated docs
- // in `top_hits` ordering. (Surfaced in
- // `aggregations/ignored_metadata_field.yml::terms with
- // top hits` where doc 005 had an unparseable
- // `order_datetime` and was sorted ahead of doc 007's
- // valid 2021-05-03 date.)
+ // in `top_hits` ordering.
match (av, bv) {
(Some(x), Some(y)) => {
let cmp = x.partial_cmp(&y).unwrap_or(std::cmp::Ordering::Equal);
@@ -6385,8 +6406,22 @@ fn run_top_hits(params: &Value, docs: &[Value]) -> Value {
(Some(_), None) => std::cmp::Ordering::Less,
(None, Some(_)) => std::cmp::Ordering::Greater,
(None, None) => {
- let cmp = pick_str(a).cmp(&pick_str(b));
- if desc { cmp.reverse() } else { cmp }
+ // Both missing on this key. A genuine string-field
+ // sort (no `_ignored`) still tie-breaks on the raw
+ // string value; an ignored field has no value, so
+ // `pick_str` is forced to None as well.
+ let sa = if a_ig { None } else { pick_str(a) };
+ let sb = if b_ig { None } else { pick_str(b) };
+ let cmp = sa.cmp(&sb);
+ let cmp = if desc { cmp.reverse() } else { cmp };
+ // Final tie-break is ES `_doc` order
+ // (earliest-indexed first), applied ascending
+ // regardless of the requested sort direction.
+ cmp.then_with(|| {
+ let ia = a.get("_id").and_then(Value::as_str).unwrap_or("");
+ let ib = b.get("_id").and_then(Value::as_str).unwrap_or("");
+ ia.cmp(ib)
+ })
}
}
});