From d448d20802f30333e1fa4a6b8880e832ace8311f Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Thu, 20 Aug 2020 09:01:53 +0200 Subject: [PATCH] more flexibility in managing elasticsearch indexes --- app/models/concerns/indexable.rb | 22 +++++++++++----------- lib/tasks/datacite_doi.rake | 8 ++++---- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/app/models/concerns/indexable.rb b/app/models/concerns/indexable.rb index fd2637a92..c759d8cc5 100644 --- a/app/models/concerns/indexable.rb +++ b/app/models/concerns/indexable.rb @@ -536,10 +536,10 @@ def delete_alias end # create both indexes used for aliasing - def create_index - alias_name = self.index_name - index_name = self.index_name + "_v1" - alternate_index_name = self.index_name + "_v2" + def create_index(options={}) + alias_name = options[:alias_name] || self.index_name + index_name = (options[:index_name] || self.index_name) + "_v1" + alternate_index_name = (options[:index_name] || self.index_name) + "_v2" client = Elasticsearch::Model.client # delete index if it has the same name as the alias @@ -608,8 +608,8 @@ def delete_index(options={}) # delete and create inactive index to use current mappings # Needs to run every time we change the mappings - def upgrade_index - inactive_index ||= self.inactive_index + def upgrade_index(options={}) + inactive_index ||= (options[:index] || self.inactive_index) self.__elasticsearch__.create_index!(index: inactive_index, force: true) "Upgraded inactive index #{inactive_index}." @@ -617,8 +617,8 @@ def upgrade_index # show stats for both indexes def index_stats(options={}) - active_index = self.active_index - inactive_index = self.inactive_index + active_index = options[:active_index] || self.active_index + inactive_index = options[:inactive_index] || self.inactive_index client = Elasticsearch::Model.client # TODO switch to DataciteDoi index @@ -659,9 +659,9 @@ def index_stats(options={}) # switch between the two indexes, i.e. the index that is aliased def switch_index(options={}) - alias_name = self.index_name - index_name = self.index_name + "_v1" - alternate_index_name = self.index_name + "_v2" + alias_name = options[:alias_name] || self.index_name + index_name = (options[:index_name] || self.index_name) + "_v1" + alternate_index_name = (options[:index_name] || self.index_name) + "_v2" client = Elasticsearch::Model.client diff --git a/lib/tasks/datacite_doi.rake b/lib/tasks/datacite_doi.rake index 659e91b5a..6e4694ec7 100644 --- a/lib/tasks/datacite_doi.rake +++ b/lib/tasks/datacite_doi.rake @@ -3,7 +3,7 @@ namespace :datacite_doi do desc "Create index for datacite dois" task :create_index => :environment do - puts DataciteDoi.create_index + puts DataciteDoi.create_index(alias_name: ENV["ALIAS"], index_name: ENV["INDEX"]) end desc "Delete index for datacite dois" @@ -13,7 +13,7 @@ namespace :datacite_doi do desc "Upgrade index for datacite dois" task :upgrade_index => :environment do - puts DataciteDoi.upgrade_index + puts DataciteDoi.upgrade_index(index_name: ENV["INDEX"]) end desc "Create alias for datacite dois" @@ -28,12 +28,12 @@ namespace :datacite_doi do desc "Show index stats for datacite dois" task :index_stats => :environment do - puts DataciteDoi.index_stats + puts DataciteDoi.index_stats(active_index: ENV["ACTIVE"], inactive_index: ENV["INACTIVE"]) end desc "Switch index for datacite dois" task :switch_index => :environment do - puts DataciteDoi.switch_index + puts DataciteDoi.switch_index(alias_name: ENV["ALIAS"], index_name: ENV["INDEX"]) end desc "Return active index for datacite dois"