From d769005273e5c46f6eab42e928f0a5e486c8bc8b Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Thu, 20 Aug 2020 08:47:33 +0200 Subject: [PATCH] improved management of elasticsearch index aliases --- app/models/concerns/indexable.rb | 28 ++++++++++++++++++++++++---- lib/tasks/activity.rake | 2 +- lib/tasks/client.rake | 2 +- lib/tasks/client_prefix.rake | 2 +- lib/tasks/datacite_doi.rake | 2 +- lib/tasks/doi.rake | 12 +++++++++++- lib/tasks/event.rake | 2 +- lib/tasks/other_doi.rake | 2 +- lib/tasks/prefix.rake | 2 +- lib/tasks/provider.rake | 2 +- lib/tasks/provider_prefix.rake | 2 +- 11 files changed, 44 insertions(+), 14 deletions(-) diff --git a/app/models/concerns/indexable.rb b/app/models/concerns/indexable.rb index a5a4074e0..fd2637a92 100644 --- a/app/models/concerns/indexable.rb +++ b/app/models/concerns/indexable.rb @@ -482,14 +482,21 @@ def create_alias # end end + # list all aliases + def list_aliases + client = Elasticsearch::Model.client + cat_client = Elasticsearch::API::Cat::CatClient.new(client) + puts cat_client.aliases + end + # delete alias def delete_alias + client = Elasticsearch::Model.client + 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) # indexes in DOI model are aliased from DataciteDoi and OtherDoi models @@ -565,11 +572,17 @@ def create_index end # delete index and both indexes used for aliasing - def delete_index + def delete_index(options={}) + client = Elasticsearch::Model.client + + if options[:index] + self.__elasticsearch__.delete_index!(index: options[:index]) + return "Deleted index #{options[:index]}." + end + alias_name = self.index_name index_name = self.index_name + "_v1" alternate_index_name = self.index_name + "_v2" - client = Elasticsearch::Model.client # indexes in DOI model are aliased from DataciteDoi and OtherDoi models # TODO switch to DataciteDoi index @@ -812,6 +825,13 @@ def create_template end end + # list all templates + def list_templates(options={}) + client = Elasticsearch::Model.client + cat_client = Elasticsearch::API::Cat::CatClient.new(client) + puts cat_client.templates(name: options[:name]) + end + # delete index template def delete_template alias_name = self.index_name diff --git a/lib/tasks/activity.rake b/lib/tasks/activity.rake index 77d07fd76..d182479e2 100644 --- a/lib/tasks/activity.rake +++ b/lib/tasks/activity.rake @@ -8,7 +8,7 @@ namespace :activity do desc "Delete index for activities" task :delete_index => :environment do - puts Activity.delete_index + puts Activity.delete_index(index: ENV["INDEX"]) end desc "Upgrade index for activities" diff --git a/lib/tasks/client.rake b/lib/tasks/client.rake index fa72fbc25..35b719df6 100644 --- a/lib/tasks/client.rake +++ b/lib/tasks/client.rake @@ -8,7 +8,7 @@ namespace :client do desc "Delete index for clients" task :delete_index => :environment do - puts Client.delete_index + puts Client.delete_index(index: ENV["INDEX"]) end desc "Upgrade index for clients" diff --git a/lib/tasks/client_prefix.rake b/lib/tasks/client_prefix.rake index 0dbd1f42c..ac0c013fd 100644 --- a/lib/tasks/client_prefix.rake +++ b/lib/tasks/client_prefix.rake @@ -8,7 +8,7 @@ namespace :client_prefix do desc "Delete index for client_prefixes" task :delete_index => :environment do - puts ClientPrefix.delete_index + puts ClientPrefix.delete_index(index: ENV["INDEX"]) end desc "Upgrade index for client_prefixes" diff --git a/lib/tasks/datacite_doi.rake b/lib/tasks/datacite_doi.rake index e66cf36d6..659e91b5a 100644 --- a/lib/tasks/datacite_doi.rake +++ b/lib/tasks/datacite_doi.rake @@ -8,7 +8,7 @@ namespace :datacite_doi do desc "Delete index for datacite dois" task :delete_index => :environment do - puts DataciteDoi.delete_index + puts DataciteDoi.delete_index(index: ENV["INDEX"]) end desc "Upgrade index for datacite dois" diff --git a/lib/tasks/doi.rake b/lib/tasks/doi.rake index 7ad6a12e5..95244d0b8 100644 --- a/lib/tasks/doi.rake +++ b/lib/tasks/doi.rake @@ -9,7 +9,7 @@ namespace :doi do desc "Delete index for dois" task :delete_index => :environment do - puts Doi.delete_index + puts Doi.delete_index(index: ENV["INDEX"]) end desc "Upgrade index for dois" @@ -22,6 +22,11 @@ namespace :doi do puts Doi.create_alias end + desc "List aliases for dois" + task :list_aliases => :environment do + puts Doi.list_aliases + end + desc "Delete alias for dois" task :delete_alias => :environment do puts Doi.delete_alias @@ -37,6 +42,11 @@ namespace :doi do puts Doi.switch_index end + desc "List templates for dois" + task :list_templates => :environment do + puts Doi.list_templates(name: "dois*") + end + desc "Return active index for dois" task :active_index => :environment do puts Doi.active_index + " is the active index." diff --git a/lib/tasks/event.rake b/lib/tasks/event.rake index 26b73d1de..7247eea65 100644 --- a/lib/tasks/event.rake +++ b/lib/tasks/event.rake @@ -8,7 +8,7 @@ namespace :event do desc "Delete index for events" task :delete_index => :environment do - puts Event.delete_index + puts Event.delete_index(index: ENV["INDEX"]) end desc "Upgrade index for events" diff --git a/lib/tasks/other_doi.rake b/lib/tasks/other_doi.rake index d06ea6cb6..b5617bc3e 100644 --- a/lib/tasks/other_doi.rake +++ b/lib/tasks/other_doi.rake @@ -8,7 +8,7 @@ namespace :other_doi do desc "Delete index for other dois" task :delete_index => :environment do - puts OtherDoi.delete_index + puts OtherDoi.delete_index(index: ENV["INDEX"]) end desc "Upgrade index for other dois" diff --git a/lib/tasks/prefix.rake b/lib/tasks/prefix.rake index 80dc153ac..929e7c06a 100644 --- a/lib/tasks/prefix.rake +++ b/lib/tasks/prefix.rake @@ -8,7 +8,7 @@ namespace :prefix do desc "Delete index for prefixes" task :delete_index => :environment do - puts Prefix.delete_index + puts Prefix.delete_index(index: ENV["INDEX"]) end desc "Upgrade index for prefixes" diff --git a/lib/tasks/provider.rake b/lib/tasks/provider.rake index e590af02e..79cecb3a3 100644 --- a/lib/tasks/provider.rake +++ b/lib/tasks/provider.rake @@ -8,7 +8,7 @@ namespace :provider do desc "Delete index for providers" task delete_index: :environment do - puts Provider.delete_index + puts Provider.delete_index(index: ENV["INDEX"]) end desc "Upgrade index for providers" diff --git a/lib/tasks/provider_prefix.rake b/lib/tasks/provider_prefix.rake index f29f11e24..577121409 100644 --- a/lib/tasks/provider_prefix.rake +++ b/lib/tasks/provider_prefix.rake @@ -8,7 +8,7 @@ namespace :provider_prefix do desc "Delete index for provider_prefixes" task :delete_index => :environment do - puts ProviderPrefix.delete_index + puts ProviderPrefix.delete_index(index: ENV["INDEX"]) end desc "Upgrade index for provider_prefixes"