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(api): #1093 — unknown lookup fields refused under opt-in strict mode; autoindex indices strict at create

xerj-org6058aeeb82b1
diff --git a/engine/README.md b/engine/README.md
index a12d8269f..dd4fe452e 100644
--- a/engine/README.md
+++ b/engine/README.md
@@ -212,6 +212,25 @@ match against flat documents and return silently wrong hits. It refuses at
parse time instead, with a message that names the alternative (denormalize the
relationship). Any other query type answers `unknown query type`.
+**Unknown fields in lookup clauses: strict by choice, not by default.**
+ES answers a lookup (`term`, `terms`, `match`, `match_phrase`,
+`match_phrase_prefix`, `prefix`, `wildcard`, `regexp`, `fuzzy`, `range`) naming
+a field the index does not map with a silent 0-hit — which a caller cannot
+distinguish from an empty corpus; the map-gate measurement counted 5 silent
+wrong-field misses in 30 agent intents. XERJ keeps that default on the wire
+(the ES-YAML suite has legitimate cases querying an unmapped `missing_field`)
+but answers it with the `_xerj.hints` `unknown_field` diagnostic, and refuses
+with a **400 naming the field** and the searchable fields where strictness is
+asked for: `?strict_unknown_fields=true` per request,
+`index.query.strict_unknown_fields=true` per index (one strict target covers
+the whole request), and — because `xerj autoindex` PUTs an explicit full
+schema, where a wrong field name is a mistake and not a dynamic-mapping
+probe — every index autoindex creates. `?strict_unknown_fields=false` opts
+one request back out. Two exemptions hold either way: `exists` on an unmapped
+field stays a truthful 0-hit, and a just-created index with no mappings yet
+is never validated. `_msearch` applies the same rule per item (setting only
+— NDJSON items carry no URL params).
+
## Aggregation Types Supported
Complete and machine-checked the same way, from
diff --git a/engine/crates/xerj-api/src/es_compat.rs b/engine/crates/xerj-api/src/es_compat.rs
index ff7d4fd24..0e161d572 100644
--- a/engine/crates/xerj-api/src/es_compat.rs
+++ b/engine/crates/xerj-api/src/es_compat.rs
@@ -6938,6 +6938,16 @@ pub struct EsSearchQueryParams {
/// * `exact` — serialize and count every omitted byte.
/// * `false` / `off` / `0` — do not measure, emit nothing.
pub savings: Option<String>,
+ /// xerj extension, `?strict_unknown_fields=` (#1093): refuse lookup
+ /// clauses (`term`, `match`, `range`, …) that name fields no target
+ /// index maps, instead of ES's silent 0-hit 200.
+ ///
+ /// * `true` — enforce for this request regardless of index settings.
+ /// * `false` / `off` / `0` — never enforce, regardless of index settings
+ /// (the explicit escape hatch for one request against a strict index).
+ /// * absent — each target index's `index.query.strict_unknown_fields`
+ /// setting decides; a strict mark on ANY target covers the request.
+ pub strict_unknown_fields: Option<String>,
}
/// Parse `?savings=` into a measurement mode. Anything unrecognised falls
@@ -7313,24 +7323,142 @@ fn fields_request_includes_passage(fields: &Value) -> bool {
/// every response the caller already handled correctly is exactly the kind
/// of noise this project's review guidance rules out. Returns `None`
/// whenever the hint would not be actionable.
+/// Field-keyed leaf clause types (`{"match": {"field": …}}`), shared by the
+/// unknown-field hint and the #1093 refusal so the two walks cannot drift.
+const FIELD_KEYED_CLAUSES: &[&str] = &[
+ "match",
+ "match_phrase",
+ "match_phrase_prefix",
+ "term",
+ "terms",
+ "prefix",
+ "wildcard",
+ "regexp",
+ "fuzzy",
+ "range",
+];
+
+/// A query field is "known" when any of `schemas` maps it, maps its parent
+/// (`symbols.name.keyword` → `symbols`), or it is an ES metadata field
+/// (`_id`, `_index`, …) a query may legitimately target.
+fn field_known_in_any(name: &str, schemas: &[&xerj_common::types::Schema]) -> bool {
+ if name.starts_with('_') {
+ return true;
+ }
+ schemas.iter().any(|s| {
+ s.field(name).is_some()
+ || name
+ .split_once('.')
+ .is_some_and(|(parent, _)| s.field(parent).is_some())
+ })
+}
+
+/// #1093: `(clause, field)` pairs named by lookup clauses that no schema in
+/// `schemas` maps — the clauses `_search` refuses instead of answering with
+/// ES's silent zero-hit 200, which a caller cannot tell from an empty corpus.
+///
+/// Every clause in [`FIELD_KEYED_CLAUSES`] is a lookup: zero hits there is a
+/// claim about the data, and a wrong field name makes it a false claim.
+/// `exists` is deliberately absent — zero IS the truthful answer to "does any
+/// document have this field" — and keeps its `_xerj.hints` diagnostic instead
+/// of a refusal. `multi_match` appears once, joining its field list, and only
+/// when EVERY field it names is unknown (a half-real multi_match still
+/// searches the fields that exist, so it gets the hint, not the 400); a
+/// wildcard pattern field (`"ob*"` — legal in `multi_match.fields`) counts as
+/// known when it matches at least one mapped field in any schema.
+fn unknown_lookup_clause_fields(
+ q: &Value,
+ schemas: &[&xerj_common::types::Schema],
+ out: &mut Vec<(String, String)>,
+) {
+ match q {
+ Value::Object(o) => {
+ for (k, v) in o {
+ if FIELD_KEYED_CLAUSES.contains(&k.as_str()) {
+ if let Value::Object(inner) = v {
+ for f in inner.keys() {
+ if !field_known_in_any(f, schemas) {
+ let entry = (k.clone(), f.clone());
+ if !out.contains(&entry) {
+ out.push(entry);
+ }
+ }
+ }
+ }
+ } else if k == "multi_match" {
+ if let Some(Value::Array(fs)) = v.get("fields") {
+ let names: Vec<String> = fs
+ .iter()
+ .filter_map(Value::as_str)
+ // `body^3` — the boost is not part of the name.
+ .map(|f| f.split('^').next().unwrap_or(f).to_string())
+ .collect();
+ let all_unknown = !names.is_empty()
+ && names.iter().all(|f| {
+ if f.contains('*') {
+ !schemas.iter().any(|s| {
+ s.fields.iter().any(|sf| glob_match_simple(f, &sf.name))
+ })
+ } else {
+ !field_known_in_any(f, schemas)
+ }
+ });
+ if all_unknown {
+ out.push(("multi_match".to_string(), names.join(", ")));
+ }
+ }
+ } else if k == "span_multi" {
+ // `{"span_multi": {"match": {"prefix": {"field": …}}}}` —
+ // the inner multi-term objects are NOT field-keyed (their
+ // keys are `field`/`value`/`boost`), so the generic walk
+ // would read "field" itself as a field name. The field is
+ // the `field` member of the wrapped multi-term spec.
+ if let Some(m) = v.get("match").and_then(Value::as_object) {
+ for (mt, spec) in m {
+ if let Some(f) = spec.get("field").and_then(Value::as_str) {
+ if !field_known_in_any(f, schemas) {
+ let entry = (format!("span_multi {mt}"), f.to_string());
+ if !out.contains(&entry) {
+ out.push(entry);
+ }
+ }
+ }
+ }
+ }
+ } else if k == "intervals" {
+ // `{"intervals": {"<field>": {"match"|"prefix"|…: {…}}}}` —
+ // the field is the KEY; the spec's own `prefix`/`wildcard`/
+ // `regexp`/`fuzzy` keys are interval kinds, not fields, and
+ // must not be walked as field-keyed clauses.
+ if let Some(inner) = v.as_object() {
+ for (f, _) in inner {
+ if !field_known_in_any(f, schemas) {
+ let entry = ("intervals".to_string(), f.clone());
+ if !out.contains(&entry) {
+ out.push(entry);
+ }
+ }
+ }
+ }
+ } else {
+ unknown_lookup_clause_fields(v, schemas, out);
+ }
+ }
+ }
+ Value::Array(a) => a
+ .iter()
+ .for_each(|v| unknown_lookup_clause_fields(v, schemas, out)),
+ _ => {}
+ }
+}
+
/// Field names a query refers to, gathered from the ordinary ES leaf shapes.
///
/// Subfield suffixes are kept (`symbols.name.keyword`) but the parent is also
/// recorded, so a query against a real field's subfield is not reported as
/// unknown.
fn referenced_query_fields(q: &Value, out: &mut Vec<String>) {
- const FIELD_KEYED: &[&str] = &[
- "match",
- "match_phrase",
- "match_phrase_prefix",
- "term",
- "terms",
- "prefix",
- "wildcard",
- "regexp",
- "fuzzy",
- "range",
- ];
+ const FIELD_KEYED: &[&str] = FIELD_KEYED_CLAUSES;
match q {
Value::Object(o) => {
for (k, v) in o {
@@ -7351,6 +7479,25 @@ fn referenced_query_fields(q: &Value, out: &mut Vec<String>) {
out.push(f.split('^').next().unwrap_or(f).to_string());
}
}
+ } else if k == "span_multi" {
+ // The wrapped multi-term spec is not field-keyed; its
+ // `field` member is the reference (see the matching case
+ // in `unknown_lookup_clause_fields`).
+ if let Some(m) = v.get("match").and_then(Value::as_object) {
+ for spec in m.values() {
+ if let Some(f) = spec.get("field").and_then(Value::as_str) {
+ out.push(f.to_string());
+ }
+ }
+ }
+ } else if k == "intervals" {
+ // Fields are the keys of the intervals object; the spec
+ // bodies are interval kinds, not clauses.
+ if let Some(inner) = v.as_object() {
+ for f in inner.keys() {
+ out.push(f.clone());
+ }
+ }
} else {
referenced_query_fields(v, out);
}
@@ -7408,17 +7555,7 @@ fn unknown_field_hint(
// Text-ish fields are what a full-text query actually wants; listing every
// keyword and numeric column would bury the answer.
- let searchable: Vec<String> = schema
- .fields
- .iter()
- .filter(|f| {
- matches!(
- f.field_type,
- xerj_common::types::FieldType::Text | xerj_common::types::FieldType::Keyword
- ) && !f.name.starts_with("ax_")
- })
- .map(|f| f.name.clone())
- .collect();
+ let searchable = searchable_text_fields(schema);
let primary = searchable
.iter()
@@ -7450,6 +7587,165 @@ fn unknown_field_hint(
}))
}
+/// Text-ish fields are what a full-text query actually wants; listing every
+/// keyword and numeric column would bury the answer.
+fn searchable_text_fields(schema: &xerj_common::types::Schema) -> Vec<String> {
+ schema
+ .fields
+ .iter()
+ .filter(|f| {
+ matches!(
+ f.field_type,
+ xerj_common::types::FieldType::Text | xerj_common::types::FieldType::Keyword
+ ) && !f.name.starts_with("ax_")
+ })
+ .map(|f| f.name.clone())
+ .collect()
+}
+
+/// The #1093 refusal reason for a search, or `None` when the query's lookup
+/// clauses are all known, no strictness is requested, or there is nothing to
+/// validate against.
+///
+/// Strictness: the `?strict_unknown_fields=` URL param, or the
+/// `index.query.strict_unknown_fields` setting on ANY resolved index — a
+/// strict mark anywhere in the target set must cover the whole request, or
+/// a heterogeneous alias would answer the very 0-hit its strict member was
+/// configured to refuse.
+///
+/// Fresh indices (schema with no fields yet — dynamic mapping has seen no
+/// documents) are skipped on both sides: they cannot prove a field unknown,
+/// and when every target is fresh the request is exactly the ES case where
+/// the field may simply not have arrived yet.
+/// Is `index.query.strict_unknown_fields` truthy in this settings tree?
+/// Three spellings reach storage: nested (`{"index": {"query": …}}`, also
+/// the shape a wrapper-less body lands in), a flat top-level key, and a flat
+/// key merged UNDER `index` by the `_settings` handler's normalization —
+/// ES clients routinely send flat dotted keys, and that handler stores them
+/// verbatim.
+fn strict_unknown_fields_set(settings: &Value) -> bool {
+ let truthy = |v: &Value| {
+ matches!(v, Value::Bool(true)) || v.as_str().is_some_and(|s| s == "true" || s == "1")
+ };
+ if let Some(v) = settings.pointer("/index/query/strict_unknown_fields") {
+ if truthy(v) {
+ return true;
+ }
+ }
+ [Some(settings), settings.get("index")]
+ .into_iter()
+ .flatten()
+ .any(|container| {
+ container
+ .get("index.query.strict_unknown_fields")
+ .is_some_and(truthy)
+ })
+}
+
+/// The #1093 refusal reason for a search, or `None` when the query's lookup
+/// clauses are all known, no target asks for strict, or there is nothing to
+/// validate against.
+///
+/// Strictness: the `?strict_unknown_fields=` URL param (`false`/`off`/`0`
+/// forces lenient and beats any setting; `true` enforces outright; absent
+/// defers to the settings), then the `index.query.strict_unknown_fields`
+/// setting on ANY resolved index — a strict mark anywhere in the target set
+/// covers the whole request, or a heterogeneous alias would silently answer
+/// the very 0-hit its strict member was configured to refuse.
+///
+/// The default is ES's own lenient behaviour: a lookup on an unmapped field
+/// is a 0-hit 200 (with the `_xerj.hints` diagnostic). Turning the refusal
+/// on wholesale broke the ES-YAML suite (aggregation cases legitimately
+/// query `missing_field` expecting 200), so strictness is opt-in — and
+/// `xerj autoindex` turns it on for the indices it creates, whose explicit
+/// full schemas are exactly where a wrong field name is a mistake and not
+/// a dynamic-mapping probe (#1093's measured case: 5 of 30 agent intents).
+async fn unknown_lookup_field_reason(
+ state: &crate::state::AppState,
+ index_names: &[&str],
+ strict_param: Option<&str>,
+ query: Option<&Value>,
+) -> Option<String> {
+ match strict_param {
+ Some("false") | Some("off") | Some("0") => return None,
+ _ => {}
+ }
+ let mut enforce = strict_param == Some("true");
+ let query = query?;
+ let mut schemas: Vec<xerj_common::types::Schema> = Vec::new();
+ for name in index_names {
+ // A missing index is not this check's error to report — the search
+ // itself 404s (or is skipped for ignore_unavailable) further down.
+ let Ok(idx) = state.engine.get_index(name) else {
+ continue;
+ };
+ // Two stores can carry the setting: the engine's `settings.json`
+ // copy (written at create) and the display map `PUT /_settings`
+ // merges into — generic keys are not forwarded to the engine store,
+ // so reading one of the two would silently miss the other.
+ let display = state
+ .engine
+ .index_settings
+ .get(*name)
+ .map(|v| v.clone())
+ .unwrap_or(Value::Null);
+ if !enforce
+ && (strict_unknown_fields_set(&display)
+ || strict_unknown_fields_set(&idx.get_settings().await))
+ {
+ enforce = true;
+ }
+ let schema = idx.schema().await;
+ if !schema.fields.is_empty() {
+ schemas.push(schema);
+ }
+ }
+ if !enforce {
+ return None;
+ }
+ // Every target is fresh (no mapped fields anywhere): nothing can be
+ // proven unknown, and this is exactly the ES case where the field may
+ // simply not have arrived yet — dynamic mapping has seen no documents.
+ if schemas.is_empty() {
+ return None;
+ }
+ let refs: Vec<&xerj_common::types::Schema> = schemas.iter().collect();
+ let mut unknown = Vec::new();
+ unknown_lookup_clause_fields(query, &refs, &mut unknown);
+ if unknown.is_empty() {
+ return None;
+ }
+
+ // `QueryError::UnknownField` was written for exactly this refusal and,
+ // until #1093, constructed nowhere. The per-clause reasons come from its
+ // Display so the crate's error vocabulary stays the one source.
+ let named = unknown
+ .iter()
+ .take(5)
+ .map(|(clause, field)| {
+ xerj_query::QueryError::UnknownField {
+ field: field.clone(),
+ context: format!("{clause} clause"),
+ }
+ .to_string()
+ })
+ .collect::<Vec<_>>()
+ .join("; ");
+ let searchable = searchable_text_fields(&schemas[0]);
+ Some(format!(
+ "{named}. This target enforces strict_unknown_fields because ES's answer \
+ here — a silent 0-hit — is indistinguishable from an empty corpus. \
+ Unset index.query.strict_unknown_fields (or send ?strict_unknown_fields=false \
+ for one request) to restore that behaviour; `exists` on an unmapped field \
+ stays a truthful 0-hit either way. Searchable text fields here: {}",
+ if searchable.is_empty() {
+ "(none)".to_string()
+ } else {
+ searchable.join(", ")
+ },
+ ))
+}
+
/// `term`/`terms` clauses in a query, as (field, value) pairs.
///
/// Only exact-match clauses: a `match` against text is analysed and a zero
@@ -8684,7 +8980,10 @@ mod semantic_text_lexical_hint_tests {
/// The hint rides alongside the existing diagnostics rather than
/// displacing one: a query naming a nonexistent field AND a
- /// semantic_text field must report both.
+ /// semantic_text field must report both. Runs in the default (lenient)
+ /// mode — the strict opt-in of #1093 would refuse this query before
+ /// hints exist, and the additivity being pinned here is a property of
+ /// lenient responses.
#[tokio::test]
async fn the_hint_is_additive_to_the_unknown_field_hint() {
let state = test_state();
@@ -8715,6 +9014,545 @@ mod semantic_text_lexical_hint_tests {
}
}
+/// #1093 — opt-in strict mode refuses lookup clauses (`term`, `match`,
+/// `range`, …) naming fields no target index maps, instead of ES's silent
+/// 0-hit 200 that a caller cannot tell from an empty corpus. The default is
+/// ES's own lenient answer (with the `_xerj.hints` diagnostic); strictness
+/// comes from `?strict_unknown_fields=true`, the
+/// `index.query.strict_unknown_fields` setting, or an autoindex-created
+/// index. Pinned here: the refusal, both opt-in routes and the per-request
+/// opt-out, the `exists` exemption, the fresh-index exemption, and the
+/// walker's span_multi/intervals special cases.
+#[cfg(test)]
+mod unknown_field_refusal_tests {
+ use super::*;
+ use axum::{
+ body::{to_bytes, Body},
+ http::Request,
+ };
+ use tower::ServiceExt;
+ use xerj_common::{
+ config::{Config, WalSync},
+ metrics::Metrics,
+ };
+ use xerj_engine::Engine;
+
+ fn test_state() -> AppState {
+ let dir = tempfile::tempdir().expect("tempdir").keep();
+ let mut config = Config::default();
+ config.server.data_dir = dir.to_string_lossy().into_owned();
+ config.storage.wal_sync = WalSync::Async;
+ let metrics = Metrics::new().expect("metrics");
+ let engine = Engine::new(config.clone()).expect("engine");
+ AppState::new(config, engine, metrics)
+ }
+
+ async fn request_json(
+ state: &AppState,
+ method: &str,
+ path: &str,
+ body: Value,
+ ) -> (StatusCode, Value) {
+ let response = crate::router::build_es_compat_router(state.clone())
+ .oneshot(
+ Request::builder()
+ .method(method)
+ .uri(path)
+ .header("content-type", "application/json")
+ .body(Body::from(body.to_string()))
+ .expect("request"),
+ )
+ .await
+ .expect("route response");
+ let status = response.status();
+ let bytes = to_bytes(response.into_body(), usize::MAX).await.unwrap();
+ let json = serde_json::from_slice(&bytes).unwrap_or_else(|error| {
+ panic!(
+ "{method} {path} returned non-JSON ({error}): {}",
+ String::from_utf8_lossy(&bytes)
+ )
+ });
+ (status, json)
+ }
+
+ /// `ctx` text + `status` keyword, three documents.
+ async fn build_index(state: &AppState, index: &str) {
+ let (status, body) = request_json(
+ state,
+ "PUT",
+ &format!("/{index}"),
+ json!({"mappings": {"properties": {
+ "ctx": {"type": "text"},
+ "status": {"type": "keyword"},
+ "title": {"type": "text"},
+ }}}),
+ )
+ .await;
+ assert_eq!(status, StatusCode::OK, "{body}");
+ for (id, text) in ["alpha graph", "beta edges", "gamma nodes"]
+ .iter()
+ .enumerate()
+ {
+ let (status, body) = request_json(
+ state,
+ "PUT",
+ &format!("/{index}/_doc/{id}"),
+ json!({"ctx": text, "status": "ok", "title": format!("doc {id}")}),
+ )
+ .await;
+ assert!(status.is_success(), "{body}");
+ }
+ state
+ .engine
+ .get_index(index)
+ .expect("index")
+ .refresh()
+ .await
+ .expect("refresh");
+ }
+
+ fn reason(json: &Value) -> String {
+ json["error"]["root_cause"][0]["reason"]
+ .as_str()
+ .unwrap_or_default()
+ .to_string()
+ }
+
+ /// The refusal itself: every unknown lookup clause is named, the envelope
+ /// is the house 400, and the reason carries the way out.
+ #[tokio::test]
+ async fn unknown_lookup_fields_are_refused_with_every_clause_named() {
+ let state = test_state();
+ build_index(&state, "uf-strict").await;
+
+ let (status, json) = request_json(
+ &state,
+ "POST",
+ "/uf-strict/_search?strict_unknown_fields=true",
+ json!({"query": {"bool": {"must": [
+ {"term": {"stauts": "ok"}},
+ {"range": {"titel": {"gte": "a"}}},
+ ]}}}),
+ )
+ .await;
+ assert_eq!(status, StatusCode::BAD_REQUEST, "{json}");
+ assert_eq!(json["error"]["type"], "search_phase_execution_exception");
+ let r = reason(&json);
+ assert!(
+ r.contains("unknown field `stauts` in term clause")
+ && r.contains("unknown field `titel` in range clause"),
+ "both clauses named: {r}"
+ );
+ assert!(
+ r.contains("strict_unknown_fields=false"),
+ "the per-request opt-out is in the reason: {r}"
+ );
+
+ // The default, no param: ES's own silent 0-hit.
+ let (status, json) = request_json(
+ &state,
+ "POST",
+ "/uf-strict/_search",
+ json!({"query": {"term": {"stauts": "ok"}}}),
+ )
+ .await;
+ assert_eq!(status, StatusCode::OK, "default is lenient: {json}");
+ assert_eq!(json["hits"]["total"]["value"], 0, "{json}");
+ }
+
+ /// The default carries the `_xerj.hints` unknown-field diagnostic, so a
+ /// lenient caller keeps the explanation a strict caller gets as a 400 —
+ /// and the per-request opt-out beats a strict index setting.
+ #[tokio::test]
+ async fn the_default_is_lenient_with_the_hint_and_false_beats_the_setting() {
+ let state = test_state();
+ build_index(&state, "uf-lenient").await;
+
+ let (status, json) = request_json(
+ &state,
+ "POST",
+ "/uf-lenient/_search",
+ json!({"query": {"term": {"stauts": "ok"}}}),
+ )
+ .await;
+ assert_eq!(status, StatusCode::OK, "{json}");
+ assert_eq!(json["hits"]["total"]["value"], 0, "{json}");
+ let codes = json["_xerj"]["hints"]
+ .as_array()
+ .map(|h| {
+ h.iter()
+ .filter_map(|x| x["code"].as_str())
+ .collect::<Vec<_>>()
+ })
+ .unwrap_or_default();
+ assert!(
+ codes.contains(&"unknown_field"),
+ "lenient mode keeps the hint: {codes:?} / {json}"
+ );
+
+ // Now make the index strict and opt out for one request.
+ let (status, body) = request_json(
+ &state,
+ "PUT",
+ "/uf-lenient/_settings",
+ json!({"index.query.strict_unknown_fields": true}),
+ )
+ .await;
+ assert_eq!(status, StatusCode::OK, "{body}");
+ let (status, _) = request_json(
+ &state,
+ "POST",
+ "/uf-lenient/_search",
+ json!({"query": {"term": {"stauts": "ok"}}}),
+ )
+ .await;
+ assert_eq!(status, StatusCode::BAD_REQUEST, "the setting enforces");
+
+ let (status, json) = request_json(
+ &state,
+ "POST",
+ "/uf-lenient/_search?strict_unknown_fields=false",
+ json!({"query": {"term": {"stauts": "ok"}}}),
+ )
+ .await;
+ assert_eq!(status, StatusCode::OK, "?false beats the setting: {json}");
+ assert_eq!(json["hits"]["total"]["value"], 0, "{json}");
+ }
+
+ /// The per-index setting — any ONE resolved index marked strict covers
+ /// the whole request, so a heterogeneous alias cannot answer the very
+ /// 0-hit its strict member was configured to refuse.
+ #[tokio::test]
+ async fn the_index_setting_enforces_strict_for_the_whole_request() {
+ let state = test_state();
+ build_index(&state, "uf-set-a").await;
+ build_index(&state, "uf-set-b").await;
+ let (status, body) = request_json(
+ &state,
+ "PUT",
+ "/uf-set-b/_settings",
+ json!({"index.query.strict_unknown_fields": true}),
+ )
+ .await;
+ assert_eq!(status, StatusCode::OK, "{body}");
+
+ // No strict member: the lenient default.
+ let (status, _) = request_json(
+ &state,
+ "POST",
+ "/uf-set-a/_search",
+ json!({"query": {"term": {"stauts": "ok"}}}),
+ )
+ .await;
+ assert_eq!(status, StatusCode::OK);
+
+ // With the strict member in the target set: refused.
+ let (status, json) = request_json(
+ &state,
+ "POST",
+ "/uf-set-a,uf-set-b/_search",
+ json!({"query": {"term": {"stauts": "ok"}}}),
+ )
+ .await;
+ assert_eq!(status, StatusCode::BAD_REQUEST, "{json}");
+ assert!(
+ reason(&json).contains("unknown field `stauts` in term clause"),
+ "the field is named: {json}"
+ );
+ }
+
+ /// The settings spelled the ES-native way (nested under `index`) and set
+ /// at CREATE time — the shape `xerj autoindex` sends — must enforce too.
+ #[tokio::test]
+ async fn a_create_time_nested_setting_enforces() {
+ let state = test_state();
+ let (status, body) = request_json(
+ &state,
+ "PUT",
+ "/uf-created",
+ json!({
+ "settings": {"index": {"query": {"strict_unknown_fields": true}}},
+ "mappings": {"properties": {"ctx": {"type": "text"}}},
+ }),
+ )
+ .await;
+ assert_eq!(status, StatusCode::OK, "{body}");
+
+ let (status, json) = request_json(
+ &state,
+ "POST",
+ "/uf-created/_search",
+ json!({"query": {"term": {"stauts": "ok"}}}),
+ )
+ .await;
+ assert_eq!(status, StatusCode::BAD_REQUEST, "{json}");
+ }
+
+ /// `exists` is exempt on purpose: zero IS the truthful answer to "does
+ /// any document have this field", and heterogeneous-index probes rely on
+ /// it. The hint still explains the zero.
+ #[tokio::test]
+ async fn exists_on_an_unknown_field_stays_a_truthful_zero() {
+ let state = test_state();
+ build_index(&state, "uf-exists").await;
+
+ let (status, json) = request_json(
+ &state,
+ "POST",
+ "/uf-exists/_search?strict_unknown_fields=true",
+ json!({"query": {"exists": {"field": "stauts"}}}),
+ )
+ .await;
+ assert_eq!(status, StatusCode::OK, "{json}");
+ assert_eq!(json["hits"]["total"]["value"], 0, "{json}");
+ }
+
+ /// A `multi_match` that names one real field still searches it — hinted,
+ /// not refused; one that names ONLY unknown fields is refused.
+ #[tokio::test]
+ async fn multi_match_is_refused_only_when_every_named_field_is_unknown() {
+ let state = test_state();
+ build_index(&state, "uf-mm").await;
+
+ let (status, json) = request_json(
+ &state,
+ "POST",
+ "/uf-mm/_search?strict_unknown_fields=true",
+ json!({"query": {"multi_match": {"query": "graph", "fields": ["ctx", "nope"]}}}),
+ )
+ .await;
+ assert_eq!(status, StatusCode::OK, "{json}");
+ assert!(
+ json["hits"]["total"]["value"].as_i64().unwrap_or(0) > 0,
+ "the real half of the multi_match still matched: {json}"
+ );
+
+ let (status, json) = request_json(
+ &state,
+ "POST",
+ "/uf-mm/_search?strict_unknown_fields=true",
+ json!({"query": {"multi_match": {"query": "graph", "fields": ["nope", "titel"]}}}),
+ )
+ .await;
+ assert_eq!(status, StatusCode::BAD_REQUEST, "{json}");
+ assert!(
+ reason(&json).contains("multi_match"),
+ "the refused clause is named: {json}"
+ );
+ }
+
+ /// A wildcard field pattern in `multi_match.fields` is legal ES; it must
+ /// not be REFUSED for matching no literal field name. (Whether the
+ /// engine expands the pattern to matching fields is a separate question
+ /// this test does not pin — today it does not, and that gap is engine
+ /// work, not a false 400.)
+ #[tokio::test]
+ async fn multi_match_wildcard_fields_matching_a_real_field_are_not_refused() {
+ let state = test_state();
+ build_index(&state, "uf-glob").await;
+
+ let (status, json) = request_json(
+ &state,
+ "POST",
+ "/uf-glob/_search?strict_unknown_fields=true",
+ json!({"query": {"multi_match": {"query": "graph", "fields": ["ct*"]}}}),
+ )
+ .await;
+ assert_eq!(
+ status,
+ StatusCode::OK,
+ "a pattern matching a real field must not be refused: {json}"
+ );
+ }
+
+ /// `span_multi` wraps a multi-term query whose inner keys are
+ /// `field`/`value` — the walk must read the wrapped field, not treat
+ /// `field`/`prefix` themselves as field names (the false positive the
+ /// ES-YAML 190_index_prefix_search case surfaced). `span_multi` is not a
+ /// supported query type here, so the parser 400 is what a caller sees
+ /// when the wrapped field is real — the pin is that the strict walk does
+ /// not MASK it with a bogus `unknown field \`field\``. `intervals` IS
+ /// supported: its field is the KEY and its spec keys are interval kinds.
+ #[tokio::test]
+ async fn span_multi_and_intervals_are_not_false_positives() {
+ let state = test_state();
+ build_index(&state, "uf-span").await;
+
+ // Real field wrapped in span_multi: the parser's own refusal (the
+ // query type is unsupported), never an unknown-field error.
+ let (status, json) = request_json(
+ &state,
+ "POST",
+ "/uf-span/_search?strict_unknown_fields=true",
+ json!({"query": {"span_multi": {"match": {"prefix": {"field": "title", "value": "doc"}}}}}),
+ )
+ .await;
+ assert_eq!(status, StatusCode::BAD_REQUEST, "{json}");
+ let r = reason(&json);
+ assert!(
+ r.contains("unknown query type `span_multi`"),
+ "the parser's refusal, not a masked one: {r}"
+ );
+ assert!(
+ !r.contains("unknown field `field`"),
+ "the strict walk must not read span_multi's inner keys as fields: {r}"
+ );
+
+ // Unknown field wrapped in span_multi: the strict walk fires first,
+ // naming the wrapped clause — not the inner keys.
+ let (status, json) = request_json(
+ &state,
+ "POST",
+ "/uf-span/_search?strict_unknown_fields=true",
+ json!({"query": {"span_multi": {"match": {"prefix": {"field": "titel", "value": "doc"}}}}}),
+ )
+ .await;
+ assert_eq!(status, StatusCode::BAD_REQUEST, "{json}");
+ assert!(
+ reason(&json).contains("unknown field `titel` in span_multi prefix clause"),
+ "the wrapped clause is named: {json}"
+ );
+
+ // intervals over a real field: 200, no refusal — the spec's own
+ // `match`/`prefix` keys are interval kinds, not field names.
+ let (status, json) = request_json(
+ &state,
+ "POST",
+ "/uf-span/_search?strict_unknown_fields=true",
+ json!({"query": {"intervals": {"title": {
+ "match": {"query": "doc", "ordered": true}
+ }}}}),
+ )
+ .await;
+ assert_eq!(status, StatusCode::OK, "intervals real field: {json}");
+
+ // intervals over an unknown field: refused.
+ let (status, json) = request_json(
+ &state,
+ "POST",
+ "/uf-span/_search?strict_unknown_fields=true",
+ json!({"query": {"intervals": {"titel": {
+ "match": {"query": "doc", "ordered": true}
+ }}}}),
+ )
+ .await;
+ assert_eq!(status, StatusCode::BAD_REQUEST, "{json}");
+ assert!(
+ reason(&json).contains("unknown field `titel` in intervals clause"),
+ "the intervals field is named: {json}"
+ );
+ }
+
+ /// A just-created index with no mappings yet cannot prove any field
+ /// unknown — dynamic mapping has seen nothing — so it is exempt even in
+ /// strict mode.
+ #[tokio::test]
+ async fn a_fresh_index_with_no_mappings_is_not_validated() {
+ let state = test_state();
+ let (status, body) = request_json(
+ &state,
+ "PUT",
+ "/uf-fresh",
+ json!({"settings": {"index.query.strict_unknown_fields": true}}),
+ )
+ .await;
+ assert_eq!(status, StatusCode::OK, "{body}");
+
+ let (status, json) = request_json(
+ &state,
+ "POST",
+ "/uf-fresh/_search?strict_unknown_fields=true",
+ json!({"query": {"term": {"anything": "at-all"}}}),
+ )
+ .await;
+ assert_eq!(status, StatusCode::OK, "{json}");
+ assert_eq!(json["hits"]["total"]["value"], 0, "{json}");
+ }
+
+ /// A subfield of a real field (`ctx.exact`) and ES metadata (`_id`) are
+ /// not "unknown", exactly as the lenient hint already treated them.
+ #[tokio::test]
+ async fn subfields_of_real_fields_and_metadata_fields_are_known() {
+ let state = test_state();
+ build_index(&state, "uf-sub").await;
+
+ let (status, json) = request_json(
+ &state,
+ "POST",
+ "/uf-sub/_search?strict_unknown_fields=true",
+ json!({"query": {"term": {"ctx.exact": "graph"}}}),
+ )
+ .await;
+ assert_eq!(status, StatusCode::OK, "subfield of a real field: {json}");
+
+ let (status, json) = request_json(
+ &state,
+ "POST",
+ "/uf-sub/_search?strict_unknown_fields=true",
+ json!({"query": {"term": {"_id": "0"}}}),
+ )
+ .await;
+ assert_eq!(status, StatusCode::OK, "metadata field: {json}");
+ }
+
+ /// `_msearch` refuses the offending item alone, ES's per-item error
+ /// shape, and the rest of the batch still runs — against a strict index,
+ /// since NDJSON items carry no URL params.
+ #[tokio::test]
+ async fn msearch_refuses_the_offending_item_and_runs_its_sibling() {
+ let state = test_state();
+ build_index(&state, "uf-ms").await;
+ let (status, body) = request_json(
+ &state,
+ "PUT",
+ "/uf-ms/_settings",
+ json!({"index.query.strict_unknown_fields": true}),
+ )
+ .await;
+ assert_eq!(status, StatusCode::OK, "{body}");
+
+ let response = crate::router::build_es_compat_router(state.clone())
+ .oneshot(
+ Request::builder()
+ .method("POST")
+ .uri("/_msearch")
+ .header("content-type", "application/x-ndjson")
+ .body(Body::from(concat!(
+ "{\"index\":\"uf-ms\"}\n",
+ "{\"query\":{\"term\":{\"stauts\":\"ok\"}}}\n",
+ "{\"index\":\"uf-ms\"}\n",
+ "{\"query\":{\"term\":{\"status\":\"ok\"}}}\n",
+ )))
+ .expect("request"),
+ )
+ .await
+ .expect("route response");
+ let status = response.status();
+ let bytes = to_bytes(response.into_body(), usize::MAX).await.unwrap();
+ let json: Value = serde_json::from_slice(&bytes).expect("msearch json");
+ assert_eq!(status, StatusCode::OK, "{json}");
+ let responses = json["responses"].as_array().expect("responses");
+ assert_eq!(responses.len(), 2, "{json}");
+ assert_eq!(responses[0]["status"], 400, "{json}");
+ assert_eq!(
+ responses[0]["error"]["type"],
+ "search_phase_execution_exception"
+ );
+ assert!(
+ responses[0]["error"]["root_cause"][0]["reason"]
+ .as_str()
+ .unwrap_or_default()
+ .contains("unknown field `stauts` in term clause"),
+ "the item names the field: {json}"
+ );
+ assert_eq!(responses[1]["status"], 200, "{json}");
+ assert!(
+ responses[1]["hits"]["total"]["value"].as_i64().unwrap_or(0) > 0,
+ "the sibling item ran: {json}"
+ );
+ }
+}
+
/// `POST /{index}/_search`.
///
/// Thin wrapper over [`search_impl`] that installs the Painless fault sink for
@@ -9678,6 +10516,28 @@ async fn search_impl(
}
}
+ // ── #1093: refuse lookup clauses naming fields no resolved index maps ──
+ // Opt-in (`?strict_unknown_fields=true`, the `index.query.strict_unknown_fields`
+ // setting, or an autoindex-created index); runs after the PIT filter merge and
+ // `_index`-constraint strip, on the query as it will actually execute. ES answers
+ // these with a silent 0-hit 200; here that made a wrong field name indistinguishable from an
+ // empty corpus (the map-gate run counted 5 silent wrong-field misses in
+ // 30 agent intents). The refusal names the field and both escape hatches;
+ // `exists` is exempt (zero IS the truthful answer there) and fresh
+ // all-dynamic indices are exempt (nothing is mapped yet).
+ if !index_names.is_empty() {
+ if let Some(reason) = unknown_lookup_field_reason(
+ &state,
+ &index_names,
+ params.strict_unknown_fields.as_deref(),
+ body.query.as_ref(),
+ )
+ .await
+ {
+ return search_shard_validation_error(&reason);
+ }
+ }
+
// Track the original knn config for downstream profile emission.
// After we synthesise the inner Knn query and clear body.knn, the
// profile builder still needs to know whether this was a knn search.
@@ -24325,6 +25185,55 @@ async fn msearch_impl(
}
}
+ // #1093, the `_msearch` half of the `_search` refusal: a lookup
+ // clause naming a field no strict target index maps fails THIS item
+ // with the same 400 the single-request path answers with, and the
+ // rest of the batch still runs. Wildcard/unresolvable targets
+ // degrade to the lenient default (nothing is provably unknown);
+ // there is no per-item URL param in NDJSON, so strictness here
+ // comes from the index setting (or `?strict_unknown_fields=true`,
+ // which does not exist per item) — the index setting only.
+ {
+ let mut names: Vec<String> = Vec::new();
+ for part in index_name
+ .split(',')
+ .map(str::trim)
+ .filter(|s| !s.is_empty())
+ {
+ if let Some(entry) = state.engine.aliases.get(part) {
+ for backing in entry.value().iter() {
+ if !names.contains(backing) {
+ names.push(backing.clone());
+ }
+ }
+ } else if !part.contains('*') && part != "_all" && !names.iter().any(|n| n == part)
+ {
+ names.push(part.to_string());
+ }
+ }
+ let name_refs: Vec<&str> = names.iter().map(String::as_str).collect();
+ if !name_refs.is_empty() {
+ if let Some(reason) = unknown_lookup_field_reason(
+ &state,
+ &name_refs,
+ None,
+ effective_body.get("query"),
+ )
+ .await
+ {
+ responses.push(json!({
+ "error": {
+ "root_cause": [{ "type": "illegal_argument_exception", "reason": reason }],
+ "type": "search_phase_execution_exception",
+ "reason": "all shards failed",
+ },
+ "status": 400
+ }));
+ continue;
+ }
+ }
+ }
+
// ES 8.x default: when a sub-search omits `track_total_hits`, the
// total is only tracked up to 10,000 ({"value":10000,"relation":"gte"}
// beyond that) — same default the top-level `_search` handler applies.
diff --git a/engine/crates/xerj-autoindex/src/lib.rs b/engine/crates/xerj-autoindex/src/lib.rs
index 73ae2443c..9c14cf077 100644
--- a/engine/crates/xerj-autoindex/src/lib.rs
+++ b/engine/crates/xerj-autoindex/src/lib.rs
@@ -400,6 +400,22 @@ fn dataset_create_body(dataset: &PlanDataset) -> Value {
"default": {"type": analyzer}
}}});
}
+ // #1093: autoindex PUTs an explicit full schema, so a lookup naming a
+ // field outside it is a mistake, not a dynamic-mapping probe — the exact
+ // case the refusal exists for (5 of 30 agent intents silently searched a
+ // wrong field name). Strict rides HERE, in the create body, not in
+ // `build_mapping`: this value is not hashed into the generation contract
+ // identities, so existing state dirs keep their digests; and
+ // `ensure_index` tolerates `resource_already_exists` without re-sending,
+ // so indices an older build created stay lenient (opt in via the setting).
+ if let Some(settings) = create_body
+ .get_mut("settings")
+ .and_then(Value::as_object_mut)
+ {
+ settings.insert("index.query.strict_unknown_fields".to_string(), json!(true));
+ } else {
+ create_body["settings"] = json!({"index.query.strict_unknown_fields": true});
+ }
create_body
}
@@ -6405,9 +6421,10 @@ fn run_index_report_inner(
let mut refused: Vec<MappingRefusal> = Vec::new();
for d in &plan.datasets {
// The same create body the generated route installs
- // (`dataset_create_body`): frozen mapping, `ax_paths`, and — when the
- // profiler marked this dataset's prose semantic — the #1059 stemming
- // analyzer declaration in `settings.analysis.analyzer.default`.
+ // (`dataset_create_body`): frozen mapping, `ax_paths`, the #1093
+ // strict-unknown-fields mark, and — when the profiler marked this
+ // dataset's prose semantic — the #1059 stemming analyzer declaration
+ // in `settings.analysis.analyzer.default`.
let installed = es
.ensure_index(&d.index, &dataset_create_body(d))
.with_context(|| format!("create index {}", d.index))
@@ -10196,19 +10213,48 @@ mod stem_default_tests {
assert!(body.pointer("/mappings/properties/body/analyzer").is_none());
}
- /// A dataset with no profiler-marked prose sends NO settings at all — the
- /// create body is exactly the frozen mapping plus `ax_paths`, so the
- /// server keeps `standard`'s exact term space for identifier/enum data.
+ /// A dataset with no profiler-marked prose sends no ANALYZER settings —
+ /// the create body's analysis side is empty, so the server keeps
+ /// `standard`'s exact term space for identifier/enum data. The #1093
+ /// strict-mark is the one setting every create body carries (see
+ /// `every_create_body_marks_the_index_strict_on_unknown_fields`).
#[test]
fn a_non_semantic_dataset_sends_no_analyzer_settings() {
let body = dataset_create_body(&dataset("events", None));
- assert!(body.get("settings").is_none(), "{body}");
+ assert!(body.pointer("/settings/analysis").is_none(), "{body}");
assert_eq!(
body.pointer("/mappings/properties/ax_paths"),
Some(&json!({"type": "keyword"}))
);
}
+ /// #1093: every dataset index THIS build creates is strict on unknown
+ /// lookup fields, either spelling of the settings object — the explicit
+ /// full schema makes a wrong field name a mistake, not a probe. It rides
+ /// the create body only (never `build_mapping`), so the frozen identity
+ /// digests do not move.
+ #[test]
+ fn every_create_body_marks_the_index_strict_on_unknown_fields() {
+ let semantic = dataset_create_body(&dataset("prose", Some(infer::STEMMING_TEXT_ANALYZER)));
+ assert_eq!(
+ semantic.pointer("/settings/index.query.strict_unknown_fields"),
+ Some(&json!(true)),
+ "{semantic}"
+ );
+ let plain = dataset_create_body(&dataset("events", None));
+ assert_eq!(
+ plain.pointer("/settings/index.query.strict_unknown_fields"),
+ Some(&json!(true)),
+ "{plain}"
+ );
+ // The analyzer declaration survives beside it.
+ assert_eq!(
+ semantic.pointer("/settings/analysis/analyzer/default/type"),
+ Some(&json!("stemmer")),
+ "{semantic}"
+ );
+ }
+
/// The declaration survives the journal round-trip, because a pending
/// generation replayed after a restart provisions its indexes from the
/// DESERIALIZED plan: an analyzer marker lost to serialization would
@@ -10232,7 +10278,9 @@ mod stem_default_tests {
});
let legacy: PlanDataset = serde_json::from_value(legacy).unwrap();
assert_eq!(legacy.text_analyzer, None);
- assert!(dataset_create_body(&legacy).get("settings").is_none());
+ assert!(dataset_create_body(&legacy)
+ .pointer("/settings/analysis")
+ .is_none());
}
}
diff --git a/landing/llms-full.txt b/landing/llms-full.txt
index 78a2ec239..7b91f7747 100644
--- a/landing/llms-full.txt
+++ b/landing/llms-full.txt
@@ -266,6 +266,25 @@ For a complete disconnected deployment procedure, including current GitHub relea
entry — a ready-to-send corrected request — ahead of `hits` (never inside
`hits`/`total`/`_score`, so existing ES clients are unaffected). See §3.1.
+- Unknown-field lookups: strict on autoindex indices, ES-exact elsewhere. Elasticsearch
+ answers a lookup on an unmapped field with `0 hits` — indistinguishable from an empty
+ corpus, and the measured failure behind this behaviour was 5 of 30 agent intents
+ silently searching a wrong field name. XERJ's wire default is ES's own answer, plus an
+ additive `_xerj.hints` `unknown_field` entry naming what does not exist and what does;
+ but `xerj autoindex` PUTs an explicit full schema per dataset, so the indices it creates
+ (`ax-*` by default) refuse instead: a `term`, `terms`, `match`, `match_phrase`,
+ `match_phrase_prefix`, `prefix`, `wildcard`, `regexp`, `fuzzy`, or `range` naming an
+ unmapped field — or a `multi_match` whose `fields` are ALL unmapped (a half-real one
+ still searches the real fields, with a hint) — answers 400 naming the field and the
+ searchable fields. Turn the refusal on anywhere: `?strict_unknown_fields=true` per
+ request, or the `index.query.strict_unknown_fields=true` setting per index (one strict
+ target covers the whole request); `?strict_unknown_fields=false` opts one request back
+ out, and `_msearch` follows the index setting only, NDJSON items carrying no params.
+ Exemptions either way: `exists` on an unmapped field stays a truthful 0-hit, and a
+ just-created index with no mappings yet is never validated (dynamic mapping has seen
+ no documents). To get real field names, `GET /{index}/_mapping` or the `xerj_map`
+ tool/MCP tool.
+
- The fully-exercised, AI-core surface is: index & mapping management, `_doc` CRUD,
`_bulk`, `_search` (full DSL + `semantic`/`knn`/`hybrid`), `_count`, `_mget`,
`_msearch`, the `/_memory` API, and the `autoindex` CLI. Many cluster / _cat /
diff --git a/landing/llms.txt b/landing/llms.txt
index f751c95db..0d652d7e2 100644
--- a/landing/llms.txt
+++ b/landing/llms.txt
@@ -44,6 +44,7 @@ Then, from an unknown folder to an answered question: `xerj autoindex <folder>`
> - **kNN is HNSW-served (approximate) when unfiltered, exact brute-force otherwise.** An unfiltered `knn` on a full-precision cosine field with ≥1,024 docs runs a beam search over a persisted HNSW graph and exact-rescores its candidates, so `_score`s match the exact path bit-for-bit. `num_candidates` is the beam width (floored at 800; ES's 1.5×k default applies when omitted). On the 50k × 128-d bench corpus: recall@10 1.00 on the official bench query, 100-probe mean 0.976 / min 0.90 (ES 8.13.4, same protocol: 0.937 / 0.70). Recall on the ANN path is **measured, not guaranteed**, and it can return fewer than k hits. Filtered or nested kNN, non-cosine similarity, SQ8-quantized fields, indexes under 1,024 docs, a stale graph, and `semantic` / `_memory` recall all use the exact scan (recall 1.00 by construction; latency scales with vectors scanned).
> - **`rerank` is the only SEARCH-TIME feature that sends document text off the node — and it needs an operator-configured provider.** Adding `"rerank": {…}` to a `_search` body POSTs the question and the text of up to `window` hits (default 30, max 300) to a third-party relevance judge (TypeSafe AI's Jev), which returns a calibrated 0–1 probability per document; that probability replaces `_score` and `rerank.min_score` prunes by it. Two other features send text off the node, both operator configuration and off by default: `[embedding] default_endpoint` / `--embed-mode proxy` (document text at ingest and query text at search time to an external embeddings API) and the WAL tap (`PUT /_xerj/wal_tap`, every write on tapped indices to an external `_bulk` endpoint); a default node contacts neither, and its other outbound connections (the one-time neural model download, cluster Raft to your own peers) carry no document text. `rerank` is opt-in per request, inert until the node's operator sets a key (`[rerank] api_key` or `TYPESAFE_API_KEY` — you cannot pass one in a request), and `[rerank] enabled = false` forbids it. **Do not add `rerank` on data that must stay local unless the user has agreed.** Only fields the response returns are sent, so `_source` filtering also limits what leaves; `rerank.fields` is an exhaustive allow-list. Failure policy is *degrade on deadline, surface on contract*: a slow provider → HTTP 200 with the engine's order and `_rerank.applied:false` (**always read `_rerank.applied`**); no key → `503`, disabled → `403`, rejected key / malformed body / unreachable → `502`, all `rerank_exception` with **no hits** — do not retry, repeat the search without `rerank`. Refused (400), never silently ignored: with `sort`, `search_after`, `collapse`, scroll (open and continuation), `size:0`, and on `_msearch`, search templates, `_async_search`, the native `/v1` search API and gRPC. When applied, every `_score` is a probability or `null` (hits with no verdict, counted in `_rerank.unjudged`, sort last) — never a BM25 value beside probabilities. **Ranking quality IS measured — including this project's own integration defect, published**: with an evaluation key granted by TypeSafe, the stage as it first shipped scored **0.3822 nDCG@10 where BM25 alone scored 0.7750** on a 40-query SciFact pilot, because the request never bound "this document" to a document and the judge scored the whole pile once. Fixed by putting each candidate inside its own noul: **0.8299** on the pilot, and **0.7410 SciFact · 0.3312 NFCorpus · 0.3638 FiQA** on the full run (1,271 judged queries across three public BEIR datasets, BM25 top-30 shortlists, three shuffled repeats, medians; the BM25 arms scored 0.6572 / 0.3016 / 0.2382 — FiQA against BEIR's published 0.236, which validates the harness). Two measured cautions: the provider is non-deterministic (comparisons tighter than ~±0.01 nDCG@10 are noise), and the probabilities **order documents well but are NOT calibrated to BEIR relevance** (ECE 0.10–0.31; worst on FiQA — documents rated ~0.93 confidence were relevant 34% of the time) — never threshold the raw probability with `min_score`. Reference: [docs/RERANK.md](https://github.com/xerj-org/xerj/blob/main/docs/RERANK.md) · write-up with runs and gate: [blog/does-xerj-beat-jev](https://xerj.org/blog/does-xerj-beat-jev).
> - **Hybrid fusion is `rrf` or `linear` ONLY.** `fusion:"learned"` is NOT implemented and fails loudly — `400`, `hybrid fusion learned is not yet supported; use rrf or linear`. Do not depend on it.
+> - **Unknown-field lookups: on `ax-*` (autoindex) indices a wrong field name is a 400, not a silent 0-hit.** Elasticsearch returns `0 hits` for a lookup on an unmapped field, which an agent cannot tell from an empty corpus (measured: 5 of 30 agent intents silently searched a wrong field name). XERJ's wire default is ES's own behaviour — 0 hits — plus an additive `_xerj.hints` `unknown_field` entry naming what does not exist and what does; but the indices `xerj autoindex` creates have full explicit schemas, and there a `term`/`terms`/`match`/`match_phrase`/`match_phrase_prefix`/`prefix`/`wildcard`/`regexp`/`fuzzy`/`range` — or a `multi_match` whose `fields` are ALL unmapped — answers `400` naming the field and the searchable fields (a half-real `multi_match` still searches the real fields, with a hint). Do not guess field names: `GET /{index}/_mapping` or the `xerj_map` tool gives the real ones. Turn the refusal on anywhere with `?strict_unknown_fields=true` per request or `index.query.strict_unknown_fields=true` per index (one strict target covers the whole request); `?strict_unknown_fields=false` opts one request back out, and `_msearch` follows the index setting only (NDJSON items carry no params). Exemptions either way: `exists` on an unmapped field stays a truthful 0 hits, and a just-created index with no mappings yet is never validated.
> - **The DEFAULT embedding mode is lexical, not neural.** With no extra flags, semantic search and memory recall use a deterministic feature-hashing embedder (384-d, L2-normalised → cosine): hybrid lexical+vector retrieval — word / sub-word overlap, not deep semantic understanding or synonym-level recall. For real neural semantics start the server with `--embed-mode neural` — a built-in in-process BERT encoder (all-MiniLM-L6-v2) that ships in the binary and auto-downloads its model (~90 MB) on first use — or `--embed-mode proxy` (any external OpenAI-compatible `/v1/embeddings`). Only describe output as neural when one of those is actually running.
> * **`--embed-mode` is a server-start flag that applies AT INGEST.** Text is embedded as it is indexed, so the mode in force when a document was written is the mode that document keeps. Switching lexical → neural does **not** re-embed anything already indexed: you have to re-index every corpus you care about. **Decide before you index your first corpus.**
> * **What the neural download actually is:** the model files (`config.json`, `tokenizer.json`, `model.safetensors`) are fetched over HTTPS from the **HuggingFace Hub** (`sentence-transformers/all-MiniLM-L6-v2`) into `~/.cache/huggingface` on first use, then reused. **XERJ pins no checksum of its own** for them — the integrity guarantee is TLS plus the Hub, not a digest XERJ verifies. Air-gapped or policy-constrained: pre-download the three files, point `embedding.local_model_dir` at the folder, and nothing is fetched.