Module: Elastics::Tasks::Migrations
- Included in:
- Elastics::Tasks
- Defined in:
- lib/elastics/tasks/migrations.rb
Instance Method Summary collapse
- #migrate(options = {}) ⇒ Object
- #migrate!(options = {}) ⇒ Object
- #models_to_reindex(options = {}) ⇒ Object
-
#reindex(options = {}) ⇒ Object
Runs ‘#reindex_elastics` on matching models.
Instance Method Details
#migrate(options = {}) ⇒ Object
4 5 6 7 |
# File 'lib/elastics/tasks/migrations.rb', line 4 def migrate( = {}) create_indices() put_mappings() end |
#migrate!(options = {}) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/elastics/tasks/migrations.rb', line 9 def migrate!( = {}) = .merge version: :next need_reindex = .fetch(:reindex, true) drop_indices() create_indices() put_mappings() started_at = reindex() if need_reindex forward_aliases() if need_reindex reindex(.merge(version: :current, updated_after: started_at)) end end |
#models_to_reindex(options = {}) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/elastics/tasks/migrations.rb', line 41 def models_to_reindex( = {}) indices = [:indices].try!(:map, &:to_s) types = [:types].try!(:map, &:to_s) models = Model.list.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
Runs ‘#reindex_elastics` on matching models. Returns hash with timestamps with reindex start time for each model. Supports this kind of hash as `:updated_after` option, to reindex only updated records.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/elastics/tasks/migrations.rb', line 26 def reindex( = {}) version = .fetch(:version, :current) updated_after = .fetch(:updated_after, {}) Rails.application.eager_load! if defined?(Rails) VersionManager.use_version version do Hash[models_to_reindex().map do |model| started_at = Time.now log "Reindexing #{model.elastics_index_base} into " \ "#{model.elastics_index_name}/#{model.elastics_type_name}" model.reindex_elastics(updated_after: updated_after[model]) [model, started_at] end] end end |