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: collapse + track_scores max_score over the full population (conformance round 5)

xerj-org451470babf16
diff --git a/engine/crates/xerj-api/src/es_compat.rs b/engine/crates/xerj-api/src/es_compat.rs
index d5b160b6c..4691735ad 100644
--- a/engine/crates/xerj-api/src/es_compat.rs
+++ b/engine/crates/xerj-api/src/es_compat.rs
@@ -5137,6 +5137,9 @@ pub async fn search(
let mut total_count: u64 = 0;
let mut total_relation = "eq".to_string();
let mut merged_aggs: Option<Value> = None;
+ // Population max score across ALL matched docs (pre-collapse) merged from
+ // each per-index result, so collapse+track_scores reports ES `max_score`.
+ let mut merged_population_max: Option<f64> = None;
let mut any_timed_out = false;
// Request-level index-resolution policy. Wildcards default to silently
@@ -5217,6 +5220,11 @@ pub async fn search(
if result.total.relation == xerj_query::executor::TotalHitsRelation::Gte {
total_relation = "gte".to_string();
}
+ if let Some(ms) = result.max_score {
+ merged_population_max = Some(
+ merged_population_max.map_or(ms as f64, |m: f64| m.max(ms as f64)),
+ );
+ }
if let Some(new_aggs) = result.aggs {
match &mut merged_aggs {
None => merged_aggs = Some(new_aggs),
@@ -5704,12 +5712,16 @@ pub async fn search(
let explicit_field_sort = !search_req.sort.is_empty()
&& search_req.sort.iter().any(|s| !s.is_score());
if explicit_field_sort {
- merged_hits
- .iter()
- .map(|(_, h)| h.score as f64)
- .fold(None, |acc: Option<f64>, s| {
- Some(match acc { Some(m) if m >= s => m, _ => s })
- })
+ // Prefer the per-index pre-collapse population max; fall back to the
+ // (possibly collapsed/paged) merged hit set when unavailable.
+ merged_population_max.or_else(|| {
+ merged_hits
+ .iter()
+ .map(|(_, h)| h.score as f64)
+ .fold(None, |acc: Option<f64>, s| {
+ Some(match acc { Some(m) if m >= s => m, _ => s })
+ })
+ })
} else {
merged_hits.first().map(|(_, h)| h.score as f64)
}
diff --git a/engine/crates/xerj-engine/src/index.rs b/engine/crates/xerj-engine/src/index.rs
index 0c8918610..147a6e5c4 100644
--- a/engine/crates/xerj-engine/src/index.rs
+++ b/engine/crates/xerj-engine/src/index.rs
@@ -2652,6 +2652,7 @@ impl Index {
aggs: None,
timed_out: false,
profile: None,
+ max_score: None,
})
}
@@ -2806,6 +2807,7 @@ impl Index {
aggs: None,
timed_out: false,
profile: None,
+ max_score: None,
})
}
@@ -2942,6 +2944,7 @@ impl Index {
aggs: None,
timed_out: false,
profile: None,
+ max_score: None,
})
}
@@ -3454,6 +3457,7 @@ impl Index {
aggs: None,
timed_out: true,
profile: None,
+ max_score: None,
})
}
}
@@ -4973,6 +4977,15 @@ impl Index {
};
let agg_nanos = agg_start.elapsed().as_nanos() as u64;
+ // Population max over ALL matched docs (pre-collapse, pre-page) so the
+ // API can report ES `max_score` with collapse + track_scores correctly
+ // (ES reports the max over the whole result set, not just the collapsed
+ // top hit per group).
+ let population_max_score: Option<f32> = final_hits
+ .iter()
+ .map(|h| h.score)
+ .fold(None, |acc: Option<f32>, s| Some(acc.map_or(s, |m| m.max(s))));
+
// --- Apply field collapsing (deduplicate by field value, keep top hit per value) ---
// Skipped here when rescore was present (we already collapsed before rescore).
let final_hits = if let Some(collapse) = &request.collapse {
@@ -5041,6 +5054,7 @@ impl Index {
aggs: agg_result,
timed_out: false,
profile,
+ max_score: population_max_score,
})
}
diff --git a/engine/crates/xerj-query/src/executor.rs b/engine/crates/xerj-query/src/executor.rs
index c1016513f..843ab606f 100644
--- a/engine/crates/xerj-query/src/executor.rs
+++ b/engine/crates/xerj-query/src/executor.rs
@@ -114,6 +114,10 @@ pub struct SearchResult {
/// Profile data — timing breakdown for query execution phases.
#[serde(skip_serializing_if = "Option::is_none")]
pub profile: Option<serde_json::Value>,
+ /// Highest score across ALL matched docs, before collapse/pagination.
+ /// Used for ES `max_score` with collapse + track_scores (search/111).
+ #[serde(skip_serializing_if = "Option::is_none", default)]
+ pub max_score: Option<f32>,
}
impl SearchResult {
@@ -126,6 +130,7 @@ impl SearchResult {
aggs: None,
timed_out: false,
profile: None,
+ max_score: None,
}
}
}