Skip to content

Commit

Permalink
improve rake tasks for elasticsearch index management
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Aug 22, 2020
1 parent 9eb7dab commit 78f251f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 32 deletions.
37 changes: 22 additions & 15 deletions app/models/concerns/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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={})
Expand Down Expand Up @@ -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

Expand Down
10 changes: 5 additions & 5 deletions lib/tasks/datacite_doi.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down
17 changes: 11 additions & 6 deletions lib/tasks/doi.rake
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@ 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"
task :delete_index => :environment 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"
Expand All @@ -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"
Expand Down
12 changes: 6 additions & 6 deletions lib/tasks/other_doi.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down

0 comments on commit 78f251f

Please sign in to comment.