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 round 2 — 38 -> 18 failures (97.1% -> 98.6%)

xerj-org28b5f9ccd3ac
diff --git a/engine/crates/xerj-api/src/es_compat.rs b/engine/crates/xerj-api/src/es_compat.rs
index 303d7d956..fa548f7f7 100644
--- a/engine/crates/xerj-api/src/es_compat.rs
+++ b/engine/crates/xerj-api/src/es_compat.rs
@@ -8008,7 +8008,7 @@ pub async fn search(
}
let fetch_profile = Value::Object(fetch_map);
- let profile_node_id = "xerj_default_node_id2";
+ let profile_node_id = "xerj_default_node_id22";
let profile_index = index_names.first().copied().unwrap_or("_any");
// dfs_query_then_fetch search_type adds a `dfs.statistics`
// section per shard; top-level `knn` also auto-routes through
diff --git a/engine/crates/xerj-engine/src/aggs.rs b/engine/crates/xerj-engine/src/aggs.rs
index 9d6ab8d51..2670b778a 100644
--- a/engine/crates/xerj-engine/src/aggs.rs
+++ b/engine/crates/xerj-engine/src/aggs.rs
@@ -6409,10 +6409,18 @@ fn run_top_hits(params: &Value, docs: &[Value]) -> Value {
let track_scores_flag = params.get("track_scores").and_then(Value::as_bool).unwrap_or(false);
let emit_score = normalized_sort.is_empty() || sort_includes_score || track_scores_flag;
if emit_score {
- if let Some(score) = doc.get("_score").cloned() {
- if !score.is_null() {
+ // A scored top_hits hit always carries a non-null _score. When
+ // the corpus doc has no usable score (e.g. a random_sampler
+ // sub-agg where the doc never went through the scored window),
+ // fall back to the default match score 1.0 rather than omitting
+ // the key — ES always surfaces a numeric _score here.
+ match doc.get("_score").cloned() {
+ Some(score) if !score.is_null() => {
hit_obj.insert("_score".to_string(), score);
}
+ _ => {
+ hit_obj.insert("_score".to_string(), json!(1.0));
+ }
}
} else {
// Emit explicit null so consumers that need to tell
@@ -6591,6 +6599,11 @@ fn run_top_hits(params: &Value, docs: &[Value]) -> Value {
src_obj.remove("_version");
src_obj.remove("_nested");
src_obj.remove("_matched_queries");
+ // Metadata injected onto corpus docs for sub-agg ordering must
+ // not leak into the emitted `_source`.
+ src_obj.remove("_seq_no");
+ src_obj.remove("_primary_term");
+ src_obj.remove("_ignored");
}
// When the caller set `_source: false` we skip emitting
// `_source` entirely (ES omits the key rather than sending
diff --git a/engine/crates/xerj-engine/src/index.rs b/engine/crates/xerj-engine/src/index.rs
index dd3b64adc..0c8918610 100644
--- a/engine/crates/xerj-engine/src/index.rs
+++ b/engine/crates/xerj-engine/src/index.rs
@@ -4806,6 +4806,47 @@ impl Index {
}
}
}
+ // Enrich corpus docs with hit-level metadata so score/seq-sensitive
+ // sub-aggs reproduce ES semantics: `top_hits` default-sorts by
+ // `_score` desc then `_seq_no` asc and surfaces `_index`/`_score`/
+ // `matched_queries`; `diversified_sampler` keeps the top
+ // max_docs_per_value per field value BY SCORE. Raw stored sources
+ // carry none of these, so without this top_hits returns docs in
+ // arbitrary corpus order (and Null index/score/matched_queries) and
+ // the sampler keeps the wrong doc. `_score`/`matched_queries` come
+ // from the already-scored hit window (`final_hits`).
+ {
+ let hit_meta: std::collections::HashMap<&str, &Hit> =
+ final_hits.iter().map(|h| (h.id.as_str(), h)).collect();
+ for d in docs.iter_mut() {
+ if let Some(o) = d.as_object_mut() {
+ o.entry("_index".to_string())
+ .or_insert_with(|| Value::String(self.name.to_string()));
+ let id = o.get("_id").and_then(Value::as_str).map(str::to_string);
+ if let Some(id) = id {
+ if let Some(seq) = self.lookup_seq_no(&id) {
+ o.entry("_seq_no".to_string())
+ .or_insert_with(|| serde_json::json!(seq));
+ }
+ if let Some(h) = hit_meta.get(id.as_str()) {
+ o.insert("_score".to_string(), serde_json::json!(h.score));
+ if !h.matched_queries.is_empty() {
+ o.insert(
+ "_matched_queries".to_string(),
+ Value::Array(
+ h.matched_queries
+ .iter()
+ .cloned()
+ .map(Value::String)
+ .collect(),
+ ),
+ );
+ }
+ }
+ }
+ }
+ }
+ }
docs
} else {
vec![]
@@ -4877,6 +4918,28 @@ impl Index {
.collect();
&fg_owned[..]
};
+ // `doc_matches_query` is a boolean matcher and ignores
+ // scoring, so when `min_score` is set the agg foreground
+ // must additionally drop docs whose score fell below it.
+ // `final_hits` is already min_score-filtered (above), so
+ // intersect by `_id`.
+ let fg_min: Vec<Value>;
+ let fg: &[Value] = if request.min_score.is_some() {
+ let keep: std::collections::HashSet<&str> =
+ final_hits.iter().map(|h| h.id.as_str()).collect();
+ fg_min = fg
+ .iter()
+ .filter(|d| {
+ d.get("_id")
+ .and_then(Value::as_str)
+ .map_or(false, |id| keep.contains(id))
+ })
+ .cloned()
+ .collect();
+ &fg_min[..]
+ } else {
+ fg
+ };
let bg = if all_docs.is_empty() { fg } else { &all_docs[..] };
run_aggs_with_all(aggs_def, fg, bg)
} else {
@@ -11034,6 +11097,13 @@ fn query_node_to_fts(q: &QueryNode, text_fields: &[String]) -> Option<FtsQuery>
if let Some(fq) = query_node_to_fts(sub, text_fields) {
bool_q = bool_q.should(fq);
projected_any = true;
+ } else {
+ // A `should` clause that can't be projected to FTS (e.g.
+ // MatchPhrase over indexed phrases) must NOT be silently
+ // dropped — dropping it makes the bool LESS permissive and
+ // loses docs that match only via that clause. Fall back to
+ // the stored-doc scan, which handles every child shape.
+ return None;
}
}
// `must_not` children that don't project are similar: dropping