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
fix(es-compat): default track_total_hits=10000 + treat -1 as exact (ES 8.x semantics)
xerj-org2957e43366f3
diff --git a/engine/crates/xerj-api/src/es_compat.rs b/engine/crates/xerj-api/src/es_compat.rsindex 6dea20aa4..05e579b4d 100644--- a/engine/crates/xerj-api/src/es_compat.rs+++ b/engine/crates/xerj-api/src/es_compat.rs@@ -4172,6 +4172,14 @@ pub async fn search(});}}+ // ES 8.x default: when `track_total_hits` is absent, totals are only+ // tracked accurately up to 10,000 — beyond that the response reports+ // {"value": 10000, "relation": "gte"}. Scroll requests are exempt:+ // ES keeps scroll totals exact (scroll/10_basic.yml asserts relation+ // "eq" without setting track_total_hits).+ if body.track_total_hits.is_none() && !is_scroll_request {+ body.track_total_hits = Some(json!(10_000));+ }// `?_source_includes=f1,f2` and `?_source_excludes=f3` — query-param// source filter. ES resolves these to take precedence over any body-// level `_source` value (see test search/10_source_filtering.yml@@ -11990,6 +11998,15 @@ pub async fn msearch(State(state): State<AppState>, body: bytes::Bytes) -> impl}}+ // ES 8.x default: when a sub-search omits `track_total_hits`, the+ // total is only tracked up to 10,000 ({"value":10000,"relation":"gte"}+ // beyond that) — same default the top-level `_search` handler applies.+ if effective_body.get("track_total_hits").is_none() {+ if let Some(obj) = effective_body.as_object_mut() {+ obj.insert("track_total_hits".to_string(), json!(10_000));+ }+ }+// Parse search request.let search_req = match xerj_query::parse_request(&effective_body).map_err(|e| xerj_common::XerjError::invalid_query(e.to_string()))@@ -12021,6 +12038,7 @@ pub async fn msearch(State(state): State<AppState>, body: bytes::Bytes) -> impllet mut merged_hits: Vec<(String, xerj_query::executor::Hit)> = Vec::new();let mut total_count: u64 = 0;+ let mut total_relation = "eq";let mut merged_aggs: Option<Value> = None;let mut search_error: Option<String> = None;@@ -12035,6 +12053,9 @@ pub async fn msearch(State(state): State<AppState>, body: bytes::Bytes) -> implmatch idx.search(&search_req).await {Ok(result) => {total_count += result.total.value;+ if result.total.relation == xerj_query::executor::TotalHitsRelation::Gte {+ total_relation = "gte";+ }if merged_aggs.is_none() {merged_aggs = result.aggs;}@@ -12060,6 +12081,19 @@ pub async fn msearch(State(state): State<AppState>, body: bytes::Bytes) -> impllet took_ms = started.elapsed().as_millis() as u64;let max_score = merged_hits.first().map(|(_, h)| h.score as f64);+ // `track_total_hits: <N>` (including the injected 10k default) caps+ // the merged total across indices; each per-index search already+ // applied its own cap but the sum can exceed N. Mirrors the+ // cross-index re-cap in the main `_search` handler.+ if let Some(Value::Number(n)) = effective_body.get("track_total_hits") {+ if let Some(limit) = n.as_u64() {+ if limit > 0 && total_count > limit {+ total_count = limit;+ total_relation = "gte";+ }+ }+ }+let hits: Vec<Value> = merged_hits.into_iter().map(|(idx_name, h)| {@@ -12078,7 +12112,7 @@ pub async fn msearch(State(state): State<AppState>, body: bytes::Bytes) -> impl"timed_out": false,"_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 },"hits": {- "total": { "value": total_count, "relation": "eq" },+ "total": { "value": total_count, "relation": total_relation },"max_score": max_score,"hits": hits,},@@ -19890,7 +19924,12 @@ pub async fn async_search_submit(Path(index): Path<String>,body: OptionalJson<EsSearchBody>,) -> impl IntoResponse {- let body = body.into_or_default();+ let mut body = body.into_or_default();+ // ES 8.x default: absent track_total_hits → totals tracked up to 10,000+ // only ({"value":10000,"relation":"gte"} beyond), same as `_search`.+ if body.track_total_hits.is_none() {+ body.track_total_hits = Some(json!(10_000));+ }let idx = match state.engine.get_or_create_index(&index) {Ok(i) => i,@@ -19926,7 +19965,10 @@ pub async fn async_search_submit("timed_out": result.timed_out,"_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 },"hits": {- "total": { "value": result.total.value, "relation": "eq" },+ "total": {+ "value": result.total.value,+ "relation": if result.total.relation == xerj_query::executor::TotalHitsRelation::Gte { "gte" } else { "eq" },+ },"max_score": max_score,"hits": hits,}diff --git a/engine/crates/xerj-query/src/parser.rs b/engine/crates/xerj-query/src/parser.rsindex 72e5cc229..f4ef5149c 100644--- a/engine/crates/xerj-query/src/parser.rs+++ b/engine/crates/xerj-query/src/parser.rs@@ -188,8 +188,14 @@ pub fn parse_request(body: &Value) -> Result<SearchRequest> {Some(Value::Bool(true)) | None => TrackTotalHits::True,Some(Value::Bool(false)) => TrackTotalHits::False,Some(Value::Number(n)) => {- let limit = n.as_u64().unwrap_or(10_000);- TrackTotalHits::Limit(limit)+ // ES accepts `track_total_hits: -1` as an alias for `true`+ // (track the exact total).+ if n.as_i64().map(|v| v < 0).unwrap_or(false) {+ TrackTotalHits::True+ } else {+ let limit = n.as_u64().unwrap_or(10_000);+ TrackTotalHits::Limit(limit)+ }}_ => TrackTotalHits::True,};@@ -3114,6 +3120,33 @@ mod tests {}}+ // ── track_total_hits ──────────────────────────────────────────────────────++ #[test]+ fn test_track_total_hits_parsing() {+ // Absent → exact tracking at the parser level (the ES 10k default is+ // injected by the HTTP layer, NOT here — `_count` and internal+ // sub-searches rely on the parser staying exact when unset).+ let req = parse_request(&json!({"query": {"match_all": {}}})).unwrap();+ assert_eq!(req.track_total_hits, TrackTotalHits::True);++ // true → exact.+ let req = parse_request(&json!({"track_total_hits": true})).unwrap();+ assert_eq!(req.track_total_hits, TrackTotalHits::True);++ // false → don't track.+ let req = parse_request(&json!({"track_total_hits": false})).unwrap();+ assert_eq!(req.track_total_hits, TrackTotalHits::False);++ // Integer N → cap at N.+ let req = parse_request(&json!({"track_total_hits": 10_000})).unwrap();+ assert_eq!(req.track_total_hits, TrackTotalHits::Limit(10_000));++ // -1 is the ES alias for `true` (exact), NOT a 10k cap.+ let req = parse_request(&json!({"track_total_hits": -1})).unwrap();+ assert_eq!(req.track_total_hits, TrackTotalHits::True);+ }+// ── match_phrase ──────────────────────────────────────────────────────────#[test]