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(query): standalone keyword prefix/wildcard → constant_score (ES-exact max_score)

xerj-org77781b9fd43c
diff --git a/engine/crates/xerj-engine/src/index.rs b/engine/crates/xerj-engine/src/index.rs
index 2343c4602..17a5bdfe8 100644
--- a/engine/crates/xerj-engine/src/index.rs
+++ b/engine/crates/xerj-engine/src/index.rs
@@ -14173,19 +14173,23 @@ fn strip_nested_path_in_query(q: &QueryNode, path: &str) -> QueryNode {
field,
value,
boost,
+ constant_score,
} => QueryNode::Prefix {
field: strip(field),
value: value.clone(),
boost: *boost,
+ constant_score: *constant_score,
},
QueryNode::Wildcard {
field,
value,
boost,
+ constant_score,
} => QueryNode::Wildcard {
field: strip(field),
value: value.clone(),
boost: *boost,
+ constant_score: *constant_score,
},
QueryNode::Exists { field } => QueryNode::Exists {
field: strip(field),
@@ -19395,6 +19399,7 @@ fn query_node_to_fts(
field,
value,
boost,
+ constant_score,
} => {
// `prefix` on a KEYWORD field: whole-value, case-sensitive prefix
// match over the keyword FST term dictionary — byte-identical to ES
@@ -19417,7 +19422,11 @@ fn query_node_to_fts(
prefix: value.clone(),
boost: boost.unwrap_or(1.0),
max_expansions: usize::MAX,
- constant_score: false,
+ // ES rewrites a standalone `prefix` query to a constant_score
+ // (every keyword match scores `boost`, no BM25). The shared
+ // query_string / match_bool_prefix lowerings pass `false` and
+ // keep BM25.
+ constant_score: *constant_score,
}));
}
// TEXT field: expand the prefix against the analyzed term dictionary
@@ -19444,6 +19453,7 @@ fn query_node_to_fts(
field,
value,
boost,
+ constant_score,
} => {
// `wildcard` on a KEYWORD field: expand the keyword FST term
// dictionary to every term matching the pattern (`*`=0+ chars,
@@ -19464,7 +19474,11 @@ fn query_node_to_fts(
pattern: value.clone(),
boost: boost.unwrap_or(1.0),
case_insensitive: true,
- constant_score: false,
+ // ES rewrites a standalone `wildcard` query to a
+ // constant_score. The `term{case_insensitive}` /
+ // query_string lowerings that SHARE this node pass `false`
+ // and keep BM25 — do not blanket-flip this.
+ constant_score: *constant_score,
}));
}
// TEXT field: expand the pattern against the analyzed term
@@ -21287,6 +21301,7 @@ mod fts_projection_tests {
field: "body".into(),
value: "run".into(),
boost: None,
+ constant_score: true,
};
match query_node_to_fts(&q, &tf, &kw(&[])).expect("text prefix projects") {
FtsQuery::Prefix(p) => {
@@ -21300,6 +21315,7 @@ mod fts_projection_tests {
field: "body".into(),
value: "run*".into(),
boost: None,
+ constant_score: true,
};
match query_node_to_fts(&q, &tf, &kw(&[])).expect("text wildcard projects") {
FtsQuery::Wildcard(w) => {
diff --git a/engine/crates/xerj-engine/src/sql.rs b/engine/crates/xerj-engine/src/sql.rs
index 690536b6c..492779f75 100644
--- a/engine/crates/xerj-engine/src/sql.rs
+++ b/engine/crates/xerj-engine/src/sql.rs
@@ -617,6 +617,7 @@ fn parse_condition(tokens: &[Token], pos: &mut usize) -> Result<QueryNode, Strin
field: field.clone(),
value: pattern,
boost: None,
+ constant_score: false,
}
}
"not_like" => {
@@ -633,6 +634,7 @@ fn parse_condition(tokens: &[Token], pos: &mut usize) -> Result<QueryNode, Strin
field: field.clone(),
value: pattern,
boost: None,
+ constant_score: false,
}],
minimum_should_match: None,
}
diff --git a/engine/crates/xerj-query/src/ast.rs b/engine/crates/xerj-query/src/ast.rs
index c3616f2a0..21d7d269f 100644
--- a/engine/crates/xerj-query/src/ast.rs
+++ b/engine/crates/xerj-query/src/ast.rs
@@ -265,6 +265,13 @@ pub struct DistanceFeature {
pub origin: serde_json::Value,
}
+/// serde `skip_serializing_if` helper: omit a `bool` field when it is `false`
+/// so query round-trips stay byte-identical to their pre-`constant_score` form.
+#[inline]
+fn is_false(b: &bool) -> bool {
+ !*b
+}
+
/// A single node in the query tree.
///
/// All query types — leaf and compound — are variants of this one enum.
@@ -343,6 +350,12 @@ pub enum QueryNode {
value: String,
#[serde(skip_serializing_if = "Option::is_none")]
boost: Option<f32>,
+ /// True when this node came from a *standalone* `{prefix:{…}}` query,
+ /// which ES rewrites to a `constant_score` (every match scores `boost`,
+ /// no BM25). False for internally-generated prefixes (query_string,
+ /// match_bool_prefix) that keep BM25 scoring.
+ #[serde(default, skip_serializing_if = "is_false")]
+ constant_score: bool,
},
/// Wildcard pattern match (`?` = any char, `*` = zero-or-more chars).
@@ -351,6 +364,11 @@ pub enum QueryNode {
value: String,
#[serde(skip_serializing_if = "Option::is_none")]
boost: Option<f32>,
+ /// True when this node came from a *standalone* `{wildcard:{…}}` query,
+ /// which ES rewrites to a `constant_score`. False for the shared
+ /// `term{case_insensitive}` / query_string lowerings that keep BM25.
+ #[serde(default, skip_serializing_if = "is_false")]
+ constant_score: bool,
},
/// Matches documents where the field has any non-null value.
diff --git a/engine/crates/xerj-query/src/parser.rs b/engine/crates/xerj-query/src/parser.rs
index 174969479..40e8aa3db 100644
--- a/engine/crates/xerj-query/src/parser.rs
+++ b/engine/crates/xerj-query/src/parser.rs
@@ -590,6 +590,7 @@ fn parse_multi_match(params: &Value) -> Result<QueryNode> {
field: field.to_string(),
value: tokens[0].clone(),
boost: fb,
+ constant_score: false,
};
should.push(prefix);
continue;
@@ -619,6 +620,7 @@ fn parse_multi_match(params: &Value) -> Result<QueryNode> {
field: field.to_string(),
value: tokens[last].clone(),
boost: None,
+ constant_score: false,
});
let (im, is, imm) = if operator_and {
(inner_clauses, vec![], None)
@@ -724,6 +726,7 @@ fn parse_term(params: &Value) -> Result<QueryNode> {
field,
value: val_str,
boost,
+ constant_score: false,
};
return Ok(maybe_named(node, name));
}
@@ -937,6 +940,7 @@ fn parse_prefix(params: &Value) -> Result<QueryNode> {
field,
value: value.to_string(),
boost: None,
+ constant_score: true,
});
}
@@ -954,6 +958,7 @@ fn parse_prefix(params: &Value) -> Result<QueryNode> {
field,
value,
boost,
+ constant_score: true,
})
}
@@ -973,6 +978,7 @@ fn parse_wildcard(params: &Value) -> Result<QueryNode> {
field,
value: value.to_string(),
boost: None,
+ constant_score: true,
});
}
@@ -990,6 +996,7 @@ fn parse_wildcard(params: &Value) -> Result<QueryNode> {
field,
value,
boost,
+ constant_score: true,
})
}
@@ -1596,6 +1603,7 @@ fn parse_qs_unary(
field: f,
value: value.to_lowercase(),
boost: None,
+ constant_score: false,
});
}
Some(QueryNode::Match {
@@ -3533,6 +3541,7 @@ fn parse_match_bool_prefix(params: &Value) -> Result<QueryNode> {
field,
value: fold(&raw_tokens[0]),
boost: None,
+ constant_score: false,
});
}
@@ -3564,6 +3573,7 @@ fn parse_match_bool_prefix(params: &Value) -> Result<QueryNode> {
field: field.clone(),
value: fold(&raw_tokens[last_idx]),
boost: None,
+ constant_score: false,
});
// operator:and → every clause must match; otherwise should + mm.