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
fix(api): expand wildcard indices in _msearch (#1932) (#1939)
xerj-org77336dc6d8ca
diff --git a/engine/crates/xerj-api/src/es_compat.rs b/engine/crates/xerj-api/src/es_compat.rsindex d8ddc50dc..35c8fb061 100644--- a/engine/crates/xerj-api/src/es_compat.rs+++ b/engine/crates/xerj-api/src/es_compat.rs@@ -26528,13 +26528,13 @@ async fn msearch_impl(// Resolve index names (comma-separated or "*" = all).let index_names: Vec<String> = if let Some(pit) = pit_context.as_ref() {pit.indices.clone()- } else if index_name == "*" || index_name == "_all" {- state.engine.index_name_list()} else {- index_name- .split(',')- .flat_map(|n| state.engine.resolve_alias(n.trim()))- .collect()+ // #1932: expand wildcards and aliases through the same selector+ // the other read endpoints use. `resolve_alias` knew aliases+ // only, so `logs-*` reached `get_index` as a literal name and the+ // item answered 404. A pattern that matches nothing is an empty+ // result, and a missing concrete name still fails the item.+ resolve_index_selector(&state, &index_name).await};let index_names: Vec<String> = if idx_constraints.is_empty() {diff --git a/engine/crates/xerj-api/tests/msearch_wildcard_index.rs b/engine/crates/xerj-api/tests/msearch_wildcard_index.rsnew file mode 100644index 000000000..7ff81073b--- /dev/null+++ b/engine/crates/xerj-api/tests/msearch_wildcard_index.rs@@ -0,0 +1,139 @@+//! Issue #1932: `_msearch` did not expand a wildcard index. A header+//! `{"index":"logs-*"}` or the path form `/logs-*/_msearch` answered the item+//! with 404 `index_not_found_exception` for the literal pattern, while+//! `_search` on the same pattern found the documents. The item now resolves+//! its target through the same selector the other read endpoints use.+//!+//! Elasticsearch is referenced for wire semantics only; no ES code is+//! reproduced here.++use axum::body::Body;+use axum::http::{Request, StatusCode};+use serde_json::{json, Value};+use tower::ServiceExt;++async fn app() -> (axum::Router, tempfile::TempDir) {+ let dir = tempfile::tempdir().expect("tempdir");+ let mut config = xerj_common::config::Config::default();+ config.server.data_dir = dir.path().to_string_lossy().into_owned();+ config.storage.wal_sync = xerj_common::config::WalSync::Async;+ let metrics = xerj_common::metrics::Metrics::new().expect("metrics");+ let engine = xerj_engine::Engine::new(config.clone()).expect("engine");+ let state = xerj_api::state::AppState::new(config, engine, metrics);+ (xerj_api::router::build_es_compat_router(state), dir)+}++async fn send(+ app: &axum::Router,+ method: &str,+ path: &str,+ ctype: &str,+ body: String,+) -> (StatusCode, Value) {+ let mut req = Request::builder().method(method).uri(path);+ if !body.is_empty() {+ req = req.header("content-type", ctype);+ }+ let response = app+ .clone()+ .oneshot(req.body(Body::from(body)).unwrap())+ .await+ .unwrap();+ let status = response.status();+ let bytes = axum::body::to_bytes(response.into_body(), usize::MAX)+ .await+ .unwrap();+ (+ status,+ serde_json::from_slice(&bytes).unwrap_or(Value::Null),+ )+}++fn hit_indices(item: &Value) -> Vec<String> {+ let mut out: Vec<String> = item["hits"]["hits"]+ .as_array()+ .unwrap_or(&Vec::new())+ .iter()+ .filter_map(|h| h["_index"].as_str().map(str::to_string))+ .collect();+ out.sort();+ out+}++#[tokio::test]+async fn msearch_expands_a_wildcard_index_in_the_header_and_the_path() {+ let (app, _dir) = app().await;+ for index in ["logs-app", "logs-web", "metrics-app"] {+ let (st, resp) = send(+ &app,+ "PUT",+ &format!("/{index}"),+ "application/json",+ json!({"mappings": {"properties": {"msg": {"type": "text"}}}}).to_string(),+ )+ .await;+ assert_eq!(st, StatusCode::OK, "create {index}: {resp}");+ let (st, resp) = send(+ &app,+ "PUT",+ &format!("/{index}/_doc/1?refresh=true"),+ "application/json",+ json!({"msg": format!("disk full on {index}")}).to_string(),+ )+ .await;+ assert_eq!(st, StatusCode::CREATED, "index into {index}: {resp}");+ }++ let query = r#"{"query":{"match":{"msg":"disk"}}}"#;+ let cases = [+ ("/_msearch", format!("{{\"index\":\"logs-*\"}}\n{query}\n")),+ ("/logs-*/_msearch", format!("{{}}\n{query}\n")),+ (+ "/_msearch",+ format!("{{\"index\":\"logs-*,metrics-app\"}}\n{query}\n"),+ ),+ ];+ let expected: [&[&str]; 3] = [+ &["logs-app", "logs-web"],+ &["logs-app", "logs-web"],+ &["logs-app", "logs-web", "metrics-app"],+ ];+ for ((path, body), want) in cases.into_iter().zip(expected) {+ let (st, resp) = send(&app, "POST", path, "application/x-ndjson", body.clone()).await;+ assert_eq!(st, StatusCode::OK, "{path} {body}: {resp}");+ let item = &resp["responses"][0];+ assert_eq!(item["status"], 200, "{path} {body}: {resp}");+ assert_eq!(hit_indices(item), want, "{path} {body}: {resp}");+ assert_eq!(+ item["hits"]["total"]["value"],+ want.len(),+ "{path} {body}: {resp}"+ );+ }++ // A pattern that matches nothing is an empty result, as on `_search`,+ // not a 404 for the pattern.+ let (st, resp) = send(+ &app,+ "POST",+ "/_msearch",+ "application/x-ndjson",+ format!("{{\"index\":\"nomatch-*\"}}\n{query}\n"),+ )+ .await;+ assert_eq!(st, StatusCode::OK, "{resp}");+ let item = &resp["responses"][0];+ assert_eq!(item["status"], 200, "{resp}");+ assert_eq!(item["hits"]["total"]["value"], 0, "{resp}");++ // A missing concrete name still fails that item with 404.+ let (_, resp) = send(+ &app,+ "POST",+ "/_msearch",+ "application/x-ndjson",+ format!("{{\"index\":\"missing\"}}\n{query}\n"),+ )+ .await;+ assert_eq!(resp["responses"][0]["status"], 404, "{resp}");+}