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): omit versioning fields on a failed _bulk item (#1672)

xerj-org51f4fe24ce86
diff --git a/engine/crates/xerj-api/src/es_compat.rs b/engine/crates/xerj-api/src/es_compat.rs
index 8399be838..b59a3bc35 100644
--- a/engine/crates/xerj-api/src/es_compat.rs
+++ b/engine/crates/xerj-api/src/es_compat.rs
@@ -18876,14 +18876,12 @@ async fn process_bulk_body(
std::collections::BTreeSet::new();
let mut items: Vec<EsBulkItem> = Vec::with_capacity(result.items.len());
for item in result.items {
- if item.error.is_some() || !(200..300).contains(&item.status) {
+ let failed = item.error.is_some() || !(200..300).contains(&item.status);
+ if failed {
failed_items += 1;
}
audited_index_names.insert(item.index.clone());
- if item.error.is_none()
- && (200..300).contains(&item.status)
- && matches!(item.action.as_str(), "index" | "create" | "update")
- {
+ if !failed && matches!(item.action.as_str(), "index" | "create" | "update") {
*docs_indexed_tally.entry(item.index.clone()).or_insert(0) += 1;
}
let get = item.get_source.clone().map(|s| {
@@ -18894,18 +18892,30 @@ async fn process_bulk_body(
});
// Real per-item `_seq_no` / `_version` — same resolvers `_mget` and
// GET `_doc` use (were hardcoded `_version: 1` / a wall-clock
- // timestamp masquerading as `_seq_no`).
- let (version, seq_no) = state
- .engine
- .get_index(&item.index)
- .ok()
- .map(|idx| {
- (
- idx.lookup_version(&item.id).unwrap_or(1),
- idx.lookup_seq_no(&item.id).unwrap_or(0),
- )
- })
- .unwrap_or((1, 0));
+ // timestamp masquerading as `_seq_no`). A failed item omits them:
+ // looking up a previous document and printing its version next to
+ // `status: 400` reads as "this write landed".
+ let (version, seq_no, primary_term, shards) = if failed {
+ (None, None, None, None)
+ } else {
+ let (version, seq_no) = state
+ .engine
+ .get_index(&item.index)
+ .ok()
+ .map(|idx| {
+ (
+ idx.lookup_version(&item.id).unwrap_or(1),
+ idx.lookup_seq_no(&item.id).unwrap_or(0),
+ )
+ })
+ .unwrap_or((1, 0));
+ (
+ Some(version),
+ Some(seq_no),
+ Some(1),
+ Some(crate::responses::EsShards::single_success()),
+ )
+ };
let item_result = EsBulkItemResult {
index: item.index,
id: item.id,
@@ -18913,14 +18923,14 @@ async fn process_bulk_body(
// A successful action with no explicit result is a `delete`
// (the only one that does not set one). A FAILED action has no
// result at all — see `EsBulkItemResult::result`.
- result: if item.error.is_some() {
+ result: if failed {
None
} else {
Some(item.result.clone().unwrap_or_else(|| "deleted".to_string()))
},
- shards: crate::responses::EsShards::single_success(),
+ shards,
seq_no,
- primary_term: 1,
+ primary_term,
status: item.status,
get,
error: item.error.map(|e| {
@@ -19147,9 +19157,24 @@ mod malformed_bulk_route_tests {
assert_eq!(response["errors"], true);
assert_eq!(response["items"][0]["index"]["status"], 201);
+ let ok_item = &response["items"][0]["index"];
+ assert!(
+ ok_item.get("_version").is_some()
+ && ok_item.get("_seq_no").is_some()
+ && ok_item.get("_primary_term").is_some()
+ && ok_item.get("_shards").is_some(),
+ "a successful bulk item keeps its versioning fields: {ok_item}"
+ );
+
let failed = &response["items"][1]["index"];
assert_eq!(failed["_id"], "broken-doc");
assert_eq!(failed["status"], 400);
+ for absent in ["_version", "_seq_no", "_primary_term", "_shards", "result"] {
+ assert!(
+ failed.get(absent).is_none(),
+ "a failed bulk item must omit {absent}: {failed}"
+ );
+ }
assert_eq!(
failed["error"]["type"], "document_parsing_exception",
"unexpected response: {response}"
diff --git a/engine/crates/xerj-api/src/responses.rs b/engine/crates/xerj-api/src/responses.rs
index d84f21af2..0c9aabbf8 100644
--- a/engine/crates/xerj-api/src/responses.rs
+++ b/engine/crates/xerj-api/src/responses.rs
@@ -445,8 +445,11 @@ pub struct EsBulkItemResult {
pub index: String,
#[serde(rename = "_id")]
pub id: String,
- #[serde(rename = "_version")]
- pub version: u64,
+ /// Present on a successful item. Absent on a failure: a previous
+ /// document's version (or the default `1`) used to be written next to
+ /// `status: 400`, which reads as "this write landed".
+ #[serde(rename = "_version", skip_serializing_if = "Option::is_none")]
+ pub version: Option<u64>,
/// `"created"` / `"updated"` / `"deleted"` / `"noop"`, and ABSENT on an
/// item that failed.
///
@@ -459,12 +462,13 @@ pub struct EsBulkItemResult {
/// failed item; so do we.
#[serde(skip_serializing_if = "Option::is_none")]
pub result: Option<String>,
- #[serde(rename = "_shards")]
- pub shards: EsShards,
- #[serde(rename = "_seq_no")]
- pub seq_no: u64,
- #[serde(rename = "_primary_term")]
- pub primary_term: u64,
+ /// Absent on a failed item, same as `_version` / `_seq_no`.
+ #[serde(rename = "_shards", skip_serializing_if = "Option::is_none")]
+ pub shards: Option<EsShards>,
+ #[serde(rename = "_seq_no", skip_serializing_if = "Option::is_none")]
+ pub seq_no: Option<u64>,
+ #[serde(rename = "_primary_term", skip_serializing_if = "Option::is_none")]
+ pub primary_term: Option<u64>,
pub status: u16,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<BulkItemError>,