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.
Rust agent-memoryai-agentsbm25claude-codeclicode-searchcursordatabaseelasticsearchfull-text-searchhybrid-searchjsonllocal-firstmcpmcp-servermodel-context-protocolragrustsearch-enginevector-search
perf(engine): residual gate does not materialise hits under count_only (part 1 of #577)
xerj-org7214942787bd
diff --git a/engine/crates/xerj-engine/src/index.rs b/engine/crates/xerj-engine/src/index.rsindex ff8d486dc..f7c7fb97b 100644--- a/engine/crates/xerj-engine/src/index.rs+++ b/engine/crates/xerj-engine/src/index.rs@@ -16569,23 +16569,34 @@ impl Index {}total_count += 1;seen_ids.insert(id.clone());- let hit = Hit {- id,- score: sh.score,- source,- seq_no: hit_seq_no,- version: hit_version,- sort: Vec::new(),- explain: None,- highlight: None,- matched_queries: Vec::new(),- passage: None,- };- if let Some(topk) = sort_topk.as_mut() {- let seq = hit_seq_no.unwrap_or(u64::MAX);- topk.offer(hit, seq, self);- } else {- all_hits.push(hit);+ // #577: under `count_only` (size:0) the page is+ // empty, so — exactly like the normal walk, which+ // does NOT materialise a hit at all under+ // count_only (the `if !count_only` guards near+ // this pass at ~14939/14975) — skip the `Hit`+ // build and `_source` retention; only the count+ // matters. Without this a size:0 bool with a+ // non-projectable filter held O(matches) hydrated+ // `Hit`s before the (empty) slice discarded them.+ if !count_only {+ let hit = Hit {+ id,+ score: sh.score,+ source,+ seq_no: hit_seq_no,+ version: hit_version,+ sort: Vec::new(),+ explain: None,+ highlight: None,+ matched_queries: Vec::new(),+ passage: None,+ };+ if let Some(topk) = sort_topk.as_mut() {+ let seq = hit_seq_no.unwrap_or(u64::MAX);+ topk.offer(hit, seq, self);+ } else {+ all_hits.push(hit);+ }}}// Preserve the segment-tail deadline propagationdiff --git a/engine/crates/xerj-engine/tests/bool_filter_is_non_scoring.rs b/engine/crates/xerj-engine/tests/bool_filter_is_non_scoring.rsindex 3e88fa130..934922413 100644--- a/engine/crates/xerj-engine/tests/bool_filter_is_non_scoring.rs+++ b/engine/crates/xerj-engine/tests/bool_filter_is_non_scoring.rs@@ -355,6 +355,60 @@ async fn residual_filter_still_excludes_nonmatching_docs() {}}+/// #577: a `count_only` (size:0) residual query must return the exact total via+/// the membership gate with ZERO materialised hits. The residual pass now skips+/// the `Hit` build under count_only (mirroring the normal walk), rather than+/// hydrating O(matches) `Hit`s for a page that is empty. Result-invariant guard:+/// the count stays exact and the page stays empty.+#[tokio::test]+async fn residual_count_only_returns_exact_total_with_no_hits() {+ let dir = TempDir::new().unwrap();+ let engine = make_engine(&dir);+ let mut schema = Schema::empty();+ schema+ .fields+ .push(FieldConfig::new("body", FieldType::Text));+ schema+ .fields+ .push(FieldConfig::new("tag", FieldType::Keyword));+ engine.create_index("residual-count-only", schema).unwrap();+ let idx = engine.get_index("residual-count-only").unwrap();+ for i in 0..120usize {+ let alphas = "alpha ".repeat(1 + i % 5);+ let mut doc = json!({ "body": alphas });+ if i % 2 == 0 {+ doc.as_object_mut()+ .unwrap()+ .insert("tag".into(), json!("kept"));+ }+ idx.index_document(Some(format!("d{i:03}")), doc)+ .await+ .unwrap();+ }+ idx.flush().await.unwrap();++ let counted = idx+ .search(&req(+ json!({"bool": {+ "must": [{"match": {"body": "alpha"}}],+ "filter": [{"exists": {"field": "tag"}}]+ }}),+ 0,+ ))+ .await+ .unwrap();+ assert_eq!(+ counted.total.value, 60,+ "count_only must still apply the residual membership gate — only the 60 \+ even docs carry `tag`"+ );+ assert_eq!(+ counted.hits.len(),+ 0,+ "size:0 returns no hits — the residual pass must not materialise any (#577)"+ );+}+/// The residual gate must be page-size-invariant: forcing full materialisation/// and re-counting survivors must give the same top hit, the same score, and/// the same total at size:1 and size:200 (a gate that miscounts or mis-ranks