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): 400 an unanswerable field in a multi-knn array clause (#542)

xerj-org71c0216f3bfb
diff --git a/engine/crates/xerj-api/tests/knn_on_missing_field_400s.rs b/engine/crates/xerj-api/tests/knn_on_missing_field_400s.rs
index 9d73f921b..c955e200d 100644
--- a/engine/crates/xerj-api/tests/knn_on_missing_field_400s.rs
+++ b/engine/crates/xerj-api/tests/knn_on_missing_field_400s.rs
@@ -166,3 +166,58 @@ async fn knn_on_declared_but_empty_vector_field_is_empty_200() {
.unwrap_or(u64::MAX);
assert_eq!(total, 0, "expected an empty result set: {body}");
}
+
+/// #542 (follow-up to #498): the MULTI-knn array form (`knn: [...]`) must also
+/// 400 a clause naming an unanswerable field — #498 only wired the check into
+/// the single top-level knn arm, so the array form still returns a silent
+/// empty 200.
+#[tokio::test]
+async fn multi_knn_on_absent_field_is_400_not_silent_empty() {
+ let (app, _dir) = app().await;
+ let (status, body) = json_req(
+ &app,
+ "PUT",
+ "/codes",
+ json!({ "mappings": { "properties": {
+ "name": { "type": "text" },
+ "code": { "type": "keyword" }
+ } } }),
+ )
+ .await;
+ assert!(status.is_success(), "create index: {status} {body}");
+ let (_s, _b) = json_req(
+ &app,
+ "POST",
+ "/codes/_doc/1",
+ json!({ "name": "sniff", "code": "fn sniff() {}" }),
+ )
+ .await;
+ let (_s, _b) = json_req(&app, "POST", "/codes/_refresh", json!({})).await;
+
+ // The ES multi-knn array form with 2+ clauses (a single-element array
+ // collapses to the single-knn path, already fixed by #498). A clause naming
+ // a field absent from the mapping must fail loudly, not return a silent
+ // empty 200.
+ let (status, body) = json_req(
+ &app,
+ "POST",
+ "/codes/_search",
+ json!({ "knn": [
+ { "field": "embedding", "query_vector": [0.1, 0.2, 0.3], "k": 5, "num_candidates": 50 },
+ { "field": "embedding2", "query_vector": [0.4, 0.5, 0.6], "k": 5, "num_candidates": 50 }
+ ] }),
+ )
+ .await;
+ assert_eq!(
+ status,
+ StatusCode::BAD_REQUEST,
+ "multi-knn on an absent field must be 400, not a silent empty 200 (#542): got {status} {body}"
+ );
+ assert!(
+ body.pointer("/error/reason")
+ .and_then(Value::as_str)
+ .unwrap_or_default()
+ .contains("embedding"),
+ "the 400 must name the field: {body}"
+ );
+}
diff --git a/engine/crates/xerj-engine/src/index.rs b/engine/crates/xerj-engine/src/index.rs
index 3c7ac669b..96065df8e 100644
--- a/engine/crates/xerj-engine/src/index.rs
+++ b/engine/crates/xerj-engine/src/index.rs
@@ -14100,9 +14100,21 @@ impl Index {
// one clause, and `hits.total` is the size of the deduplicated union
// — matching ES 8.13 multi-kNN semantics (live-verified 2026-07-12).
if let Some(clauses) = peel_multi_knn_query(query) {
- return self
+ let fields: Vec<String> = clauses.iter().map(|c| c.field.clone()).collect();
+ let result = self
.run_multi_knn_brute_force(request, search_deadline, clauses)
- .await;
+ .await?;
+ // #542: each knn clause validates its own field. On an empty,
+ // non-timed-out union, reject the first clause naming an
+ // unanswerable field — the same execution-truth check the single-knn
+ // arm uses (#498), so declared-but-empty and indexed-but-undeclared
+ // fields still return their real empty result.
+ if result.hits.is_empty() && !result.timed_out {
+ for field in &fields {
+ self.reject_unanswerable_knn_field(field).await?;
+ }
+ }
+ return Ok(result);
}
// Nested `knn` query: `nested { path: P, query: { knn { field: P.vec } } }`.
// ES scores each parent by the best-matching nested element and