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(engine): unwrap single-clause bool through function_score and dis_max (#643)

xerj-orgec0dd9c185e9
diff --git a/engine/crates/xerj-engine/src/index.rs b/engine/crates/xerj-engine/src/index.rs
index 825b817e1..d48ddbf49 100644
--- a/engine/crates/xerj-engine/src/index.rs
+++ b/engine/crates/xerj-engine/src/index.rs
@@ -35120,6 +35120,33 @@ fn unwrap_single_clause_bool(q: QueryNode) -> QueryNode {
name,
query: Box::new(unwrap_single_clause_bool(*query)),
},
+ // #643: `function_score` merges its inner query's score via `boost_mode`,
+ // so erasing a single-clause `bool` underneath it is score-preserving —
+ // the same recursion Lucene's FunctionScoreQuery.rewrite performs over
+ // its wrapped query. Closes the wrapper the earlier unwrap left un-erased.
+ QueryNode::FunctionScore {
+ query,
+ functions,
+ score_mode,
+ boost_mode,
+ max_boost,
+ } => QueryNode::FunctionScore {
+ query: Box::new(unwrap_single_clause_bool(*query)),
+ functions,
+ score_mode,
+ boost_mode,
+ max_boost,
+ },
+ // #643: `dis_max` scores `max(sub-scores) + tie_breaker*Σ(rest)`; erasing a
+ // single-clause `bool` in any disjunct leaves that disjunct's score
+ // unchanged, so recurse into every query (DisjunctionMaxQuery.rewrite).
+ QueryNode::DisMax {
+ queries,
+ tie_breaker,
+ } => QueryNode::DisMax {
+ queries: queries.into_iter().map(unwrap_single_clause_bool).collect(),
+ tie_breaker,
+ },
other => other,
}
}
@@ -47178,3 +47205,73 @@ pub mod graph;
#[cfg(test)]
#[path = "wal_shard_settings_tests.rs"]
mod wal_shard_settings_tests;
+
+#[cfg(test)]
+mod unwrap_single_clause_643_wrapper_tests {
+ use super::*;
+ use xerj_query::ast::{BoolOperator, QueryNode};
+
+ fn bare_match() -> QueryNode {
+ QueryNode::Match {
+ field: "body".into(),
+ query: "x".into(),
+ operator: BoolOperator::Or,
+ analyzer: None,
+ boost: None,
+ minimum_should_match: None,
+ }
+ }
+
+ fn single_clause_bool() -> QueryNode {
+ QueryNode::Bool {
+ must: vec![bare_match()],
+ should: vec![],
+ must_not: vec![],
+ filter: vec![],
+ minimum_should_match: None,
+ }
+ }
+
+ /// #643: `unwrap_single_clause_bool` must recurse through `function_score`'s
+ /// inner query (a score-preserving wrapper), erasing the nested one-clause
+ /// bool to the bare clause.
+ #[test]
+ fn unwrap_recurses_through_function_score() {
+ let q = QueryNode::FunctionScore {
+ query: Box::new(single_clause_bool()),
+ functions: vec![],
+ score_mode: Default::default(),
+ boost_mode: Default::default(),
+ max_boost: None,
+ };
+ match unwrap_single_clause_bool(q) {
+ QueryNode::FunctionScore { query, .. } => assert!(
+ matches!(*query, QueryNode::Match { .. }),
+ "the single-clause bool inside function_score must be erased to \
+ the bare clause, got {:?}",
+ query
+ ),
+ other => panic!("function_score wrapper must be preserved, got {:?}", other),
+ }
+ }
+
+ /// #643: same for every disjunct of a `dis_max`.
+ #[test]
+ fn unwrap_recurses_through_dis_max() {
+ let q = QueryNode::DisMax {
+ queries: vec![single_clause_bool()],
+ tie_breaker: 0.0,
+ };
+ match unwrap_single_clause_bool(q) {
+ QueryNode::DisMax { queries, .. } => {
+ assert_eq!(queries.len(), 1);
+ assert!(
+ matches!(queries[0], QueryNode::Match { .. }),
+ "the single-clause bool inside dis_max must be erased, got {:?}",
+ queries[0]
+ );
+ }
+ other => panic!("dis_max wrapper must be preserved, got {:?}", other),
+ }
+ }
+}
diff --git a/engine/crates/xerj-engine/tests/bool_single_clause_is_score_neutral.rs b/engine/crates/xerj-engine/tests/bool_single_clause_is_score_neutral.rs
index e12219c88..6013517b6 100644
--- a/engine/crates/xerj-engine/tests/bool_single_clause_is_score_neutral.rs
+++ b/engine/crates/xerj-engine/tests/bool_single_clause_is_score_neutral.rs
@@ -358,3 +358,41 @@ async fn must_not_only_bool_is_not_unwrapped() {
);
}
}
+
+/// #643 remaining gap: a single-clause `bool` nested inside a score-preserving
+/// wrapper that `unwrap_single_clause_bool` does NOT recurse through
+/// (`function_score` inner query, `dis_max` single query) keeps the wrapped
+/// `bool`, so #399's memtable/segment score divergence can resurface. A
+/// no-functions `function_score` and a single-query `dis_max` (tie_breaker 0)
+/// are both score-neutral, so each must rank exactly like the bare clause on
+/// BOTH populations. Fail-before: the nested `bool` is not erased, so the
+/// memtable diverges from the bare clause.
+#[tokio::test]
+async fn nested_single_clause_bool_in_wrappers_is_score_neutral() {
+ let shapes = [
+ (
+ "function_score[bool.must[1]]",
+ json!({"function_score": {"query": {"bool": {"must": [text_query()]}}}}),
+ ),
+ (
+ "dis_max[bool.must[1]]",
+ json!({"dis_max": {"queries": [{"bool": {"must": [text_query()]}}], "tie_breaker": 0.0}}),
+ ),
+ ];
+ for flush in [false, true] {
+ let (_dir, idx) = seed(&format!("bool-643-nested-{flush}"), flush).await;
+ // Reference: the SAME wrapper around the BARE clause (isolates the inner
+ // bool-unwrap effect from any wrapper scoring).
+ for (label, wrapped) in &shapes {
+ let bare_wrapped = match label {
+ l if l.starts_with("function_score") => {
+ json!({"function_score": {"query": text_query()}})
+ }
+ _ => json!({"dis_max": {"queries": [text_query()], "tie_breaker": 0.0}}),
+ };
+ let reference = idx.search(&req(bare_wrapped, 50)).await.unwrap();
+ let actual = idx.search(&req(wrapped.clone(), 50)).await.unwrap();
+ assert_same_ranking(&format!("{label} flush={flush}"), &reference, &actual);
+ }
+ }
+}