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): size:0/_count count-only mode in the scored columnar executor (127-167ms -> sub-ms)

xerj-org55ba20e23bd4
diff --git a/engine/crates/xerj-api/src/es_compat.rs b/engine/crates/xerj-api/src/es_compat.rs
index aaca3d319..03cfa2813 100644
--- a/engine/crates/xerj-api/src/es_compat.rs
+++ b/engine/crates/xerj-api/src/es_compat.rs
@@ -9220,6 +9220,30 @@ pub async fn search(
})
};
+ // ES 8.x emits `"terminated_early": false` (immediately after
+ // `timed_out`) on the hits-skipping exact-count path: `size: 0` +
+ // explicit `track_total_hits: true`, without aggregations. Live-probed
+ // against ES 8.13.4: `sort`/`min_score`/`terminate_after` do NOT
+ // suppress it, any `aggs` does, and tth default/`false`/`N` never emit
+ // it. serde_json runs with `preserve_order`, so rebuild the map to
+ // splice the key at ES's exact position (byte-parity).
+ if body.size == 0
+ && matches!(body.track_total_hits, Some(Value::Bool(true)))
+ && body.aggs.is_none()
+ && body.aggregations.is_none()
+ {
+ if let Some(obj) = response_body.as_object_mut() {
+ let mut rebuilt = serde_json::Map::with_capacity(obj.len() + 1);
+ for (k, v) in obj.iter() {
+ rebuilt.insert(k.clone(), v.clone());
+ if k == "timed_out" {
+ rebuilt.insert("terminated_early".to_string(), Value::Bool(false));
+ }
+ }
+ *obj = rebuilt;
+ }
+ }
+
// Surface the synthesized `_shards.failures` array for an HDR-percentiles
// negative-value shard failure (the `failed`/`successful` counts were
// already adjusted via `shards_failed_count` above).
diff --git a/engine/crates/xerj-api/src/responses.rs b/engine/crates/xerj-api/src/responses.rs
index 3352ea5b5..0eef97d01 100644
--- a/engine/crates/xerj-api/src/responses.rs
+++ b/engine/crates/xerj-api/src/responses.rs
@@ -28,10 +28,14 @@ fn matched_queries_is_empty(v: &Value) -> bool {
pub struct EsShards {
pub total: u32,
pub successful: u32,
- pub failed: u32,
- /// Skipped shards (search responses only).
+ /// Skipped shards (search responses only). Declared BEFORE `failed`
+ /// because serde emits fields in declaration order and ES 8.x renders
+ /// search `_shards` as {total, successful, skipped, failed} — write
+ /// responses omit `skipped` (None) and keep ES's {total, successful,
+ /// failed}. Live-verified against ES 8.13.4 (byte-parity sweep).
#[serde(skip_serializing_if = "Option::is_none")]
pub skipped: Option<u32>,
+ pub failed: u32,
}
impl EsShards {
diff --git a/engine/crates/xerj-engine/src/index.rs b/engine/crates/xerj-engine/src/index.rs
index 9427023c9..19eed7ce5 100644
--- a/engine/crates/xerj-engine/src/index.rs
+++ b/engine/crates/xerj-engine/src/index.rs
@@ -10914,6 +10914,16 @@ impl Index {
}
}
total += 1;
+ // Count-only (size:0 → keep_target 0): the
+ // score-class check exists only to fill the
+ // two-valued page — skip it; under a tth cap
+ // stop once (limit, gte) is guaranteed.
+ if keep_target == 0 {
+ if total > limit_val {
+ break 'brows;
+ }
+ continue 'brows;
+ }
let (score, kept) = if sev[*sh].matches(row) {
(*hi, &mut kept_hi)
} else {
@@ -11024,6 +11034,14 @@ impl Index {
}
if let Some(score) = root.eval(row, &sev, &fev, &clause_scores) {
total += 1;
+ // Count-only (size:0): nothing to keep — under a
+ // tth cap stop once (limit, gte) is guaranteed.
+ if keep_target == 0 {
+ if total > limit_val {
+ break;
+ }
+ continue;
+ }
push_topk(
&mut heap,
keep_target,
@@ -11066,6 +11084,14 @@ impl Index {
continue;
}
total += 1;
+ // Count-only (size:0): membership is positive-only —
+ // the negative rescale can't change the count.
+ if keep_target == 0 {
+ if total > limit_val {
+ break;
+ }
+ continue;
+ }
let score = if neg.matches(row) {
ps * *negative_boost
} else {
@@ -11159,6 +11185,14 @@ impl Index {
}
}
total += 1;
+ // Count-only (size:0): nothing to keep — under a
+ // tth cap stop once (limit, gte) is guaranteed.
+ if keep_target == 0 {
+ if total > limit_val {
+ break 'frows;
+ }
+ continue 'frows;
+ }
push_topk(
&mut heap,
keep_target,
@@ -11232,6 +11266,14 @@ impl Index {
continue;
}
total += 1;
+ // Count-only (size:0): nothing to keep — under a
+ // tth cap stop once (limit, gte) is guaranteed.
+ if keep_target == 0 {
+ if total > limit_val {
+ break;
+ }
+ continue;
+ }
push_topk(
&mut heap,
keep_target,
@@ -21189,7 +21231,7 @@ fn push_topk(heap: &mut std::collections::BinaryHeap<TopKCand>, cap: usize, c: T
fn scored_fast_plan(
query: &QueryNode,
request: &SearchRequest,
- size: usize,
+ _size: usize,
keyword_fields: &HashSet<String>,
numeric_fields: &HashSet<String>,
boolean_fields: &HashSet<String>,
@@ -21202,8 +21244,17 @@ fn scored_fast_plan(
// dropped e.g. `match(status)+highlight` to the brute FTS walk over
// every match (~2.5× ES); the columnar page is byte-identical, so the
// shared highlighter emits byte-identical fragments.
- if size == 0
- || request.aggs.is_some()
+ //
+ // `size == 0` is ADMITTED (count-only mode): the executor's Phase-2
+ // walk tallies the exact ghost-aware total with `keep_target == 0`
+ // (`push_topk` is a no-op at cap 0), returning `(vec![], total)` —
+ // the renderer emits `hits: []`, `max_score: null` exactly like the
+ // brute path. Pre-fix a scored `{size:0}` body (and `_count` with any
+ // scored query, which synthesizes one) fell to the brute FTS
+ // enumeration of EVERY match: live-measured 127 ms for the benchmark
+ // bool body and 167 ms for dis_max vs ES 0.2-0.3 ms — the largest
+ // ratio on the board (LOSS_BATTLE_PLAN "size:0 / _count brute cliff").
+ if request.aggs.is_some()
|| request.min_score.is_some()
|| !request.sort.is_empty()
|| !request.rescore.is_empty()