Skip to content

Commit

Permalink
improved management of elasticsearch index aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Aug 20, 2020
1 parent 380ffcc commit d769005
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 14 deletions.
28 changes: 24 additions & 4 deletions app/models/concerns/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/activity.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/client.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/client_prefix.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/datacite_doi.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 11 additions & 1 deletion lib/tasks/doi.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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."
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/event.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/other_doi.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/prefix.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/provider.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/provider_prefix.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit d769005

Please sign in to comment.