From 78f251fe06391901d1440364524c11b0b912241b Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Sat, 22 Aug 2020 07:42:56 +0200 Subject: [PATCH] improve rake tasks for elasticsearch index management --- app/models/concerns/indexable.rb | 37 +++++++++++++++++++------------- lib/tasks/datacite_doi.rake | 10 ++++----- lib/tasks/doi.rake | 17 +++++++++------ lib/tasks/other_doi.rake | 12 +++++------ 4 files changed, 44 insertions(+), 32 deletions(-) diff --git a/app/models/concerns/indexable.rb b/app/models/concerns/indexable.rb index c759d8cc5..72a57205c 100644 --- a/app/models/concerns/indexable.rb +++ b/app/models/concerns/indexable.rb @@ -445,10 +445,10 @@ def monitor_reindex end # create alias - def create_alias - alias_name = self.index_name - index_name = self.index_name + "_v1" - alternate_index_name = self.index_name + "_v2" + def create_alias(options={}) + alias_name = options[:alias] || self.index_name + index_name = options[:index] || self.index_name + "_v1" + alternate_index_name = options[:index] || self.index_name + "_v2" client = Elasticsearch::Model.client @@ -490,12 +490,12 @@ def list_aliases end # delete alias - def delete_alias - client = Elasticsearch::Model.client + def delete_alias(options={}) + alias_name = options[:alias] || self.index_name + index_name = options[:index] || self.index_name + "_v1" + alternate_index_name = options[:index] || self.index_name + "_v2" - alias_name = self.index_name - index_name = self.index_name + "_v1" - alternate_index_name = self.index_name + "_v2" + client = Elasticsearch::Model.client self.__elasticsearch__.delete_index!(index: alias_name) if self.__elasticsearch__.index_exists?(index: alias_name) @@ -537,9 +537,9 @@ def delete_alias # create both indexes used for aliasing 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" + alias_name = options[:alias] || self.index_name + index_name = (options[:index] || self.index_name) + "_v1" + alternate_index_name = (options[:index] || self.index_name) + "_v2" client = Elasticsearch::Model.client # delete index if it has the same name as the alias @@ -606,6 +606,13 @@ def delete_index(options={}) # end end + # list all indices + def list_indices + client = Elasticsearch::Model.client + cat_client = Elasticsearch::API::Cat::CatClient.new(client) + puts cat_client.indices + end + # delete and create inactive index to use current mappings # Needs to run every time we change the mappings def upgrade_index(options={}) @@ -659,9 +666,9 @@ def index_stats(options={}) # switch between the two indexes, i.e. the index that is aliased def switch_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" + alias_name = options[:alias] || self.index_name + index_name = (options[:index] || self.index_name) + "_v1" + alternate_index_name = (options[:index] || self.index_name) + "_v2" client = Elasticsearch::Model.client diff --git a/lib/tasks/datacite_doi.rake b/lib/tasks/datacite_doi.rake index 6e4694ec7..8da494991 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(alias_name: ENV["ALIAS"], index_name: ENV["INDEX"]) + puts DataciteDoi.create_index(alias: ENV["ALIAS"], index: ENV["INDEX"]) end desc "Delete index for datacite dois" @@ -13,17 +13,17 @@ namespace :datacite_doi do desc "Upgrade index for datacite dois" task :upgrade_index => :environment do - puts DataciteDoi.upgrade_index(index_name: ENV["INDEX"]) + puts DataciteDoi.upgrade_index(index: ENV["INDEX"]) end desc "Create alias for datacite dois" task :create_alias => :environment do - puts DataciteDoi.create_alias + puts DataciteDoi.create_alias(index: ENV["INDEX"], alias: ENV["ALIAS"]) end desc "Delete alias for datacite dois" task :delete_alias => :environment do - puts DataciteDoi.delete_alias + puts DataciteDoi.delete_alias(index: ENV["INDEX"], alias: ENV["ALIAS"]) end desc "Show index stats for datacite dois" @@ -33,7 +33,7 @@ namespace :datacite_doi do desc "Switch index for datacite dois" task :switch_index => :environment do - puts DataciteDoi.switch_index(alias_name: ENV["ALIAS"], index_name: ENV["INDEX"]) + puts DataciteDoi.switch_index(alias: ENV["ALIAS"], index: ENV["INDEX"]) end desc "Return active index for datacite dois" diff --git a/lib/tasks/doi.rake b/lib/tasks/doi.rake index 95244d0b8..b97e6edfd 100644 --- a/lib/tasks/doi.rake +++ b/lib/tasks/doi.rake @@ -4,7 +4,7 @@ namespace :doi do # TODO switch to DataCite DOI index desc "Create index for dois" task :create_index => :environment do - puts Doi.create_index + puts Doi.create_index(index: ENV["INDEX"], alias: ENV["ALIAS"]) end desc "Delete index for dois" @@ -12,14 +12,19 @@ namespace :doi do puts Doi.delete_index(index: ENV["INDEX"]) end + desc "List indices for dois" + task :list_indices => :environment do + puts Doi.list_indices + end + desc "Upgrade index for dois" task :upgrade_index => :environment do - puts Doi.upgrade_index + puts Doi.upgrade_index(index: ENV["INDEX"]) end desc "Create alias for dois" task :create_alias => :environment do - puts Doi.create_alias + puts Doi.create_alias(index: ENV["INDEX"], alias: ENV["ALIAS"]) end desc "List aliases for dois" @@ -29,17 +34,17 @@ namespace :doi do desc "Delete alias for dois" task :delete_alias => :environment do - puts Doi.delete_alias + puts Doi.delete_alias(index: ENV["INDEX"], alias: ENV["ALIAS"]) end desc "Show index stats for dois" task :index_stats => :environment do - puts Doi.index_stats + puts Doi.index_stats(active_index: ENV["ACTIVE"], inactive_index: ENV["INACTIVE"]) end desc "Switch index for dois" task :switch_index => :environment do - puts Doi.switch_index + puts Doi.switch_index(index: ENV["INDEX"], alias: ENV["ALIAS"]) end desc "List templates for dois" diff --git a/lib/tasks/other_doi.rake b/lib/tasks/other_doi.rake index b5617bc3e..c7054052b 100644 --- a/lib/tasks/other_doi.rake +++ b/lib/tasks/other_doi.rake @@ -3,7 +3,7 @@ namespace :other_doi do desc "Create index for other dois" task :create_index => :environment do - puts OtherDoi.create_index + puts OtherDoi.create_index(alias: ENV["ALIAS"], index: ENV["INDEX"]) end desc "Delete index for other dois" @@ -13,27 +13,27 @@ namespace :other_doi do desc "Upgrade index for other dois" task :upgrade_index => :environment do - puts OtherDoi.upgrade_index + puts OtherDoi.upgrade_index(index: ENV["INDEX"]) end desc "Create alias for other dois" task :create_alias => :environment do - puts OtherDoi.create_alias + puts OtherDoi.create_alias(alias: ENV["ALIAS"], index: ENV["INDEX"]) end desc "Delete alias for other dois" task :delete_alias => :environment do - puts OtherDoi.delete_alias + puts OtherDoi.delete_alias(alias: ENV["ALIAS"], index: ENV["INDEX"]) end desc "Show index stats for other dois" task :index_stats => :environment do - puts OtherDoi.index_stats + puts OtherDoi.index_stats(active_index: ENV["ACTIVE"], inactive_index: ENV["INACTIVE"]) end desc "Switch index for other dois" task :switch_index => :environment do - puts OtherDoi.switch_index + puts OtherDoi.switch_index(alias: ENV["ALIAS"], index: ENV["INDEX"]) end desc "Return active index for other dois"