From 5ff05c6f3ed3ba386c52d297e1371957bbcde053 Mon Sep 17 00:00:00 2001 From: mdecimus <11444311+mdecimus@users.noreply.github.com> Date: Wed, 17 Dec 2025 15:00:15 +0100 Subject: [PATCH] Elasticsearch: Ignore resource_already_exists_exception errors when creating indexes (closes #2535) --- crates/store/src/backend/elastic/main.rs | 45 +++++++++++++++++------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/crates/store/src/backend/elastic/main.rs b/crates/store/src/backend/elastic/main.rs index 8d814cbf..63585dcc 100644 --- a/crates/store/src/backend/elastic/main.rs +++ b/crates/store/src/backend/elastic/main.rs @@ -39,9 +39,6 @@ impl ElasticSearchStore { .property_or_default((&prefix, "index.include-source"), "false") .unwrap_or(false); - #[cfg(feature = "test_mode")] - let _ = es.drop_indexes().await; - if let Err(err) = es.create_indexes(shards, replicas, with_source).await { config.new_build_error(prefix.as_str(), err.to_string()); } @@ -102,17 +99,39 @@ impl ElasticSearchStore { } } }); - let body = serde_json::to_string(&body).unwrap_or_default(); - assert_success( - self.client - .put(format!("{}/{}", self.url, T::index().index_name())) - .body(body) - .send() - .await, - ) - .await - .map(|_| ()) + let response = self + .client + .put(format!("{}/{}", self.url, T::index().index_name())) + .body(body.to_string()) + .send() + .await + .map_err(|err| { + trc::StoreEvent::ElasticsearchError + .reason(err) + .details("Failed to create index") + })?; + + match response.status().as_u16() { + 200..300 => Ok(()), + status @ (400..500) => { + let text = response.text().await.unwrap_or_default(); + if text.contains("resource_already_exists_exception") { + // Index already exists, ignore + Ok(()) + } else { + Err(trc::StoreEvent::ElasticsearchError + .reason(text) + .ctx(trc::Key::Code, status)) + } + } + status => { + let text = response.text().await.unwrap_or_default(); + Err(trc::StoreEvent::ElasticsearchError + .reason(text) + .ctx(trc::Key::Code, status)) + } + } } #[cfg(feature = "test_mode")]