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(query): answer simple_query_string fields ["*"] from the inverted index (#2714)

xerj-org72968c390b5c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a22c78229..ca7af8113 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -113,6 +113,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
200-580 MB of warmed stored slices between 40 breaker engages. Queries
still hydrate a segment on first read. (Refs #2427)
+- `simple_query_string` with `"fields": ["*"]` is answered from the inverted
+ index instead of a stored-document scan of every segment (refs #2427). The
+ bare `*` pattern lowered to a literal `"*"` field that has no index
+ projection, so each query parsed every flushed document; on the 1 GB
+ autoindex release gate a 5-hit query walked ~1.2M stored docs and ran into
+ the 30 s request deadline (raw-surface p95 ≈ 30 s). `["*"]` now lowers like
+ an absent `fields` list, which is what ES means by it: unquoted terms are
+ matched over every mapped searchable text field. A boost on the bare
+ pattern (`"*^2"`) is dropped, and quoted phrases are unchanged. On a
+ flushed segment this also applies the searchable-field rule, so `["*"]` no
+ longer matches through an `index: false` field. `query_string` with
+ `["*"]` is unchanged.
+
- `_bulk` requests of ordinary text and code are no longer refused with a
429 while the node has memory to spare. Each raw bulk reserved 28 bytes
per input byte for analysis, a rate sized for the densest text, so a bulk
diff --git a/engine/crates/xerj-api/tests/index_false_is_honoured.rs b/engine/crates/xerj-api/tests/index_false_is_honoured.rs
index 783aec107..97330e669 100644
--- a/engine/crates/xerj-api/tests/index_false_is_honoured.rs
+++ b/engine/crates/xerj-api/tests/index_false_is_honoured.rs
@@ -617,15 +617,31 @@ async fn documented_gaps_are_still_open_and_still_documented() {
// GAP 3 — GAP 2 reached through a WILDCARD `fields` spec. A pattern is
// never an error (that half is ES's rule and is deliberate), but in
- // `query_string` / `simple_query_string` a pattern lowers to the same
- // field-less `"*"` placeholder as GAP 2, so it is answered by the
- // schema-free scan and still matches through `note`. Identical on `main`:
- // this change neither introduced nor closed it, and the CHANGELOG says so
- // instead of claiming a pattern is "answered over the searchable fields".
- for query in [
+ // `query_string` a pattern lowers to the field-less `"*"` placeholder,
+ // so it is answered by the schema-free scan and still matches through
+ // `note`. Identical on `main`, and the CHANGELOG says so instead of
+ // claiming a pattern is "answered over the searchable fields".
+ //
+ // `simple_query_string` with `["*"]` no longer belongs here (#2427): it
+ // now lowers to the same node as an ABSENT `fields`, which the segment's
+ // FTS projection answers over the mapped searchable text fields — the
+ // fix for its 30 s stored-scan walk — so on a flushed segment it finds
+ // nothing in `note`, like `multi_match` below. (A document still in the
+ // memtable is answered by the field-less scan arm, exactly as the
+ // field-less spelling always was; that is GAP 2, not a new gap.)
+ let (status, body) = search(
+ &app,
json!({ "simple_query_string": { "query": "zzquagga", "fields": ["*"] } }),
- json!({ "query_string": { "query": "zzquagga", "fields": ["*"] } }),
- ] {
+ )
+ .await;
+ assert_eq!(status, StatusCode::OK, "{body}");
+ assert_eq!(
+ body.pointer("/hits/total/value").and_then(Value::as_u64),
+ Some(0),
+ "#2427: a flushed `simple_query_string` `[\"*\"]` is answered over the \
+ searchable fields and must not reach `note`: {body}"
+ );
+ for query in [json!({ "query_string": { "query": "zzquagga", "fields": ["*"] } })] {
let (status, body) = search(&app, query.clone()).await;
assert_eq!(status, StatusCode::OK, "GAP 3 changed for {query}: {body}");
assert_eq!(
diff --git a/engine/crates/xerj-engine/src/index.rs b/engine/crates/xerj-engine/src/index.rs
index 01f8a2360..26baa34a6 100644
--- a/engine/crates/xerj-engine/src/index.rs
+++ b/engine/crates/xerj-engine/src/index.rs
@@ -46833,9 +46833,11 @@ fn check_buildable_query(
/// only in an unsearchable field still matches when no field is named at all.
/// `query_string {"query": "…"}` and a `more_like_this` with no `fields` both
/// still return the document — and so does a *wildcard* spec in
-/// `query_string` / `simple_query_string`, because `fields: ["*"]` lowers to
-/// that same field-less `"*"` placeholder rather than to an expansion over the
-/// searchable fields. Measured, not assumed. That divergence from the FTS
+/// `query_string`, because `fields: ["*"]` lowers to that same field-less
+/// `"*"` placeholder rather than to an expansion over the searchable fields.
+/// (`simple_query_string` `["*"]` now lowers like an absent `fields` (#2427),
+/// so a flushed segment answers it from the FTS projection, which applies the
+/// searchable-field rule; only memtable documents still see the scan arm.) Measured, not assumed. That divergence from the FTS
/// projection predates this change and is called out in its own comment in
/// `doc_matches_query`; making it schema-aware means threading a schema
/// through ~79 call sites and is left as its own change.
@@ -66304,3 +66306,98 @@ mod log_match_operator_tests {
);
}
}
+
+#[cfg(test)]
+mod sqs_wildcard_fields_2427_tests {
+ use super::*;
+ use serde_json::json;
+
+ /// #2427 (raw-surface p95 ≈ 30 s on the 1 GB gate):
+ /// `simple_query_string {"fields": ["*"]}` must be answered from the
+ /// inverted index, exactly like the same query with no `fields` at all.
+ ///
+ /// FAIL-BEFORE (watched): on `main` the parser lowered `["*"]` to
+ /// `Match { field: "*" }`, which has no FTS projection, so every flushed
+ /// segment was answered by parsing its stored section — this test saw
+ /// `fts=0 stored_scans=8` (same 200 hits) — and on the gate corpus that walk is what ran
+ /// into the 30 s request deadline. The hit set is asserted too, so the
+ /// faster route cannot quietly change the answer.
+ #[tokio::test]
+ async fn wildcard_fields_simple_query_string_uses_the_inverted_index() {
+ let dir = tempfile::tempdir().unwrap();
+ let mut config = Config::default();
+ config.server.data_dir = dir.path().to_string_lossy().into_owned();
+ let engine = crate::Engine::new(config).unwrap();
+ let mut schema = Schema::empty();
+ schema
+ .add_field(FieldConfig::new("title", FieldType::Text))
+ .unwrap();
+ schema
+ .add_field(FieldConfig::new("body", FieldType::Text))
+ .unwrap();
+ engine.create_index("sqs2427", schema).unwrap();
+ let idx = engine.get_index("sqs2427").unwrap();
+
+ // Two segments: the term lives in `title` in one and in `body` in the
+ // other, so a projection onto a single field would under-match.
+ for i in 0..300 {
+ let (title, body) = match i % 3 {
+ 0 => ("raft leader", "nothing here"),
+ 1 => ("unrelated", "the raft election timed out"),
+ _ => ("unrelated", "nothing here"),
+ };
+ idx.index_document(
+ Some(format!("a{i}")),
+ json!({ "title": title, "body": body }),
+ )
+ .await
+ .unwrap();
+ if i == 149 {
+ idx.flush().await.unwrap();
+ }
+ }
+ idx.flush().await.unwrap();
+
+ let run = |q: serde_json::Value| {
+ let idx = idx.clone();
+ async move {
+ SEG_FTS_HANDLED.with(|c| c.set(0));
+ SEG_STORED_SCANS.with(|c| c.set(0));
+ let req = xerj_query::parse_request(&json!({
+ "size": 5, "track_total_hits": true, "query": q
+ }))
+ .unwrap();
+ let res = idx.search(&req).await.unwrap();
+ let fts = SEG_FTS_HANDLED.with(|c| c.get());
+ let scans = SEG_STORED_SCANS.with(|c| c.get());
+ (res.total.value, fts, scans)
+ }
+ };
+
+ let (total_star, fts, scans) = run(json!({"simple_query_string": {
+ "query": "raft", "fields": ["*"], "lenient": true
+ }}))
+ .await;
+ assert_eq!(
+ total_star, 200,
+ "every document with `raft` in title OR body must match"
+ );
+ assert!(
+ fts > 0 && scans == 0,
+ "#2427: `fields: [\"*\"]` must be served by the FTS path on every \
+ segment, not by the stored-section scan: fts={fts} stored_scans={scans}"
+ );
+
+ // Same answer as the field-less spelling ES treats it as.
+ let (total_none, _, _) = run(json!({"simple_query_string": {"query": "raft"}})).await;
+ assert_eq!(total_star, total_none);
+
+ // Multi-token, the gate's raw-surface shape.
+ let (total_multi, fts, scans) = run(json!({"simple_query_string": {
+ "query": "leader election", "fields": ["*"], "lenient": true
+ }}))
+ .await;
+ assert_eq!(total_multi, 200);
+ assert!(fts > 0 && scans == 0, "fts={fts} stored_scans={scans}");
+ }
+}
diff --git a/engine/crates/xerj-query/src/parser.rs b/engine/crates/xerj-query/src/parser.rs
index 393a12064..b39006d5b 100644
--- a/engine/crates/xerj-query/src/parser.rs
+++ b/engine/crates/xerj-query/src/parser.rs
@@ -2857,6 +2857,31 @@ fn make_simple_query_node(
lenient,
}
}
+ } else if targets_every_field(fields) {
+ // No fields, or the bare `"*"` pattern (#2427). ES reads `["*"]` as
+ // "every searchable field" — it is also what an absent `fields`
+ // falls back to (`index.query.default_field` defaults to `*`) — so
+ // both spellings lower to the same field-less placeholder, which the
+ // engine projects onto every mapped text field of each segment's
+ // inverted index.
+ //
+ // `["*"]` used to lower to `Match { field: "*" }` instead. No FTS
+ // projection exists for a literal `"*"` field, so that node forced
+ // the per-document stored-section scan on EVERY segment: on the
+ // 1 GB autoindex gate one 5-hit query walked ~1.2M stored docs in
+ // pg-docs and ran into the 30 s request deadline (raw-surface p95
+ // 30.1 s). A `"*^2"` boost has no field to attach to on this node
+ // and is dropped, as it is for the phrase placeholder below.
+ //
+ // Quoted phrases keep their own arm above: the field-less
+ // placeholder is bag-of-words on the FTS path, so routing a phrase
+ // through it would loosen phrase matching.
+ QueryNode::QueryString {
+ query: term.to_string(),
+ default_field: None,
+ default_operator: None,
+ boost: None,
+ }
} else if fields.len() == 1 {
QueryNode::Match {
field: fields[0].clone(),
@@ -2867,14 +2892,6 @@ fn make_simple_query_node(
minimum_should_match: None,
lenient,
}
- } else if fields.is_empty() {
- // No fields specified — use a match_all-like placeholder.
- QueryNode::QueryString {
- query: term.to_string(),
- default_field: None,
- default_operator: None,
- boost: None,
- }
} else {
QueryNode::MultiMatch {
fields: fields.to_vec(),