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): missing agg counts empty/only-null arrays as missing (#844) (#845)

xerj-orged5c92baa061
diff --git a/engine/crates/xerj-engine/src/aggs.rs b/engine/crates/xerj-engine/src/aggs.rs
index f60fd2301..46a0119b3 100644
--- a/engine/crates/xerj-engine/src/aggs.rs
+++ b/engine/crates/xerj-engine/src/aggs.rs
@@ -7264,8 +7264,11 @@ fn run_missing(params: &Value, docs: &[Value]) -> Value {
} else {
docs.iter()
.filter(|doc| {
- let v = get_nested_field(doc, field);
- v.is_null()
+ // A doc is "missing" the field iff it has no non-null value —
+ // the exact complement of `exists` (#792/#793 value_present):
+ // absent, null, `[]` and `[null]` all count as missing, so
+ // `is_null()` alone (which kept `[]`/`[null]`) undercounted.
+ !crate::index::value_present(get_nested_field(doc, field))
})
.count()
};
@@ -13818,6 +13821,22 @@ mod tests {
);
}
+ #[test]
+ fn missing_agg_counts_empty_and_null_arrays() {
+ // ES `missing` agg counts docs with NO value: absent, null, [] and
+ // [null] all count as missing; a real value does not. This is the
+ // exact complement of `exists` (#792/#793 value_present).
+ let docs = vec![
+ json!({ "f": 5 }), // present
+ json!({ "f": null }), // missing
+ json!({ "f": [] }), // missing (empty array)
+ json!({ "f": [null] }), // missing (only-null array)
+ json!({ "other": 1 }), // missing (absent)
+ ];
+ let r = run_aggs(&json!({ "m": { "missing": { "field": "f" } } }), &docs);
+ assert_eq!(r["m"]["doc_count"].as_u64(), Some(4), "got {r:?}");
+ }
+
#[test]
fn passage_metadata_is_not_an_aggregation_field() {
let doc = json!({
diff --git a/engine/crates/xerj-engine/src/index.rs b/engine/crates/xerj-engine/src/index.rs
index 0b4994c25..80fd1e34e 100644
--- a/engine/crates/xerj-engine/src/index.rs
+++ b/engine/crates/xerj-engine/src/index.rs
@@ -37240,7 +37240,7 @@ fn doc_matches_query(q: &QueryNode, source: &Value) -> bool {
/// `[null]` are NOT present; `[null, "x"]` is (it has a non-null element).
/// Scalars and objects are present (object sub-field recursion is a separate
/// concern the dense/declared paths already handle).
-fn value_present(v: &Value) -> bool {
+pub(crate) fn value_present(v: &Value) -> bool {
match v {
Value::Null => false,
Value::Array(a) => a.iter().any(value_present),