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.

feat(es-compat): index-scoped _msearch and _msearch/template routes

xerj-org970320ec2e3a
diff --git a/engine/crates/xerj-api/src/es_compat.rs b/engine/crates/xerj-api/src/es_compat.rs
index 6dea20aa4..26e95a99b 100644
--- a/engine/crates/xerj-api/src/es_compat.rs
+++ b/engine/crates/xerj-api/src/es_compat.rs
@@ -11904,6 +11904,24 @@ fn native_type_to_es_str(ft: &FieldType) -> &'static str {
// ─────────────────────────────────────────────────────────────────────────────
pub async fn msearch(State(state): State<AppState>, body: bytes::Bytes) -> impl IntoResponse {
+ msearch_impl(state, body, None).await
+}
+
+/// GET|POST /{index}/_msearch — index-scoped multi-search. The path index is
+/// the default for any header line that omits `index`.
+pub async fn msearch_index(
+ State(state): State<AppState>,
+ Path(index): Path<String>,
+ body: bytes::Bytes,
+) -> impl IntoResponse {
+ msearch_impl(state, body, Some(index)).await
+}
+
+async fn msearch_impl(
+ state: AppState,
+ body: bytes::Bytes,
+ default_index: Option<String>,
+) -> axum::response::Response {
let text = match std::str::from_utf8(&body) {
Ok(t) => t,
Err(_) => {
@@ -11949,12 +11967,14 @@ pub async fn msearch(State(state): State<AppState>, body: bytes::Bytes) -> impl
}
};
- // Determine index name from header or fall back to "*".
+ // Determine index name: header wins, then the path index (for
+ // /{index}/_msearch), then "*" (all indices).
let index_name = header
.get("index")
.and_then(Value::as_str)
- .unwrap_or("*")
- .to_string();
+ .map(str::to_string)
+ .or_else(|| default_index.clone())
+ .unwrap_or_else(|| "*".to_string());
let started = Instant::now();
@@ -16962,6 +16982,24 @@ pub async fn msearch_template(
State(state): State<AppState>,
body: bytes::Bytes,
) -> impl IntoResponse {
+ msearch_template_impl(state, body, None).await
+}
+
+/// GET|POST /{index}/_msearch/template — index-scoped multi-search template.
+/// The path index is the default for any header line that omits `index`.
+pub async fn msearch_template_index(
+ State(state): State<AppState>,
+ Path(index): Path<String>,
+ body: bytes::Bytes,
+) -> impl IntoResponse {
+ msearch_template_impl(state, body, Some(index)).await
+}
+
+async fn msearch_template_impl(
+ state: AppState,
+ body: bytes::Bytes,
+ default_index: Option<String>,
+) -> axum::response::Response {
let text = match std::str::from_utf8(&body) {
Ok(t) => t,
Err(_) => return Json(json!({ "error": "body is not valid UTF-8" })).into_response(),
@@ -16997,11 +17035,14 @@ pub async fn msearch_template(
}
};
+ // Header wins, then the path index (for /{index}/_msearch/template),
+ // then "*" (all indices).
let index_name = header
.get("index")
.and_then(Value::as_str)
- .unwrap_or("*")
- .to_string();
+ .map(str::to_string)
+ .or_else(|| default_index.clone())
+ .unwrap_or_else(|| "*".to_string());
let params = tmpl_body.params.unwrap_or_default();
let search_body_val: Value = if let Some(source_val) = tmpl_body.source {
diff --git a/engine/crates/xerj-api/src/router.rs b/engine/crates/xerj-api/src/router.rs
index 10e0fb3c4..da9be4051 100644
--- a/engine/crates/xerj-api/src/router.rs
+++ b/engine/crates/xerj-api/src/router.rs
@@ -294,6 +294,12 @@ pub fn build_es_compat_router(state: AppState) -> Router {
)
// ── Multi-search ───────────────────────────────────────────────────
.route("/_msearch", post(es_compat::msearch))
+ // Index-scoped multi-search — the path index defaults header lines
+ // that omit `index`.
+ .route(
+ "/:index/_msearch",
+ get(es_compat::msearch_index).post(es_compat::msearch_index),
+ )
// ── Resolve index (wildcards) ──────────────────────────────────────
.route("/_resolve/index/:name", get(es_compat::resolve_index))
// ── Node & cluster stats ───────────────────────────────────────────
@@ -446,6 +452,12 @@ pub fn build_es_compat_router(state: AppState) -> Router {
.route("/:index/_search/template", post(es_compat::search_template))
// ── Multi-search template ────────────────────────────────────────────
.route("/_msearch/template", post(es_compat::msearch_template))
+ // Index-scoped multi-search template — the path index defaults header
+ // lines that omit `index`.
+ .route(
+ "/:index/_msearch/template",
+ get(es_compat::msearch_template_index).post(es_compat::msearch_template_index),
+ )
// ── Render template ──────────────────────────────────────────────────
.route("/_render/template", post(es_compat::render_template_api))
// ── Stored scripts/templates ─────────────────────────────────────────