From b9a93b7434f63fdd99ecd35b674474abb237c20f Mon Sep 17 00:00:00 2001 From: "Marcos G. Zimmermann" Date: Fri, 28 Jun 2024 18:13:00 -0300 Subject: [PATCH] fix: do not delete exiting index when the given suffix already exist --- lib/esse/index/indices.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/esse/index/indices.rb b/lib/esse/index/indices.rb index 041be98..2e6f1f5 100644 --- a/lib/esse/index/indices.rb +++ b/lib/esse/index/indices.rb @@ -50,9 +50,14 @@ def create_index(suffix: nil, body: nil, **options) # @see https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-open-close.html def reset_index(suffix: index_suffix, optimize: true, import: true, reindex: false, **options) cluster.throw_error_when_readonly! - existing = [] - suffix ||= Esse.timestamp - suffix = Esse.timestamp while index_exist?(suffix: suffix).tap { |exist| existing << suffix if exist } + + if suffix && index_exist?(suffix: suffix) + existing = [suffix] + suffix = Esse.timestamp while index_exist?(suffix: suffix).tap { |exist| existing << suffix if exist } + Esse.logger.warn { "Index with suffix #{existing.join(', ')} already exists. Using #{suffix} instead. Consider using a different suffix" } + else + suffix = Esse.timestamp while index_exist?(suffix: suffix) + end if optimize definition = [settings_hash, mappings_hash].reduce(&:merge) @@ -77,7 +82,6 @@ def reset_index(suffix: index_suffix, optimize: true, import: true, reindex: fal update_settings(suffix: suffix) end update_aliases(suffix: suffix) - existing.each { |s| delete_index!(**options, suffix: s) } true end