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(bulk): auto-embed semantic_text fields on the bulk auto-id path

xerj-org88038e70701e
diff --git a/engine/crates/xerj-engine/src/bulk.rs b/engine/crates/xerj-engine/src/bulk.rs
index 52526f491..abe5df2ed 100644
--- a/engine/crates/xerj-engine/src/bulk.rs
+++ b/engine/crates/xerj-engine/src/bulk.rs
@@ -821,6 +821,33 @@ pub async fn process_bulk_with_opts(
has
};
+ // Same gate for `semantic_text` fields: those auto-embed on ingest,
+ // which only the per-doc path does. The turbo auto-id batch path
+ // stores raw bytes and never embeds, so auto-id bulk docs into a
+ // semantic_text index would land unvectorised and `semantic`/`knn`
+ // search would silently return nothing. Route them through the slow
+ // path so `apply_semantic_embeddings` runs.
+ let mut index_needs_embedding: HashMap<String, bool> = HashMap::new();
+ let index_has_embedding = |target: &str, cache: &mut HashMap<String, bool>| -> bool {
+ if let Some(b) = cache.get(target) {
+ return *b;
+ }
+ let has = engine
+ .index_mappings
+ .get(target)
+ .map(|m| {
+ let props = m.get("properties").cloned().or_else(|| {
+ m.get("mappings")
+ .and_then(|mm| mm.get("properties"))
+ .cloned()
+ });
+ mapping_has_semantic_text(&props.unwrap_or(Value::Null))
+ })
+ .unwrap_or(false);
+ cache.insert(target.to_string(), has);
+ has
+ };
+
for action in parsed {
// `index` with no `if_seq_no` / `routing` goes through the
// turbo batch path. Actions that carry CAS or routing metadata
@@ -835,7 +862,8 @@ pub async fn process_bulk_with_opts(
&& action.dynamic_templates.is_none()
&& action.pipeline.is_none()
&& !index_has_strict_date(&action.target_index, &mut index_needs_date_validation)
- && !index_has_dynamic_copy(&action.target_index, &mut index_needs_dynamic_copy);
+ && !index_has_dynamic_copy(&action.target_index, &mut index_needs_dynamic_copy)
+ && !index_has_embedding(&action.target_index, &mut index_needs_embedding);
if is_plain_index {
match action.doc_id {
None => {
@@ -1748,6 +1776,31 @@ fn mapping_has_strict_date(props: &Value) -> bool {
false
}
+/// True when any field in the mapping is a `semantic_text` field. Such
+/// fields auto-embed their text into a companion `<field>_vector` at
+/// ingest, which only happens on the per-doc `index_document` path
+/// (`Index::apply_semantic_embeddings`). The turbo auto-id batch path
+/// stores the raw NDJSON bytes and never parses/embeds them, so an index
+/// with a `semantic_text` field MUST be routed through the per-doc path —
+/// otherwise auto-id bulk docs land with an empty vector column and
+/// `semantic`/`knn` search silently returns nothing.
+fn mapping_has_semantic_text(props: &Value) -> bool {
+ let Some(obj) = props.as_object() else {
+ return false;
+ };
+ for (_, spec) in obj {
+ if spec.get("type").and_then(Value::as_str) == Some("semantic_text") {
+ return true;
+ }
+ if let Some(sub) = spec.get("properties") {
+ if mapping_has_semantic_text(sub) {
+ return true;
+ }
+ }
+ }
+ false
+}
+
/// Walk a doc body against the index's mapping properties to find
/// the first field that is declared as `type: date` with a `format`
/// set, `ignore_malformed` off, and a value that doesn't parse
diff --git a/engine/tests/es-compat-yaml/yaml/vectors/51_semantic_text_bulk_autoid.yml b/engine/tests/es-compat-yaml/yaml/vectors/51_semantic_text_bulk_autoid.yml
new file mode 100644
index 000000000..2c00a0fd5
--- /dev/null
+++ b/engine/tests/es-compat-yaml/yaml/vectors/51_semantic_text_bulk_autoid.yml
@@ -0,0 +1,57 @@
+# semantic_text auto-embed on the BULK auto-id path — regression lock.
+#
+# Bug (fixed): the `_bulk` turbo ingest path stores raw NDJSON bytes and
+# defers parsing to flush, so it never ran `apply_semantic_embeddings`.
+# Auto-id `index` actions (no `_id`) were routed through that turbo batch,
+# so documents bulk-loaded into a `semantic_text` index landed with an
+# EMPTY companion vector column — and `semantic`/`knn` search silently
+# returned ZERO hits (HTTP 200, no error). Explicit-id and `create`
+# actions took the per-doc path and embedded correctly, which masked the
+# bug in the recipes (they set an explicit `_id`).
+#
+# Fix: bulk.rs routes any index that declares a `semantic_text` field
+# through the per-doc path (like the existing strict-date / dynamic-copy
+# guards) so embedding runs regardless of how `_id` is assigned.
+#
+# This test bulk-indexes with NO `_id` (auto-generated) and asserts the
+# semantic query finds the docs. Before the fix hits.total.value was 0.
+
+setup:
+ - do:
+ indices.create:
+ index: semantic-bulk-autoid
+ body:
+ mappings:
+ properties:
+ body:
+ type: semantic_text
+
+ - do:
+ bulk:
+ refresh: true
+ body:
+ - index:
+ _index: semantic-bulk-autoid
+ - body: "a hungry dog chased the ball across the green park"
+ - index:
+ _index: semantic-bulk-autoid
+ - body: "investors sold technology shares amid rising interest rates"
+
+---
+"semantic query finds auto-id bulk-indexed docs (companion vector was embedded)":
+ - do:
+ search:
+ index: semantic-bulk-autoid
+ body:
+ size: 1
+ query:
+ semantic:
+ field: body
+ query: "a dog ran after a ball in the park"
+ k: 10
+
+ # Both auto-id bulk docs carry a vector → pool total is 2 (was 0 pre-fix).
+ - match: { hits.total.value: 2 }
+ - length: { hits.hits: 1 }
+ # The vocabulary-overlapping doc must rank first.
+ - match: { hits.hits.0._source.body: "a hungry dog chased the ball across the green park" }