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

Instance Method Summary collapse

Instance Attribute Details

#indices_path=(value) ⇒ Object (writeonly)

Sets the attribute indices_path

Parameters:

  • value

    the value to set the attribute indices_path to.



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



136
137
138
139
140
141
# File 'lib/elastics/tasks/indices.rb', line 136

def alias_action(action, index, version)
  {action => {
    index: versioned_index_name(index, version),
    alias: versioned_index_name(index, :alias),
  }}
end

#create_indices(options = {}) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/elastics/tasks/indices.rb', line 94

def create_indices(options = {})
  version = options.fetch :version, :current
  each_filtered(indices, options[: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, options if version.to_s == 'current'
end

#drop_indices(options = {}) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/elastics/tasks/indices.rb', line 85

def drop_indices(options = {})
  version = options.fetch :version, :current
  each_filtered(indices, options[: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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/elastics/tasks/indices.rb', line 117

def forward_aliases(options = {})
  new_versions = {}
  post_aliases options do |index|
    new_versions[index] = version_manager.next_version index
    if client.index_exists?(versioned_index_name(index, :current))
      [
        alias_action(:remove, index, :current),
        alias_action(:add, index, :next),
      ]
    else
      alias_action(:add, index, :next)
    end
  end
  drop_indices(options.merge version: :current) if options.fetch(:drop, true)
  new_versions.each do |index, version|
    version_manager.set index, current: version
  end
end

#indicesObject



66
67
68
# File 'lib/elastics/tasks/indices.rb', line 66

def indices
  @indices ||= config[:index] ? [config[:index]] : indices_settings.keys
end

#indices_from_dirsObject

Reads indices settings from separate files. Index name is taken from file name, setting can be given specific for env or one for all envs.

development:
  settings:
    number_of_shards: 5
production:
  settings:
    number_of_shards: 10

# or
settings:
  number_of_shards: 1


56
57
58
59
60
61
62
63
64
# File 'lib/elastics/tasks/indices.rb', line 56

def indices_from_dirs
  indices_paths.map { |path| Dir["#{path}/*.yml"] }.
    flatten.sort.
    each_with_object({}) do |file, hash|
      name = File.basename file, '.yml'
      data = load_yaml(file)
      hash[name] = data[Rails.env] || data
    end
end

#indices_from_filesObject

Reads indices settings from single yml file. Setting can be given specific for env or one for all envs.

tweet:
  development:
    settings:
      number_of_shards: 5
  production:
    settings:
      number_of_shards: 10

user:
  settings:
    number_of_shards: 5


33
34
35
36
37
38
39
40
41
# File 'lib/elastics/tasks/indices.rb', line 33

def indices_from_files
  indices_paths.each_with_object({}) do |path, hash|
    file = "#{path}.yml"
    next unless File.exists?(file)
    load_yaml(file).each do |name, data|
      hash[name] = data[Rails.env] || data
    end
  end
end

#indices_pathsObject



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_settingsObject

Merges settings from single files and dirs.



15
16
17
# File 'lib/elastics/tasks/indices.rb', line 15

def indices_settings
  @indices_settings ||= indices_from_files.merge!(indices_from_dirs)
end

#manage_aliases(action, options = {}) ⇒ Object

Action can be :add or :remove.



110
111
112
113
114
115
# File 'lib/elastics/tasks/indices.rb', line 110

def manage_aliases(action, options = {})
  version = options.fetch :version, :current
  post_aliases(options) do |index|
    alias_action(action, index, version)
  end
end

#post_aliases(options = {}, &block) ⇒ Object



143
144
145
146
147
# File 'lib/elastics/tasks/indices.rb', line 143

def post_aliases(options = {}, &block)
  actions = each_filtered(indices, options[: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



74
75
76
77
78
79
80
81
82
83
# File 'lib/elastics/tasks/indices.rb', line 74

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



70
71
72
# File 'lib/elastics/tasks/indices.rb', line 70

def versioned_index_name(*args)
  version_manager.index_name *args
end