Skip to content

Commit

Permalink
more flexibility in managing elasticsearch indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Aug 20, 2020
1 parent d769005 commit d448d20
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
22 changes: 11 additions & 11 deletions app/models/concerns/indexable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -608,17 +608,17 @@ 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}."
end

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

Expand Down
8 changes: 4 additions & 4 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
puts DataciteDoi.create_index(alias_name: ENV["ALIAS"], index_name: ENV["INDEX"])
end

desc "Delete index for datacite dois"
Expand All @@ -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"
Expand All @@ -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"
Expand Down

0 comments on commit d448d20

Please sign in to comment.