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(ingest): auto-apply index.default_pipeline on single-doc writes

xerj-orgb05f15022412
diff --git a/engine/crates/xerj-api/src/es_compat.rs b/engine/crates/xerj-api/src/es_compat.rs
index 72b4e3d95..8328f60e9 100644
--- a/engine/crates/xerj-api/src/es_compat.rs
+++ b/engine/crates/xerj-api/src/es_compat.rs
@@ -1445,10 +1445,41 @@ pub async fn get_settings(
pub struct IndexDocAutoParams {
/// `refresh=true|wait_for` — accepted without error; memtable is always visible.
pub refresh: Option<String>,
- /// `pipeline=my_pipeline` — accepted and logged; pipeline execution not supported.
+ /// `pipeline=my_pipeline` — ingest pipeline to run before indexing.
+ /// `_none` disables the index's `default_pipeline`; absent falls back to it.
pub pipeline: Option<String>,
}
+/// Resolve the effective ingest pipeline for a single-doc write.
+///
+/// Mirrors Elasticsearch `index.default_pipeline` semantics:
+/// - `Some("_none")` → `None` (explicit opt-out; no pipeline runs).
+/// - `Some(name)` → that pipeline (an explicit `?pipeline=` always wins).
+/// - `None` → the index's `index.default_pipeline` setting, if any.
+///
+/// The default is read from `engine.index_settings`, handling the nested
+/// `{index: {default_pipeline: ..}}`, dotted `{"index.default_pipeline": ..}`,
+/// and flat `{default_pipeline: ..}` shapes — mirroring the `number_of_shards`
+/// lookup pattern used elsewhere in this module.
+fn resolve_effective_pipeline(
+ state: &AppState,
+ index: &str,
+ explicit: Option<&str>,
+) -> Option<String> {
+ match explicit {
+ Some("_none") => None,
+ Some(p) => Some(p.to_string()),
+ None => state.engine.index_settings.get(index).and_then(|v| {
+ v.get("index")
+ .and_then(|ix| ix.get("default_pipeline"))
+ .or_else(|| v.get("index.default_pipeline"))
+ .or_else(|| v.get("default_pipeline"))
+ .and_then(Value::as_str)
+ .map(|s| s.to_string())
+ }),
+ }
+}
+
/// Fast path for `POST /{index}/_doc`: receives raw bytes to skip the axum
/// JSON extractor's extra validation pass and parse directly with simd_json /
/// serde_json. No schema validation is performed here; the engine layer handles
@@ -1507,8 +1538,10 @@ pub async fn index_doc_auto(
d
};
- // Execute ingest pipeline if specified.
- let doc = if let Some(ref pipeline) = params.pipeline {
+ // Execute ingest pipeline if one is specified (explicit `?pipeline=`) or
+ // resolved from the index's `index.default_pipeline` setting.
+ let effective_pipeline = resolve_effective_pipeline(&state, &index, params.pipeline.as_deref());
+ let doc = if let Some(ref pipeline) = effective_pipeline {
match state.engine.process_through_pipeline(pipeline, vec![doc]) {
Ok(mut results) if !results.is_empty() => {
let (action, transformed) = results.remove(0);
@@ -1561,7 +1594,8 @@ pub struct IndexDocParams {
pub op_type: Option<String>,
/// `refresh=true|wait_for` — accepted without error; memtable is always visible.
pub refresh: Option<String>,
- /// `pipeline=my_pipeline` — accepted and logged; pipeline execution not supported.
+ /// `pipeline=my_pipeline` — ingest pipeline to run before indexing.
+ /// `_none` disables the index's `default_pipeline`; absent falls back to it.
pub pipeline: Option<String>,
/// `routing=X` — routing metadata stored on the document (surfaces
/// as `_routing` under `fields`/`exists` queries).
@@ -1624,8 +1658,10 @@ pub async fn index_doc(
} else {
doc
};
- // Execute ingest pipeline if specified.
- let doc = if let Some(ref pipeline) = params.pipeline {
+ // Execute ingest pipeline if one is specified (explicit `?pipeline=`) or
+ // resolved from the index's `index.default_pipeline` setting.
+ let effective_pipeline = resolve_effective_pipeline(&state, &index, params.pipeline.as_deref());
+ let doc = if let Some(ref pipeline) = effective_pipeline {
match state
.engine
.process_through_pipeline(pipeline, vec![doc.clone()])
@@ -1713,7 +1749,8 @@ pub async fn index_doc(
pub struct CreateDocParams {
/// `refresh=true|wait_for` — accepted without error; memtable is always visible.
pub refresh: Option<String>,
- /// `pipeline=my_pipeline` — accepted and logged; pipeline execution not supported.
+ /// `pipeline=my_pipeline` — ingest pipeline to run before indexing.
+ /// `_none` disables the index's `default_pipeline`; absent falls back to it.
pub pipeline: Option<String>,
}
@@ -1726,9 +1763,27 @@ pub async fn create_doc(
Query(params): Query<CreateDocParams>,
Json(doc): Json<Value>,
) -> impl IntoResponse {
- if let Some(ref pipeline) = params.pipeline {
- tracing::info!(pipeline = %pipeline, index = %index, "?pipeline parameter accepted (pipeline execution not supported)");
- }
+ // Execute ingest pipeline if one is specified (explicit `?pipeline=`) or
+ // resolved from the index's `index.default_pipeline` setting.
+ let effective_pipeline = resolve_effective_pipeline(&state, &index, params.pipeline.as_deref());
+ let doc = if let Some(ref pipeline) = effective_pipeline {
+ match state
+ .engine
+ .process_through_pipeline(pipeline, vec![doc.clone()])
+ {
+ Ok(mut results) if !results.is_empty() => {
+ let (action, transformed) = results.remove(0);
+ if matches!(action, xerj_wasm::pipeline::ProcessAction::Drop) {
+ return Json(json!({"result": "noop", "_id": id, "_version": 0}))
+ .into_response();
+ }
+ transformed
+ }
+ _ => doc,
+ }
+ } else {
+ doc
+ };
let idx = match state.engine.get_or_create_index(&index) {
Ok(i) => i,
diff --git a/engine/crates/xerj-engine/src/index.rs b/engine/crates/xerj-engine/src/index.rs
index 8a095c8f2..c8596aa98 100644
--- a/engine/crates/xerj-engine/src/index.rs
+++ b/engine/crates/xerj-engine/src/index.rs
@@ -1100,21 +1100,9 @@ impl Index {
}
}
- // Check for a default_pipeline and log (scripting not executed, just logged).
- {
- let settings = self.settings.read().await;
- if let Some(pipeline) = settings
- .pointer("/index/default_pipeline")
- .and_then(Value::as_str)
- {
- info!(
- pipeline = pipeline,
- index = self.name.as_str(),
- doc_id = doc_id.as_str(),
- "default_pipeline would be applied (pipeline execution not yet supported)"
- );
- }
- }
+ // `index.default_pipeline` is resolved and executed at the API layer
+ // (xerj-api es_compat::resolve_effective_pipeline) before the document
+ // reaches the engine, so no pipeline handling is needed here.
// Auto-embed `semantic_text` fields: vectorise the field's text into
// its companion vector field (`<field>_vector`) so it becomes
diff --git a/engine/tests/es-compat-yaml/yaml/ingest/60_default_pipeline.yml b/engine/tests/es-compat-yaml/yaml/ingest/60_default_pipeline.yml
new file mode 100644
index 000000000..90a33347c
--- /dev/null
+++ b/engine/tests/es-compat-yaml/yaml/ingest/60_default_pipeline.yml
@@ -0,0 +1,68 @@
+---
+"index.default_pipeline auto-applies on single-doc writes":
+ # (1) Register a pipeline that sets field2 = "_value".
+ - do:
+ ingest.put_pipeline:
+ id: "my_pipe"
+ body:
+ processors:
+ - set:
+ field: "field2"
+ value: "_value"
+ - match: { acknowledged: true }
+
+ # (2) Create an index whose default_pipeline is my_pipe.
+ - do:
+ indices.create:
+ index: "idx"
+ body:
+ settings:
+ index:
+ default_pipeline: "my_pipe"
+
+ # (3) Index a doc WITHOUT any ?pipeline param — the default must apply.
+ - do:
+ index:
+ index: "idx"
+ id: "1"
+ refresh: true
+ body: { foo: "bar" }
+
+ - do:
+ get:
+ index: "idx"
+ id: "1"
+ - match: { _source.foo: "bar" }
+ - match: { _source.field2: "_value" }
+
+ # (4) Index a doc with pipeline=_none — the default must be suppressed.
+ - do:
+ index:
+ index: "idx"
+ id: "2"
+ pipeline: "_none"
+ refresh: true
+ body: { foo: "baz" }
+
+ - do:
+ get:
+ index: "idx"
+ id: "2"
+ - match: { _source.foo: "baz" }
+ - is_false: _source.field2
+
+ # (5) Regression guard: an explicit pipeline into a plain index (no default)
+ # still runs the transform.
+ - do:
+ index:
+ index: "plain"
+ id: "3"
+ pipeline: "my_pipe"
+ refresh: true
+ body: { foo: "qux" }
+
+ - do:
+ get:
+ index: "plain"
+ id: "3"
+ - match: { _source.field2: "_value" }