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): project _source filters inside arrays of objects (#644)

xerj-org94a41ead5788
diff --git a/engine/crates/xerj-engine/src/index.rs b/engine/crates/xerj-engine/src/index.rs
index a3601e9f7..521c7d678 100644
--- a/engine/crates/xerj-engine/src/index.rs
+++ b/engine/crates/xerj-engine/src/index.rs
@@ -26697,13 +26697,28 @@ fn filter_object(source: &Value, includes: &[String], excludes: &[String]) -> Va
out.insert(key.clone(), filtered);
}
}
- // An array is kept WHOLE whenever its key is selected: XERJ does
- // not project inside arrays (documented scope), which also keeps
- // it free of the projection-induced data loss #602 must avoid.
- // (Consequence: an exclude cannot strip fields inside an array of
- // objects — tracked separately, not a #602 regression.)
- Value::Array(_) => {
- out.insert(key.clone(), val.clone());
+ // ES treats an array index as transparent to a `_source` path,
+ // so a pattern projects INTO each element (#644): `tags.name`
+ // matches `tags[i].name`. A fully-included array with no exclude
+ // reaching into it is kept whole (the fast path); otherwise each
+ // element is filtered at the SAME `child` path — an object
+ // element recurses, a scalar element survives only in an
+ // accepting state (like a scalar leaf).
+ Value::Array(arr) => {
+ if child_all && !exc_live {
+ out.insert(key.clone(), val.clone());
+ } else {
+ let projected: Vec<Value> = arr
+ .iter()
+ .filter_map(|elem| match elem {
+ Value::Object(_) => {
+ Some(es_filter(elem, &child, child_all, includes, excludes))
+ }
+ other => child_all.then(|| other.clone()),
+ })
+ .collect();
+ out.insert(key.clone(), Value::Array(projected));
+ }
}
// A scalar leaf is kept ONLY in an ACCEPTing state. A scalar that
// merely sits on a LIVE (not-yet-accepted) include path cannot
@@ -27118,6 +27133,35 @@ mod source_filter_tests {
);
}
+ /// #644: ES projects INSIDE an array of objects — the array index is
+ /// transparent to a `_source` path, so `include ["tags.name"]` keeps only
+ /// each element's `name`, and `exclude ["logs.secret"]` scrubs `secret` from
+ /// every element. XERJ currently keeps the array whole (the #602 no-array-
+ /// projection scope), leaking the un-selected fields.
+ #[test]
+ fn source_filter_projects_inside_arrays_of_objects() {
+ // include: keep only the named leaf inside each array element.
+ assert_eq!(
+ filter_object(
+ &json!({ "tags": [{ "name": "x", "id": 1 }, { "name": "y", "id": 2 }], "n": 9 }),
+ &["tags.name".to_string()],
+ &[]
+ ),
+ json!({ "tags": [{ "name": "x" }, { "name": "y" }] }),
+ "#644: include tags.name must project into each array element"
+ );
+ // exclude: scrub the named leaf from each array element.
+ assert_eq!(
+ filter_object(
+ &json!({ "logs": [{ "msg": 1, "secret": 2 }, { "msg": 3, "secret": 4 }] }),
+ &[],
+ &["logs.secret".to_string()]
+ ),
+ json!({ "logs": [{ "msg": 1 }, { "msg": 3 }] }),
+ "#644: exclude logs.secret must strip secret from every array element"
+ );
+ }
+
#[test]
fn explicit_source_true_keeps_mapping_derived_companions() {
let companions = generated_embedding_companion_fields(&embedded_schema());