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(api): scope scroll continuation mapping suppression to the opening route (#637)

xerj-orgf50e93911203
diff --git a/engine/crates/xerj-api/src/es_compat.rs b/engine/crates/xerj-api/src/es_compat.rs
index 822f856aa..1a0f40baf 100644
--- a/engine/crates/xerj-api/src/es_compat.rs
+++ b/engine/crates/xerj-api/src/es_compat.rs
@@ -8729,10 +8729,11 @@ fn render_collapse_inner_hits(
/// Whether the index mapping sets `_source.enabled: false`. ES suppresses
/// `_source` in the response for such an index; xerj keeps the underlying
/// source available internally (fields/highlight/_ignored extraction) and makes
-/// the suppression a response-time decision. Mirrors the per-hit check in
-/// `search_impl`'s render, extracted so scroll-open can capture the SAME
-/// decision into `ScrollContext::source_disabled` and continuation pages stay in
-/// parity with the first page (#637).
+/// the suppression a response-time decision. Extracted from `search_impl`'s
+/// per-hit render so the scroll continuation (`scroll_page_response`) can apply
+/// the SAME per-hit decision from each hit's own index — mapping suppression is
+/// NOT a snapshot constant, because a scroll can span indices with divergent
+/// `_source` settings (#637).
fn mapping_source_disabled(state: &AppState, index: &str) -> bool {
state
.engine
@@ -14648,9 +14649,12 @@ async fn search_impl(
// re-derives the mapping check per hit from `h.index` instead.
//
// The per-hit null-source case is likewise handled in
- // `scroll_page_response`. (The `/:index/_search_scroll` route's own first
- // page does not suppress any of these, so its capture site stays
- // request-level only — matching ITS render; see #659.)
+ // `scroll_page_response`. `scroll_page_response` is shared with the
+ // `/:index/_search_scroll` route, whose first page does NOT apply the
+ // mapping check — so the per-hit mapping gate there is guarded by the
+ // `mapping_source_check` flag set below (true here, false for that
+ // route), keeping each route's continuation identical to its own first
+ // page. That route's broader `_source` gap is tracked in #659.
let scroll_source_disabled = matches!(body.source, Some(Value::Bool(false)))
|| (suppress_source_for_stored && body.source.is_none());
let ctx = xerj_engine::engine::ScrollContext {
@@ -14664,6 +14668,9 @@ async fn search_impl(
seq_no_primary_term: emit_seq_no,
version: emit_version,
source_disabled: scroll_source_disabled,
+ // `_search?scroll=` first page (this fn) suppresses mapping
+ // `_source.enabled:false` per hit, so the continuation must too.
+ mapping_source_check: true,
created: now,
keep_alive,
expires_at: now + keep_alive,
@@ -20385,6 +20392,12 @@ pub async fn search_with_scroll(
seq_no_primary_term: emit_seq_no,
version: emit_version,
source_disabled: matches!(body.source, Some(Value::Bool(false))),
+ // `_search_scroll` (this route) does NOT apply mapping
+ // `_source.enabled:false` suppression on its first page (it emits
+ // `_source` unconditionally), so its continuation must not either —
+ // otherwise it gains a new first-page/continuation split. That
+ // route's broader `_source` gap is tracked in #659.
+ mapping_source_check: false,
created: now,
keep_alive,
expires_at: now + keep_alive,
@@ -20670,12 +20683,18 @@ async fn scroll_page_response(
// borrows `ctx.hits`.
let emit_seq_no = ctx.seq_no_primary_term;
let emit_version = ctx.version;
- // #624: the opening request's `_source: false` governs the whole
- // scroll — omit `_source` on continuation pages too (ES fixes source
- // selection at open; the `_search?scroll=` first page already omits
- // it via search_impl). inner_hits still render from the collapse
- // group snapshot regardless.
+ // #624/#637: the opening request's REQUEST-level `_source` decision
+ // (`_source:false`, stored_fields-implied) governs the whole scroll —
+ // omit `_source` on continuation pages too (ES fixes source selection
+ // at open). inner_hits still render from the collapse group snapshot
+ // regardless.
let source_disabled = ctx.source_disabled;
+ // #637: whether to ALSO apply the mapping `_source.enabled:false`
+ // suppression per hit below. Only true for scrolls opened by
+ // `search_impl` (whose first page applies it); false for the
+ // `_search_scroll` route, whose first page does not — so its
+ // continuation stays byte-identical to that first page (#659).
+ let mapping_source_check = ctx.mapping_source_check;
// Detect whether the initial search sorted by a non-score key;
// in that case `max_score` must be null on scroll pages too.
@@ -20805,15 +20824,20 @@ async fn scroll_page_response(
});
// #624/#637: honor the first page's `_source` suppression
// (the sentinels have already been consumed into
- // `inner_hits` above), matching search_impl exactly.
- // `source_disabled` carries the request-level constant
- // (`_source:false`, stored_fields-implied); the mapping
- // `_source.enabled:false` case is re-derived PER HIT from
- // this hit's own index, because a scroll can span indices
- // with divergent `_source` settings and the first page
- // suppresses that case per-hit (#658 skeptic). inner_hits
- // are emitted regardless.
- if !source_disabled && !mapping_source_disabled(state, &h.index) {
+ // `inner_hits` above). `source_disabled` carries the
+ // request-level constant (`_source:false`, stored_fields-
+ // implied). The mapping `_source.enabled:false` case is
+ // re-derived PER HIT from this hit's own index (a scroll
+ // can span indices with divergent `_source` settings, and
+ // search_impl's first page suppresses it per-hit — #658
+ // skeptic), but ONLY when the opening route applies it on
+ // its first page (`mapping_source_check`); the
+ // `_search_scroll` route does not, so its continuation
+ // stays identical to its first page (#659). inner_hits are
+ // emitted regardless.
+ if !source_disabled
+ && !(mapping_source_check && mapping_source_disabled(state, &h.index))
+ {
o.insert("_source".to_string(), src);
}
if let Some(inner) = collapse_inner {
@@ -20917,6 +20941,7 @@ mod passage_scroll_tests {
seq_no_primary_term: false,
version: false,
source_disabled: false,
+ mapping_source_check: false,
created: now,
keep_alive: Duration::from_secs(60),
expires_at: now + Duration::from_secs(60),
@@ -20976,6 +21001,7 @@ mod passage_scroll_tests {
seq_no_primary_term: false,
version: false,
source_disabled: false,
+ mapping_source_check: false,
created: now,
keep_alive: Duration::from_secs(60),
expires_at: now + Duration::from_secs(60),
@@ -21031,6 +21057,7 @@ mod passage_scroll_tests {
seq_no_primary_term: false,
version: false,
source_disabled: false,
+ mapping_source_check: false,
created: now,
keep_alive: Duration::from_secs(60),
expires_at: now + Duration::from_secs(60),
diff --git a/engine/crates/xerj-api/tests/scroll_source_suppression_parity.rs b/engine/crates/xerj-api/tests/scroll_source_suppression_parity.rs
index e41fa4130..6716e410c 100644
--- a/engine/crates/xerj-api/tests/scroll_source_suppression_parity.rs
+++ b/engine/crates/xerj-api/tests/scroll_source_suppression_parity.rs
@@ -260,3 +260,56 @@ async fn scroll_multi_index_source_suppression_is_per_hit() {
);
assert_eq!(b_seen, 4, "expected all 4 b_on hits across pages: {hits:?}");
}
+
+/// Regression guard for the sibling `/:index/_search_scroll` route
+/// (`search_with_scroll`), which shares `scroll_page_response` for continuation
+/// but emits `_source` UNCONDITIONALLY on its own first page. This #637 change
+/// must NOT make that route's continuation start suppressing `_source` for a
+/// mapping-disabled index while its first page still emits it — a new
+/// first-page/continuation split (caught by the #658 regression skeptic; guarded
+/// by the `ScrollContext::mapping_source_check` flag). The route stays
+/// self-consistent; fixing its first page to actually suppress is #659.
+#[tokio::test]
+async fn search_scroll_route_source_parity_first_page_and_continuation_agree() {
+ let (app, _dir) = app().await;
+ seed(
+ &app,
+ "docs",
+ json!({"mappings": {
+ "_source": {"enabled": false},
+ "properties": {"grp": {"type": "keyword"}, "v": {"type": "long"}}
+ }}),
+ )
+ .await;
+
+ // Open via the `_search_scroll` route (NOT `_search?scroll=`).
+ let (st, first) = call(
+ &app,
+ "POST",
+ "/docs/_search_scroll?scroll=5m",
+ json!({"query": {"match_all": {}}, "size": 1, "sort": [{"v": "asc"}]}),
+ )
+ .await;
+ assert_eq!(st, StatusCode::OK, "search_scroll open: {first}");
+ let first_has_source = first["hits"]["hits"][0].get("_source").is_some();
+ let sid = first["_scroll_id"].as_str().expect("scroll id").to_string();
+
+ let (st, page2) = call(
+ &app,
+ "POST",
+ "/_search/scroll",
+ json!({"scroll": "5m", "scroll_id": sid}),
+ )
+ .await;
+ assert_eq!(st, StatusCode::OK, "continuation: {page2}");
+ let cont_has_source = page2["hits"]["hits"][0].get("_source").is_some();
+
+ // The crux: whatever this route does on its first page, the continuation must
+ // AGREE — #637 must not introduce a page-to-page `_source` split here.
+ assert_eq!(
+ first_has_source, cont_has_source,
+ "#637/#659: the _search_scroll route's continuation must match its own first \
+ page on _source presence (first={first_has_source}, continuation={cont_has_source}); \
+ page1={first} page2={page2}"
+ );
+}
diff --git a/engine/crates/xerj-engine/src/engine.rs b/engine/crates/xerj-engine/src/engine.rs
index 0ca7224fd..8814408bc 100644
--- a/engine/crates/xerj-engine/src/engine.rs
+++ b/engine/crates/xerj-engine/src/engine.rs
@@ -170,12 +170,24 @@ pub struct ScrollContext {
/// Whether to emit `_version` on every page. Same once-at-open semantics
/// as `seq_no_primary_term`.
pub version: bool,
- /// Whether the opening request suppressed `_source` (`"_source": false`).
- /// Captured once at open — like `seq_no_primary_term`/`version`, ES fixes
- /// the `_source` selection at the scroll-opening request and applies it to
- /// every continuation page — so the continuation render omits `_source`
- /// when this is set, matching the `_search?scroll=` first page (#624).
+ /// Whether the opening request suppressed `_source` at the REQUEST level —
+ /// `"_source": false`, or `stored_fields` implying suppression when
+ /// `_source` was unspecified. These are constant across the whole snapshot
+ /// (index-independent), so a single captured boolean is correct. Captured
+ /// once at open — like `seq_no_primary_term`/`version`, ES fixes the
+ /// `_source` selection at the scroll-opening request — so the continuation
+ /// render omits `_source` when this is set, matching the first page
+ /// (#624/#637).
pub source_disabled: bool,
+ /// Whether the opening route's first page also applies mapping-level
+ /// `_source.enabled: false` suppression (which is PER-HIT: a scroll can span
+ /// indices with divergent `_source` settings). `search_impl` (`_search?scroll=`)
+ /// does, so the continuation re-derives it per hit from each hit's own index;
+ /// `search_with_scroll` (`_search_scroll`) does NOT suppress it on its first
+ /// page, so its continuation must not either, or it would introduce a new
+ /// first-page/continuation split on that route (#637; that route's own gap is
+ /// tracked in #659).
+ pub mapping_source_check: bool,
pub created: Instant,
/// The keep-alive window last requested for this context. A
/// continuation without an explicit `scroll` parameter re-arms the
diff --git a/engine/crates/xerj-engine/tests/search_context_ttl.rs b/engine/crates/xerj-engine/tests/search_context_ttl.rs
index c31553859..72dcbe086 100644
--- a/engine/crates/xerj-engine/tests/search_context_ttl.rs
+++ b/engine/crates/xerj-engine/tests/search_context_ttl.rs
@@ -39,6 +39,7 @@ fn scroll_ctx(expires_in: i64) -> ScrollContext {
seq_no_primary_term: false,
version: false,
source_disabled: false,
+ mapping_source_check: false,
created: now,
keep_alive,
expires_at,