Module: Elastics::Tasks::Indices
- Included in:
- Elastics::Tasks
- Defined in:
- lib/elastics/tasks/indices.rb
Overview
Most of methods accepts ‘options` hash with:
-
‘:indices` - array of indices to perform action on
-
‘:version` - mapping version to use in method
Instance Attribute Summary collapse
-
#indices_path ⇒ Object
writeonly
Sets the attribute indices_path.
Instance Method Summary collapse
- #alias_action(action, index, version) ⇒ Object
- #create_indices(options = {}) ⇒ Object
- #drop_indices(options = {}) ⇒ Object
- #forward_aliases(options = {}) ⇒ Object
- #indices ⇒ Object
- #indices_paths ⇒ Object
- #indices_settings ⇒ Object
-
#manage_aliases(action, options = {}) ⇒ Object
Action can be :add or :remove.
- #post_aliases(options = {}, &block) ⇒ Object
- #purge(keep_data = false) ⇒ Object
- #versioned_index_name(*args) ⇒ Object
Instance Attribute Details
#indices_path=(value) ⇒ Object (writeonly)
Sets the attribute indices_path
8 9 10 |
# File 'lib/elastics/tasks/indices.rb', line 8 def indices_path=(value) @indices_path = value end |
Instance Method Details
#alias_action(action, index, version) ⇒ Object
90 91 92 93 94 95 |
# File 'lib/elastics/tasks/indices.rb', line 90 def alias_action(action, index, version) {action => { index: versioned_index_name(index, version), alias: versioned_index_name(index, :alias), }} end |
#create_indices(options = {}) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/elastics/tasks/indices.rb', line 52 def create_indices( = {}) version = .fetch :version, :current each_filtered(indices, [:indices]) do |index| versioned_index = versioned_index_name(index, version) exists = client.index_exists?(versioned_index) log_msg = "Creating index #{index} (#{versioned_index})" log_msg << ' - Skipping: exists' if exists log log_msg unless exists client.put(index: versioned_index, body: indices_settings[index]) end end manage_aliases :add, if version.to_s == 'current' end |
#drop_indices(options = {}) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/elastics/tasks/indices.rb', line 43 def drop_indices( = {}) version = .fetch :version, :current each_filtered(indices, [:indices]) do |index| versioned_index = versioned_index_name(index, version) log "Deleting index #{index} (#{versioned_index})" client.delete index: versioned_index end end |
#forward_aliases(options = {}) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/elastics/tasks/indices.rb', line 75 def forward_aliases( = {}) new_versions = {} post_aliases do |index| new_versions[index] = version_manager.next_version index [ alias_action(:remove, index, :current), alias_action(:add, index, :next), ] end drop_indices(.merge version: :current) if .fetch(:drop, true) new_versions.each do |index, version| version_manager.set index, current: version end end |
#indices ⇒ Object
24 25 26 |
# File 'lib/elastics/tasks/indices.rb', line 24 def indices @indices ||= config[:index] ? [config[:index]] : indices_settings.keys end |
#indices_paths ⇒ Object
10 11 12 |
# File 'lib/elastics/tasks/indices.rb', line 10 def indices_paths @indices_paths ||= base_paths.map { |x| File.join x, 'indices' } end |
#indices_settings ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/elastics/tasks/indices.rb', line 14 def indices_settings @indices_settings ||= indices_paths.map { |path| Dir["#{path}/*.yml"] }. flatten.sort. each_with_object({}) do |file, hash| name = File.basename file, '.yml' data = YAML.load_file(file) hash[name] = data[Rails.env] || data end end |
#manage_aliases(action, options = {}) ⇒ Object
Action can be :add or :remove.
68 69 70 71 72 73 |
# File 'lib/elastics/tasks/indices.rb', line 68 def manage_aliases(action, = {}) version = .fetch :version, :current post_aliases() do |index| alias_action(action, index, version) end end |
#post_aliases(options = {}, &block) ⇒ Object
97 98 99 100 101 |
# File 'lib/elastics/tasks/indices.rb', line 97 def post_aliases( = {}, &block) actions = each_filtered(indices, [:indices]).map(&block).flatten log "Posting aliases: #{actions.inspect}" client.post id: :_aliases, body: {actions: actions} if actions.any? end |
#purge(keep_data = false) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/elastics/tasks/indices.rb', line 32 def purge(keep_data = false) unless keep_data drop_indices drop_indices version: :next end index = version_manager.service_index log "Deleting index #{index}" version_manager.reset client.delete index: index end |
#versioned_index_name(*args) ⇒ Object
28 29 30 |
# File 'lib/elastics/tasks/indices.rb', line 28 def versioned_index_name(*args) version_manager.index_name *args end |