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(es-compat): migrate _cat/segments onto the shared alias resolver (#451 class B)

xerj-org3933233e7b18
diff --git a/engine/crates/xerj-api/src/es_compat.rs b/engine/crates/xerj-api/src/es_compat.rs
index c1dfe013b..844fea195 100644
--- a/engine/crates/xerj-api/src/es_compat.rs
+++ b/engine/crates/xerj-api/src/es_compat.rs
@@ -29239,29 +29239,25 @@ pub async fn cat_segments(
// index shard prirep ip segment generation docs.count docs.deleted size size.memory committed searchable version compound
let node = state.engine.node_id.as_str();
let mut rows: Vec<(String, u64, u64)> = Vec::new();
- let indices_to_list: Vec<String> = if index == "_all" || index == "*" {
- state
- .engine
- .list_indices()
- .await
- .into_iter()
- .map(|i| i.name)
- .collect()
- } else {
- vec![index.clone()]
- };
- for idx_name in &indices_to_list {
- if let Ok(idx) = state.engine.get_index(idx_name) {
- let stats = idx.stats().await;
- // Real on-disk size: recursive byte sum of the index's data_dir.
- let size = dir_size_bytes(idx.data_dir());
- // Represent the index's durable data as one logical segment.
- // generation 0; committed + searchable are true (data is queryable
- // and persisted). We do NOT fabricate a Lucene version string —
- // xerj has no Lucene segments — so we report the xerj build version.
- let _ = node; // segments output has no node column in ES; kept for parity
- rows.push((idx_name.clone(), stats.doc_count, size));
- }
+ // Resolve the selector through the shared resolver (#451): an alias expands
+ // to every member (`get_index` collapsed it to `aliased.first()`, so an
+ // alias listed a single row under the alias name), and `_all` / `*` / glob /
+ // comma forms resolve uniformly. A cat endpoint lists what exists rather
+ // than 404ing, so an unknown selector is an empty listing.
+ let members = resolve_selector_with_filters(&state, &index)
+ .await
+ .map(|(m, _filters)| m)
+ .unwrap_or_default();
+ for (idx_name, idx) in &members {
+ let stats = idx.stats().await;
+ // Real on-disk size: recursive byte sum of the index's data_dir.
+ let size = dir_size_bytes(idx.data_dir());
+ // Represent the index's durable data as one logical segment.
+ // generation 0; committed + searchable are true (data is queryable
+ // and persisted). We do NOT fabricate a Lucene version string —
+ // xerj has no Lucene segments — so we report the xerj build version.
+ let _ = node; // segments output has no node column in ES; kept for parity
+ rows.push((idx_name.clone(), stats.doc_count, size));
}
if params.format.as_deref() == Some("json") {
diff --git a/engine/crates/xerj-api/tests/cat_segments_through_alias_lists_every_member.rs b/engine/crates/xerj-api/tests/cat_segments_through_alias_lists_every_member.rs
new file mode 100644
index 000000000..5c01cdff4
--- /dev/null
+++ b/engine/crates/xerj-api/tests/cat_segments_through_alias_lists_every_member.rs
@@ -0,0 +1,101 @@
+//! Issue #451 (failure class B): `GET /_cat/segments/{index}` listed only
+//! `[index]` and resolved it with `Engine::get_index`, which collapses an alias
+//! to `aliased.first()`. So `_cat/segments` over a multi-index alias reported a
+//! single row (the alias name, the first member's stats) instead of one row per
+//! member — and a glob selector matched nothing.
+//!
+//! Migrated onto the shared `resolve_selector_with_filters` resolver (#451),
+//! which expands `_all` / `*` / globs / aliases / comma-lists uniformly.
+//!
+//! 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 call(app: &axum::Router, method: &str, path: &str, body: Value) -> (StatusCode, Value) {
+ let mut req = Request::builder().method(method).uri(path);
+ let body = if body.is_null() {
+ Body::empty()
+ } else {
+ req = req.header("content-type", "application/json");
+ Body::from(body.to_string())
+ };
+ let response = app.clone().oneshot(req.body(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),
+ )
+}
+
+/// `_cat/segments` over a 2-member alias must return a row per member, naming
+/// the concrete members — not one row under the alias name.
+#[tokio::test]
+async fn cat_segments_through_a_multi_index_alias_lists_every_member() {
+ let (app, _dir) = app().await;
+
+ for (member, id) in [("idx-a", "1"), ("idx-b", "2")] {
+ let (st, _) = call(
+ &app,
+ "PUT",
+ &format!("/{member}"),
+ json!({"mappings": {"properties": {"v": {"type": "keyword"}}}}),
+ )
+ .await;
+ assert_eq!(st, StatusCode::OK, "create {member}");
+ let (st, _) = call(
+ &app,
+ "PUT",
+ &format!("/{member}/_doc/{id}"),
+ json!({"v": member}),
+ )
+ .await;
+ assert_eq!(st, StatusCode::CREATED, "index doc {id} into {member}");
+ let (st, _) = call(&app, "POST", &format!("/{member}/_refresh"), Value::Null).await;
+ assert_eq!(st, StatusCode::OK, "refresh {member}");
+ }
+ let (st, _) = call(
+ &app,
+ "POST",
+ "/_aliases",
+ json!({"actions": [
+ {"add": {"index": "idx-a", "alias": "tri"}},
+ {"add": {"index": "idx-b", "alias": "tri"}}
+ ]}),
+ )
+ .await;
+ assert_eq!(st, StatusCode::OK, "create alias tri");
+
+ let (status, body) = call(&app, "GET", "/_cat/segments/tri?format=json", Value::Null).await;
+ assert_eq!(status, StatusCode::OK, "_cat/segments status: {body}");
+
+ let rows = body.as_array().cloned().unwrap_or_default();
+ let mut indices: Vec<String> = rows
+ .iter()
+ .filter_map(|r| r.get("index").and_then(Value::as_str).map(String::from))
+ .collect();
+ indices.sort();
+ assert_eq!(
+ indices,
+ vec!["idx-a".to_string(), "idx-b".to_string()],
+ "_cat/segments through the alias must list a row per concrete member, not one \
+ row under the alias name (#451): {body}"
+ );
+}