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.

perf(query): constant-score prefix/wildcard/mbp → columnar ord-range plan (wildcard 4.4→0.7ms)

xerj-org82d3126c4b70
diff --git a/engine/crates/xerj-engine/src/index.rs b/engine/crates/xerj-engine/src/index.rs
index 1ae9728ca..865cee183 100644
--- a/engine/crates/xerj-engine/src/index.rs
+++ b/engine/crates/xerj-engine/src/index.rs
@@ -10282,7 +10282,9 @@ impl Index {
for fl in &filters {
match fl {
ScoredFilterLeaf::KeywordTerm { field, .. }
- | ScoredFilterLeaf::KeywordTerms { field, .. } => {
+ | ScoredFilterLeaf::KeywordTerms { field, .. }
+ | ScoredFilterLeaf::KeywordPrefix { field, .. }
+ | ScoredFilterLeaf::KeywordWildcard { field, .. } => {
let Some(Column::Keyword(k)) = cols.get(field.as_str()) else {
return None;
};
@@ -10387,6 +10389,45 @@ impl Index {
ords,
});
}
+ ScoredFilterLeaf::KeywordPrefix { field, prefix } => {
+ let Some(Column::Keyword(k)) = cols.get(field.as_str()) else {
+ return None;
+ };
+ // `terms` is lexicographically sorted: the prefix's
+ // matches form the contiguous ordinal range
+ // [lo, hi) bounded by two partition_points.
+ let lo = k.terms.partition_point(|t| t.as_str() < prefix.as_str());
+ let hi = lo + k.terms[lo..].partition_point(|t| t.starts_with(prefix.as_str()));
+ fev.push(FilterEval::Kw {
+ col: k,
+ no_nulls: k.null_bitmap.is_empty(),
+ ords: (lo as u32..hi as u32).collect(),
+ });
+ }
+ ScoredFilterLeaf::KeywordWildcard { field, pattern } => {
+ let Some(Column::Keyword(k)) = cols.get(field.as_str()) else {
+ return None;
+ };
+ // Narrow to the leading literal's prefix range, then
+ // run the SAME matcher as the brute path over the
+ // (distinct) dictionary terms so match semantics
+ // cannot drift.
+ let lit: &str = pattern
+ .split(['*', '?'])
+ .next()
+ .unwrap_or("");
+ let lo = k.terms.partition_point(|t| t.as_str() < lit);
+ let hi = lo + k.terms[lo..].partition_point(|t| t.starts_with(lit));
+ let ords: Vec<u32> = (lo..hi)
+ .filter(|&o| wildcard_match(&k.terms[o], pattern))
+ .map(|o| o as u32)
+ .collect();
+ fev.push(FilterEval::Kw {
+ col: k,
+ no_nulls: k.null_bitmap.is_empty(),
+ ords,
+ });
+ }
ScoredFilterLeaf::NumericWindow {
field,
min,
@@ -20118,6 +20159,16 @@ enum ScoredFilterLeaf {
KeywordTerm { field: String, term: String },
/// `terms` on a keyword field (OR over whole-value terms).
KeywordTerms { field: String, terms: Vec<String> },
+ /// Standalone constant-score `prefix` on a keyword field — resolved
+ /// per segment to the contiguous ordinal range of matching dictionary
+ /// terms (`terms` is lexicographically sorted, so two partition_points
+ /// bound the prefix). LOSS_BATTLE_PLAN B3.
+ KeywordPrefix { field: String, prefix: String },
+ /// Standalone constant-score `wildcard` on a keyword field — resolved
+ /// per segment by matching the (distinct) dictionary terms with the
+ /// SAME `wildcard_match` the brute path uses, narrowed to the leading
+ /// literal's prefix range first.
+ KeywordWildcard { field: String, pattern: String },
/// `term` on a numeric/boolean field, or `range` on a numeric field —
/// both evaluate as an inclusive/exclusive f64 window over the numeric
/// doc-values column (a term is the degenerate `[v, v]` window;
@@ -20621,6 +20672,41 @@ fn scored_fast_plan(
organic: scoring_leaf(organic, 1.0, &fs)?,
})
}
+ // Standalone constant-score keyword prefix / wildcard
+ // (LOSS_BATTLE_PLAN B3): ES rewrites these MultiTermQueries to
+ // ConstantScore — every match scores f32(boost), no BM25 — so they
+ // are exactly a Filtered plan whose filter is the dictionary
+ // ordinal range/set of matching terms. The `constant_score: true`
+ // flag (4c69c05) is the discriminator that keeps every
+ // internally-lowered BM25 prefix/wildcard (query_string,
+ // term{case_insensitive}, multi-token match_bool_prefix trailing
+ // prefix) on the brute path, and non-keyword fields bail.
+ QueryNode::Prefix {
+ field,
+ value,
+ boost,
+ constant_score: true,
+ } if fs.kw.contains(field) => Some(ScoredPlan::Filtered {
+ filter: vec![ScoredFilterLeaf::KeywordPrefix {
+ field: field.clone(),
+ prefix: value.clone(),
+ }],
+ must_not: Vec::new(),
+ score: scoring_boost(boost)?,
+ }),
+ QueryNode::Wildcard {
+ field,
+ value,
+ boost,
+ constant_score: true,
+ } if fs.kw.contains(field) => Some(ScoredPlan::Filtered {
+ filter: vec![ScoredFilterLeaf::KeywordWildcard {
+ field: field.clone(),
+ pattern: value.clone(),
+ }],
+ must_not: Vec::new(),
+ score: scoring_boost(boost)?,
+ }),
// Filter-only bool: ES scores filter context 0.0 (the brute path's
// 1.0 floor diverged on every hit). minimum_should_match without
// should clauses is ignored by ES, but bail conservatively.
diff --git a/engine/crates/xerj-query/src/parser.rs b/engine/crates/xerj-query/src/parser.rs
index 6f4f83466..e3c11239d 100644
--- a/engine/crates/xerj-query/src/parser.rs
+++ b/engine/crates/xerj-query/src/parser.rs
@@ -3538,11 +3538,18 @@ fn parse_match_bool_prefix(params: &Value) -> Result<QueryNode> {
}
if raw_tokens.len() == 1 {
+ // Single token: the whole match_bool_prefix collapses to one
+ // standalone prefix clause, which ES rewrites to ConstantScore
+ // (flat 1.0, no BM25) exactly like a top-level `prefix` query —
+ // the same MultiTermQuery rewrite 4c69c05 threaded through
+ // parse_prefix/parse_wildcard; this site was missed (live: xerj
+ // max_score 2.5210621 vs ES 1.0). Multi-token keeps BM25 (the
+ // trailing prefix is one clause among scored Match clauses).
return Ok(QueryNode::Prefix {
field,
value: fold(&raw_tokens[0]),
boost: None,
- constant_score: false,
+ constant_score: true,
});
}