Method: Chewy::Index::Actions::ClassMethods#reset!

Defined in:
lib/chewy/index/actions.rb

#reset!(suffix = nil, apply_journal: true, journal: false, **import_options) ⇒ true, false Also known as: reset

Deletes, creates and imports data to the index. Returns the import result. If index name suffix is passed as the first argument - performs zero-downtime index resetting.

It also applies journal if anything was journaled during the reset.

Examples:

UsersIndex.reset!
UsersIndex.reset! Time.now.to_i

Parameters:

  • suffix (String) (defaults to: nil)

    a suffix for the newly created index

  • apply_journal (true, false) (defaults to: true)

    if true, journal is applied after the import is completed

  • journal (true, false) (defaults to: false)

    journaling is switched off for import during reset by default

  • import_options (Hash)

    options, passed to the import call

Returns:

  • (true, false)

    false in case of errors

See Also:



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/chewy/index/actions.rb', line 149

def reset!(suffix = nil, apply_journal: true, journal: false, **import_options)
  result = if suffix.present?
    start_time = Time.now
    indexes = self.indexes - [index_name]
    create! suffix, alias: false

    general_name = index_name
    suffixed_name = index_name(suffix: suffix)

    optimize_index_settings suffixed_name
    result = import(**import_options.merge(
      suffix: suffix,
      journal: journal,
      refresh: !Chewy.reset_disable_refresh_interval
    ))
    original_index_settings suffixed_name

    delete if indexes.blank?
    client.indices.update_aliases body: {actions: [
      *indexes.map do |index|
        {remove: {index: index, alias: general_name}}
      end,
      {add: {index: suffixed_name, alias: general_name}}
    ]}
    client.indices.delete index: indexes if indexes.present?

    self.journal.apply(start_time, **import_options) if apply_journal
    result
  else
    purge!
    import(**import_options.merge(journal: journal))
  end

  specification.lock!
  result
end