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.
Rust agent-memoryai-agentsbm25claude-codeclicode-searchcursordatabaseelasticsearchfull-text-searchhybrid-searchjsonllocal-firstmcpmcp-servermodel-context-protocolragrustsearch-enginevector-search
feat(dx): a --port flag, and tell callers when a query names a field that does not exist
bugeref5bf4ad0759
diff --git a/engine/crates/xerj-api/src/es_compat.rs b/engine/crates/xerj-api/src/es_compat.rsindex 6fba08bdf..da565cce3 100644--- a/engine/crates/xerj-api/src/es_compat.rs+++ b/engine/crates/xerj-api/src/es_compat.rs@@ -6973,6 +6973,139 @@ 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 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",+ ];+ match q {+ Value::Object(o) => {+ for (k, v) in o {+ if FIELD_KEYED.contains(&k.as_str()) {+ if let Value::Object(inner) = v {+ for f in inner.keys() {+ out.push(f.clone());+ }+ }+ } else if k == "exists" {+ if let Some(f) = v.get("field").and_then(Value::as_str) {+ out.push(f.to_string());+ }+ } else if k == "multi_match" {+ if let Some(Value::Array(fs)) = v.get("fields") {+ for f in fs.iter().filter_map(Value::as_str) {+ // `body^3` — the boost is not part of the name.+ out.push(f.split('^').next().unwrap_or(f).to_string());+ }+ }+ } else {+ referenced_query_fields(v, out);+ }+ }+ }+ Value::Array(a) => a.iter().for_each(|v| referenced_query_fields(v, out)),+ _ => {}+ }+}++/// Hint for a query that names a field the index does not have.+///+/// Fires regardless of hit count: a bogus field inside a `multi_match` that+/// also names a real one still returns results, so a zero-hit-only check would+/// never see the worst case.+///+/// This is the most expensive silent failure a newcomer hits. In blind+/// usability runs, two independent agents both guessed `content` (a reasonable+/// name that does not exist here), got `0 hits` with no explanation, and then+/// spent their largest payloads — 182 KB and 65 KB in one run — trial-and-+/// erroring field names before thinking to fetch `_mapping`. The engine knows+/// the schema; staying silent is a choice, and the wrong one.+fn unknown_field_hint(index: &str, body: &EsSearchBody, schema: &xerj_common::types::Schema) -> Option<Value> {+ let query = body.query.as_ref()?;+ let mut referenced = Vec::new();+ referenced_query_fields(query, &mut referenced);+ if referenced.is_empty() {+ return None;+ }++ let known = |name: &str| -> bool {+ // Accept the field itself, a subfield of a real field, and the ES+ // metadata fields a query may legitimately target.+ schema.field(name).is_some()+ || name.starts_with('_')+ || name+ .split_once('.')+ .is_some_and(|(parent, _)| schema.field(parent).is_some())+ };++ let unknown: Vec<String> = referenced+ .iter()+ .filter(|f| !known(f))+ .cloned()+ .collect::<std::collections::BTreeSet<_>>()+ .into_iter()+ .collect();+ if unknown.is_empty() {+ return None;+ }++ // 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 primary = searchable+ .iter()+ .find(|n| n.as_str() == "body")+ .or_else(|| searchable.first())+ .cloned()+ .unwrap_or_else(|| "body".to_string());++ Some(json!({+ "hints": [{+ "code": "unknown_field",+ "reason": format!(+ "no results, and this query names {} that {} not exist in `{index}`: {}. \+ Searchable text fields here are: {}.",+ if unknown.len() == 1 { "a field" } else { "fields" },+ if unknown.len() == 1 { "does" } else { "do" },+ unknown.join(", "),+ if searchable.is_empty() { "(none)".to_string() } else { searchable.join(", ") },+ ),+ "try": {+ "request": format!("POST /{index}/_search"),+ "body": {+ "query": {"match": {primary.clone(): "<your text>"}},+ "_source": ["ax_path", "title"],+ "fields": ["_passage"],+ },+ },+ }],+ }))+}+/// Hint attached to an oversized `GET /{index}/_doc/{id}` response.////// A document fetch cannot be narrowed the way a search can, so the recovery is@@ -12597,7 +12730,31 @@ async fn search_impl(}// Computed before `hits` is moved into `response_body` below.- let response_hint = search_response_hint(&index, &body, &hits);+ // Two independent diagnostics, merged into one `hints` array rather than+ // one winning: a query can both name a nonexistent field AND return an+ // oversized payload, and reporting only the first would hide the other.+ //+ // The unknown-field check runs whether or not there were hits. A bogus+ // field inside a `multi_match` that also names a real one is the worst+ // shape of all — it returns plenty of results, silently searching fewer+ // fields than asked, so the caller has no signal at all.+ let mut all_hints: Vec<Value> = Vec::new();+ if let Some(first) = index_names.first() {+ if let Ok(idx) = state.engine.get_index(first) {+ let schema = idx.schema().await;+ if let Some(h) = unknown_field_hint(&index, &body, &schema) {+ if let Some(Value::Array(items)) = h.get("hints") {+ all_hints.extend(items.iter().cloned());+ }+ }+ }+ }+ if let Some(h) = search_response_hint(&index, &body, &hits) {+ if let Some(Value::Array(items)) = h.get("hints") {+ all_hints.extend(items.iter().cloned());+ }+ }+ let response_hint = (!all_hints.is_empty()).then(|| json!({ "hints": all_hints }));let mut response_body = if track_total_disabled {if want_int_total {diff --git a/engine/crates/xerj-server/src/main.rs b/engine/crates/xerj-server/src/main.rsindex 1ed2cd263..d97b5a62d 100644--- a/engine/crates/xerj-server/src/main.rs+++ b/engine/crates/xerj-server/src/main.rs@@ -107,6 +107,7 @@ struct CliArgs {config: Option<PathBuf>,data_dir: Option<String>,bind: Option<String>,+ port: Option<String>,insecure: bool,embed_mode: Option<String>,onnx_model: Option<String>,@@ -120,6 +121,7 @@ fn parse_args() -> CliArgs {let mut config = None;let mut data_dir = None;let mut bind = None;+ let mut port: Option<String> = None;let mut insecure = false;let mut embed_mode = None;let mut onnx_model = None;@@ -138,6 +140,9 @@ fn parse_args() -> CliArgs {"--bind" | "-b" => {bind = args.next();}+ "--port" => {+ port = args.next();+ }"--insecure" | "-k" => {insecure = true;}@@ -167,6 +172,7 @@ fn parse_args() -> CliArgs {config,data_dir,bind,+ port,insecure,embed_mode,onnx_model,@@ -235,6 +241,10 @@ fn print_help() {client library versions aren't a reliable stand-in for a\n\compatible server version, confirmed against a real OSD\n\container). Example: --compat-version 2.11.0\n\+ --port <PORT> Port for the Elasticsearch-compatible API (default 9200).\n\+ Also claims PORT+1 for the native REST API and PORT+2\n\+ for gRPC, so a second instance needs only this one flag:\n\+ xerj --insecure --port 9300 -d ./other-data\n\--help, -h Show this help\n\--version, -V Print version and exit\n\\n\@@ -478,6 +488,33 @@ fn load_config(args: &CliArgs) -> Result<Config> {cfg.server.bind_address = addr;}+ // Running a second instance used to require hand-writing a TOML file, and+ // the three port keys are not mentioned in `--help`. In a blind usability+ // run an agent tried `-b 127.0.0.1:9310`, got "not an IP address", and+ // ended up running `strings` on the binary to find the config key names.+ // One flag removes that entirely. The sibling ports are derived so a second+ // instance does not collide on them either; set them individually in a+ // config file when that matters.+ if let Some(p) = args.port.clone() {+ let base: u16 = p.parse().map_err(|_| {+ anyhow::anyhow!("--port must be a number between 1 and 65533, got {p:?}")+ })?;+ if base == 0 || base > 65_533 {+ anyhow::bail!(+ "--port must be between 1 and 65533 (it also claims PORT+1 for the \+ native REST API and PORT+2 for gRPC), got {base}"+ );+ }+ info!(+ "server.es_compat_port = {base}, rest_port = {}, grpc_port = {} (from --port)",+ base + 1,+ base + 2+ );+ cfg.server.es_compat_port = base;+ cfg.server.rest_port = base + 1;+ cfg.server.grpc_port = base + 2;+ }+// The #228 opt-out, also settable from the environment so a container can// declare its exposure without shipping a config file. Parsed strictly: a// value we cannot read is refused rather than treated as `false`, because@@ -1111,6 +1148,8 @@ async fn run_cli_index(cmd: IndexCmdArgs) -> Result<()> {// Load config but override to a minimal in-process shape.let fake_cli = CliArgs {config: cmd.config.clone(),+ // No listener is bound for in-process indexing, so no port applies.+ port: None,data_dir: cmd.data_dir.clone(),// In-process CLI indexing binds no listener at all, so the bind// address is irrelevant here — take whatever the config says.