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): add GET /_cat/templates/{pattern} (fixes real OSD 404 crash)

Vincenzo Lombardo5a822b3e9c50
diff --git a/engine/crates/xerj-api/src/es_compat.rs b/engine/crates/xerj-api/src/es_compat.rs
index 2d0a2120e..fdf61b88b 100644
--- a/engine/crates/xerj-api/src/es_compat.rs
+++ b/engine/crates/xerj-api/src/es_compat.rs
@@ -19089,13 +19089,94 @@ pub async fn head_index(
// GET /_cat/templates — list templates in text format
// ─────────────────────────────────────────────────────────────────────────────
-pub async fn cat_templates(State(state): State<AppState>) -> impl IntoResponse {
- // name index_patterns order version
+/// Query params for `/_cat/templates` (and its `{pattern}` variant).
+/// `format=json` returns a JSON array instead of the plain-text table —
+/// OpenSearch Dashboards' own `cat.templates()` call requests this.
+#[derive(Debug, Deserialize, Default)]
+pub struct CatTemplatesParams {
+ #[serde(default)]
+ pub format: Option<String>,
+}
+
+pub async fn cat_templates(
+ State(state): State<AppState>,
+ Query(params): Query<CatTemplatesParams>,
+) -> impl IntoResponse {
+ cat_templates_inner(state, None, params).await
+}
+
+/// `GET /_cat/templates/{pattern}` — the pattern-scoped variant.
+///
+/// Previously unrouted, so a wildcard-pattern call (e.g. OpenSearch
+/// Dashboards' own startup check for `opensearch_dashboards_index_template*`)
+/// fell through to the router's bare 404 fallback: no body at all, not even
+/// an ES-shaped JSON error. `opensearch-js`'s `cat.templates()` can't parse
+/// an empty body and throws — found empirically, this is what actually
+/// crashes OSD's saved-objects migration, not the (perfectly well-formed)
+/// `.kibana` 404 it hits right before this call.
+///
+/// Same `_cat` selector semantics as `/_cat/indices/{pattern}`: a WILDCARD
+/// matching nothing is an empty 200, never a 404 — a concrete template name
+/// matching nothing does 404, but templates have no equivalent of `_all`
+/// index-not-found semantics worth enforcing here since nothing currently
+/// creates a naming collision risk.
+pub async fn cat_templates_pattern(
+ State(state): State<AppState>,
+ Path(pattern): Path<String>,
+ Query(params): Query<CatTemplatesParams>,
+) -> impl IntoResponse {
+ cat_templates_inner(state, Some(pattern), params).await
+}
+
+async fn cat_templates_inner(
+ state: AppState,
+ pattern: Option<String>,
+ params: CatTemplatesParams,
+) -> axum::response::Response {
+ let matches_pattern = |name: &str| match pattern.as_deref() {
+ None => true,
+ Some(pat) => pat
+ .split(',')
+ .map(str::trim)
+ .filter(|s| !s.is_empty())
+ .any(|p| p == "_all" || p == "*" || p == name || glob_match_simple(p, name)),
+ };
+
+ let mut rows: Vec<(String, Vec<String>, i32)> = state
+ .engine
+ .templates
+ .iter()
+ .filter(|entry| matches_pattern(entry.key()))
+ .map(|entry| {
+ let t = entry.value();
+ (entry.key().clone(), t.index_patterns.clone(), t.priority)
+ })
+ .collect();
+ rows.sort_by(|a, b| a.0.cmp(&b.0));
+
+ if params.format.as_deref() == Some("json") {
+ let arr: Vec<Value> = rows
+ .iter()
+ .map(|(name, index_patterns, priority)| {
+ json!({
+ "name": name,
+ "index_patterns": format!("[{}]", index_patterns.join(", ")),
+ "order": priority.to_string(),
+ "version": "",
+ })
+ })
+ .collect();
+ return Json(arr).into_response();
+ }
+
let mut lines: Vec<String> = Vec::new();
- for entry in state.engine.templates.iter() {
- let t = entry.value();
- let patterns = t.index_patterns.join(",");
- lines.push(format!("{} {} {} -", entry.key(), patterns, t.priority));
+ for (name, index_patterns, priority) in &rows {
+ lines.push(format!(
+ "{} {} {} -",
+ name,
+ index_patterns.join(","),
+ priority
+ ));
}
let body = if lines.is_empty() {
String::new()
diff --git a/engine/crates/xerj-api/src/router.rs b/engine/crates/xerj-api/src/router.rs
index f2fa9848f..25fa3c873 100644
--- a/engine/crates/xerj-api/src/router.rs
+++ b/engine/crates/xerj-api/src/router.rs
@@ -372,6 +372,10 @@ pub fn build_es_compat_router(state: AppState) -> Router {
.route("/_tasks/:task_id/_cancel", post(es_compat::cancel_task))
// ── Cat templates ──────────────────────────────────────────────────
.route("/_cat/templates", get(es_compat::cat_templates))
+ .route(
+ "/_cat/templates/:pattern",
+ get(es_compat::cat_templates_pattern),
+ )
// ── Ingest pipelines ───────────────────────────────────────────────
.route(
"/_ingest/pipeline",