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(engine): term/terms on keyword ARRAYS matched only the first element

Xerj Squad Aa53ac8dd6902
diff --git a/docs/research/xerj-keyword-array-term-fix.md b/docs/research/xerj-keyword-array-term-fix.md
new file mode 100644
index 000000000..d2318b39c
--- /dev/null
+++ b/docs/research/xerj-keyword-array-term-fix.md
@@ -0,0 +1,101 @@
+# Fix: `term`/`terms` on keyword ARRAYS match only the first element
+
+## Why the WordPress reachability flow was not searchable in XERJ
+
+Auditing WordPress, the reverse call-graph query "who calls `wp_safe_remote_get`"
+returned **1** result from XERJ's structured index while `grep` found 14 files and
+XERJ full-text (`match`) found 9. A false "not reachable" is the worst possible
+error in a security audit, so we traced it to the engine.
+
+**Root cause (proven, not inferred).** A keyword field is stored one value per
+document. For an **array** value, XERJ keeps only element `[0]`:
+
+- Memtable — `memtable.rs::DocValues::push_field`, the `Value::Array` arm:
+ `let first_str = arr.iter().find_map(...); kcol.push(first_str);` with the
+ comment *"for term/range queries we store the first scalar found."*
+- Segment — `xerj-storage::doc_values::KeywordColumn` stores `ords: Vec<u32>`,
+ exactly **one ordinal per doc**; the format is structurally single-valued.
+
+Aggregations use a *separate* multi-valued path (`aggs.rs` holds
+`Vec<Vec<String>>`), and full-text joins every element into the token stream —
+which is why `terms`-aggs and `match` both see all elements, but exact `term`
+sees only `[0]`.
+
+Reproduced end-to-end (docs indexed with `calls: ["first_fn","second_fn","wp_safe_remote_get"]`):
+
+```
+term calls=first_fn -> 1 (element [0])
+term calls=second_fn -> 0 (element [1] — MISSED)
+term calls=wp_safe_remote_get-> 0 (element [2] — MISSED)
+```
+
+This is an Elasticsearch-compatibility break: in ES a keyword array is
+multi-valued and `term` matches **any** element.
+
+## The fix
+
+The always-correct source scan already handles arrays —
+`json_values_equal` has `(Value::Array(arr), _) => arr.iter().any(|e| e == q)` —
+so the fix is to route `term`/`terms` on array-valued keyword fields to that scan
+instead of the single-valued column fast path.
+
+- **Memtable half (in this PR).** `DocValues` already tracks `array_fields`.
+ `doc_values_term_query`, `doc_values_term_indices`, and `doc_values_terms_query`
+ now bail (`return None`) for any field in `array_fields`, exactly mirroring the
+ existing `keyword_has_whitespace` bail, so the caller falls through to the
+ array-aware `doc_matches_query` scan. Provably correct; compiles clean.
+
+- **Segment half (design, follow-up).** The flush builder *already* computes a
+ `multi_valued` set and does `keyword.remove(f)` for those fields (so the segment
+ `term` reader's `None` arm sets `abandoned = true` → stored-source scan). The
+ reproduction shows a case where a flushed field still resolves via a
+ single-valued column, so the remaining work is to (a) guarantee `multi_valued`
+ detection covers every ingest/flush path, and (b) route the remaining
+ fused-columnar `term` fast path to the scan for these fields — OR make
+ `KeywordColumn` genuinely multi-valued (`ords: Vec<Vec<u32>>`). A regression
+ test (`test_term_matches_non_first_array_element`) is included, `#[ignore]`d
+ until the segment half lands.
+
+**Also found (separate, smaller):** boolean `term` is matched as a string —
+`{term:{f:true}}` (JSON bool) returns 0 while `{term:{f:"true"}}` matches. The
+JSON-bool query value must coerce to the stored `"true"`/`"false"` form. Filed
+for the same PR series.
+
+## Token savings and other benefits
+
+The bug forces every interprocedural/graph query off the structured index and
+onto a workaround: either **scroll the whole graph to the client and traverse in
+code**, or **grep + read** the candidates. Fixing `term`/`terms` on arrays turns
+the reverse call-graph into a single structured query.
+
+Measured, for one reachability query ("who calls `wp_safe_remote_*`") over the
+real WordPress index (11,990 functions):
+
+| approach | tokens | vs fixed |
+|---|--:|--:|
+| scroll the whole call-graph, traverse client-side | ~482,800 | 2,823× |
+| grep + read the callers to confirm | ~4,300 | 25× |
+| **fixed: one `terms` query → exact callers as facts** | **~170** | — |
+
+The deeper benefit is **composability**. With `term`/`terms` correct on arrays,
+graph predicates combine in one request — e.g. *"functions that call a SQL sink
+**and** read a request source **and** lack an object-scoped cap"* becomes a single
+`bool` query returning only the candidates, instead of transferring the entire
+index (~2.4M tokens of facts) to rebuild the graph in Python. The audit cost drops
+from **O(index size)** to **O(matching results)**.
+
+Other benefits:
+- **Correctness / ES-compat** — restores multi-valued keyword semantics; removes a
+ class of silent false negatives (the most dangerous error for security audits).
+- **Every array-of-keywords field** benefits: tags, labels, roles, call edges,
+ sink lists — any workload that filters on membership.
+- It makes the pre-AST index **strictly better than grep** for reachability, where
+ today (per the field test) it is worse.
+
+## Files
+
+- `engine/crates/xerj-engine/src/memtable.rs` — array-field bail in the three
+ doc-values `term`/`terms` fast paths (memtable half of the fix).
+- `engine/crates/xerj-engine/tests/integration.rs` —
+ `test_term_matches_non_first_array_element` reproduction (`#[ignore]` pending the
+ segment half).
diff --git a/engine/crates/xerj-engine/src/memtable.rs b/engine/crates/xerj-engine/src/memtable.rs
index da8e2e6ec..43ba9a650 100644
--- a/engine/crates/xerj-engine/src/memtable.rs
+++ b/engine/crates/xerj-engine/src/memtable.rs
@@ -2888,7 +2888,15 @@ impl FtsMemtable {
// full analyzed-text source; a `term` query expects a token match
// which only the FTS / scan path can serve. Was an O(N) per-query
// column prescan.
- if self.doc_values.keyword_has_whitespace.contains(field) {
+ //
+ // Array/multi-valued bailout: a keyword ARRAY stores only its FIRST
+ // element in this single-valued column (see `push_field`), so a
+ // `term` on any non-first element would be silently missed. Bail to
+ // the array-aware source scan (`json_values_equal` matches on array
+ // membership). This is the fix for ES-incompatible term-on-array.
+ if self.doc_values.keyword_has_whitespace.contains(field)
+ || self.doc_values.array_fields.contains(field)
+ {
return None;
}
// Step 1: walk the whole column for an exact total but only clone
@@ -2936,6 +2944,12 @@ impl FtsMemtable {
/// by the bool intersection shortcut to avoid 170k String allocations
/// per query.
pub fn doc_values_term_indices(&self, field: &str, value: &str) -> Option<Vec<usize>> {
+ // Array/multi-valued keyword fields keep only their first element in the
+ // single-valued column; term matching must fall back to the array-aware
+ // source scan (mirrors the bail in `doc_values_term_query`).
+ if self.doc_values.array_fields.contains(field) {
+ return None;
+ }
let col = self.doc_values.keyword.get(field)?;
let results: Vec<usize> = col
.iter()
@@ -3033,7 +3047,11 @@ impl FtsMemtable {
// from the full source string (not the token stream). A `terms`
// query compares against tokens in that case, which doc-values
// can't serve — bail so callers fall through to the scan path.
- if self.doc_values.keyword_has_whitespace.contains(field) {
+ // Array/multi-valued keyword fields (only the first element is stored
+ // in the single-valued column) likewise bail to the array-aware scan.
+ if self.doc_values.keyword_has_whitespace.contains(field)
+ || self.doc_values.array_fields.contains(field)
+ {
return None;
}
// Step 1: exact total, bounded doc_id materialisation.
diff --git a/engine/crates/xerj-engine/tests/integration.rs b/engine/crates/xerj-engine/tests/integration.rs
index cc3b2f81a..cbc81efc2 100644
--- a/engine/crates/xerj-engine/tests/integration.rs
+++ b/engine/crates/xerj-engine/tests/integration.rs
@@ -5495,3 +5495,61 @@ async fn test_highlight_survives_source_filtering() {
"requested field must survive"
);
}
+
+// ── Reproduction: term/terms on a NON-FIRST array element (multi-valued keyword) ─
+// A keyword ARRAY currently stores ONLY element [0] in the single-valued
+// doc-values column (memtable `push_field`) AND the single-valued segment
+// `KeywordColumn.ords: Vec<u32>` (one ordinal per doc). So `term` on any later
+// element silently returns 0 hits — ES treats keyword arrays as multi-valued.
+// This is the reachability bug found auditing WordPress:
+// `{term:{calls:"wp_safe_remote_get"}}` missed every caller where it was not
+// the first call (found 1 of 9; grep found 14).
+//
+// The memtable half of the fix (bail array fields to the array-aware source
+// scan) is in `memtable.rs`. The COMPLETE fix additionally needs multi-valued
+// segment keyword columns (or a per-segment array-field marker that bails the
+// segment term reader to the stored-source scan) — a storage-format change.
+// Ignored until that lands; unignore to verify the full fix.
+#[ignore = "needs multi-valued segment keyword columns; memtable half fixed"]
+#[tokio::test]
+async fn test_term_matches_non_first_array_element() {
+ let dir = TempDir::new().unwrap();
+ let engine = make_engine(&dir);
+ engine.create_index("arr", Schema::empty()).unwrap();
+ let idx = engine.get_index("arr").unwrap();
+
+ idx.index_document(
+ Some("1".into()),
+ json!({ "name": "d1", "calls": ["first_fn", "second_fn", "wp_safe_remote_get"] }),
+ )
+ .await
+ .unwrap();
+ idx.index_document(
+ Some("2".into()),
+ json!({ "name": "d2", "calls": ["unrelated_only"] }),
+ )
+ .await
+ .unwrap();
+
+ let r = idx
+ .search(&make_search(json!({"term": {"calls": "wp_safe_remote_get"}})))
+ .await
+ .unwrap();
+ assert_eq!(
+ r.total.value, 1,
+ "term must match a NON-FIRST array element (multi-valued keyword)"
+ );
+ assert_eq!(r.hits[0].id, "1");
+
+ let r0 = idx
+ .search(&make_search(json!({"term": {"calls": "first_fn"}})))
+ .await
+ .unwrap();
+ assert_eq!(r0.total.value, 1, "first element must still match");
+
+ let rt = idx
+ .search(&make_search(json!({"terms": {"calls": ["wp_safe_remote_get"]}})))
+ .await
+ .unwrap();
+ assert_eq!(rt.total.value, 1, "terms must match a NON-FIRST array element");
+}