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(aggs): top_hits seq_no_primary_term emits the doc's real _seq_no

xerj-org9120a60c074c
diff --git a/engine/crates/xerj-engine/src/aggs.rs b/engine/crates/xerj-engine/src/aggs.rs
index b8acfb396..fea594ef0 100644
--- a/engine/crates/xerj-engine/src/aggs.rs
+++ b/engine/crates/xerj-engine/src/aggs.rs
@@ -7502,11 +7502,15 @@ pub(crate) fn run_top_hits_with_total(
hit_obj.insert("_version".to_string(), version);
}
}
- // Honor top_hits `seq_no_primary_term: true` — emit _seq_no and
- // _primary_term (placeholder values; real values would come from
- // the doc's seq_no metadata).
+ // Honor top_hits `seq_no_primary_term: true` — emit the doc's
+ // real `_seq_no` (already enriched onto each corpus doc; the
+ // top_hits sort path reads the same field) and `_primary_term`.
+ // `_primary_term` is fixed at 1 (single-shard convention, matching
+ // the main search-hit path). Fall back to 0 only when the doc
+ // carries no `_seq_no`.
if params.get("seq_no_primary_term").and_then(Value::as_bool).unwrap_or(false) {
- hit_obj.insert("_seq_no".to_string(), json!(0));
+ let sn = doc.get("_seq_no").and_then(Value::as_i64).unwrap_or(0);
+ hit_obj.insert("_seq_no".to_string(), json!(sn));
hit_obj.insert("_primary_term".to_string(), json!(1));
}
diff --git a/engine/tests/es-compat-yaml/yaml/aggregations/top_hits.yml b/engine/tests/es-compat-yaml/yaml/aggregations/top_hits.yml
index 48fe6a5f4..c4c7b3227 100644
--- a/engine/tests/es-compat-yaml/yaml/aggregations/top_hits.yml
+++ b/engine/tests/es-compat-yaml/yaml/aggregations/top_hits.yml
@@ -338,13 +338,16 @@ sequence number and primary term:
- match: { hits.total.value: 3 }
- length: { aggregations.page.buckets: 2 }
- match: { aggregations.page.buckets.0.key: 1 }
- - gte: { aggregations.page.buckets.0.top_hits.hits.hits.0._seq_no: 0 }
- - gte: { aggregations.page.buckets.0.top_hits.hits.hits.0._primary_term: 0 }
- - gte: { aggregations.page.buckets.0.top_hits.hits.hits.1._seq_no: 0 }
- - gte: { aggregations.page.buckets.0.top_hits.hits.hits.1._primary_term: 0 }
+ # Exact _seq_no: the three docs were bulk-indexed in order on a single
+ # shard, so they carry seq_no 0, 1, 2. page-1 holds docs 0 and 1;
+ # page-2 holds doc 2. _primary_term is the single-shard convention (1).
+ - match: { aggregations.page.buckets.0.top_hits.hits.hits.0._seq_no: 0 }
+ - match: { aggregations.page.buckets.0.top_hits.hits.hits.0._primary_term: 1 }
+ - match: { aggregations.page.buckets.0.top_hits.hits.hits.1._seq_no: 1 }
+ - match: { aggregations.page.buckets.0.top_hits.hits.hits.1._primary_term: 1 }
- match: { aggregations.page.buckets.1.key: 2 }
- - gte: { aggregations.page.buckets.1.top_hits.hits.hits.0._seq_no: 0 }
- - gte: { aggregations.page.buckets.1.top_hits.hits.hits.0._primary_term: 0 }
+ - match: { aggregations.page.buckets.1.top_hits.hits.hits.0._seq_no: 2 }
+ - match: { aggregations.page.buckets.1.top_hits.hits.hits.0._primary_term: 1 }
---
synthetic _source: