Module: Elastics::Tasks::Migrations

Included in:
Elastics::Tasks
Defined in:
lib/elastics/tasks/migrations.rb

Instance Method Summary collapse

Instance Method Details

#migrate(options = {}) ⇒ Object



4
5
6
7
# File 'lib/elastics/tasks/migrations.rb', line 4

def migrate(options = {})
  create_indices(options)
  put_mappings(options)
end

#migrate!(options = {}) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/elastics/tasks/migrations.rb', line 9

def migrate!(options = {})
  options_next = options.merge version: :next
  drop_indices(options_next)
  create_indices(options_next)
  put_mappings(options_next)
  reindex(options_next) if options.fetch(:reindex, true)
  forward_aliases(options)
end

#models_to_reindex(options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/elastics/tasks/migrations.rb', line 30

def models_to_reindex(options = {})
  indices = options[:indices].try!(:map, &:to_s)
  types = options[:types].try!(:map, &:to_s)
  models = Elastics.models.select do |model|
    next if indices && !indices.include?(model.elastics_index_base)
    next if types && !types.include?(model.elastics_type_name)
    true
  end
  models.reject { |model| models.find { |other| model < other } }
end

#reindex(options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/elastics/tasks/migrations.rb', line 18

def reindex(options = {})
  version = options.fetch(:version, :current)
  Rails.application.eager_load! if defined?(Rails)
  VersionManager.use_version version do
    models_to_reindex(options).each do |model|
      log "Reindexing #{model.elastics_index_base} into " \
        "#{model.elastics_index_name}/#{model.elastics_type_name}"
      model.reindex_elastics
    end
  end
end