Module: Chewy::Index::Actions::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#create(*args, **kwargs) ⇒ Object

Creates index and applies mappings and settings. Returns false in case of unsuccessful creation.

UsersIndex.create # creates index named users

Index name suffix might be passed optionally. In this case, method creates index with suffix and makes unsuffixed alias for it.

UsersIndex.create '01-2013' # creates index users_01-2013 and alias users for it UsersIndex.create '01-2013', alias: false # creates index users_01-2013 only and no alias

Suffixed index names might be used for zero-downtime mapping change, for example. Description: (http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/).



33
34
35
36
37
# File 'lib/chewy/index/actions.rb', line 33

def create(*args, **kwargs)
  create!(*args, **kwargs)
rescue Elasticsearch::Transport::Transport::Errors::BadRequest
  false
end

#create!(suffix = nil, **options) ⇒ Object

Creates index and applies mappings and settings. Raises elasticsearch-ruby transport error in case of unsuccessfull creation.

UsersIndex.create! # creates index named users

Index name suffix might be passed optionally. In this case, method creates index with suffix and makes unsuffixed alias for it.

UsersIndex.create! '01-2014' # creates index users_01-2014 and alias users for it UsersIndex.create! '01-2014', alias: false # creates index users_01-2014 only and no alias

Suffixed index names might be used for zero-downtime mapping change, for example. Description: (http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/).



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/chewy/index/actions.rb', line 55

def create!(suffix = nil, **options)
  options.reverse_merge!(alias: true)
  general_name = index_name
  suffixed_name = index_name(suffix: suffix)

  body = specification_hash
  body[:aliases] = {general_name => {}} if options[:alias] && suffixed_name != general_name
  result = client.indices.create(index: suffixed_name, body: body)

  Chewy.wait_for_status if result
  result
end

#delete(suffix = nil) ⇒ Object

Deletes ES index. Returns false in case of error.

UsersIndex.delete # deletes users index

Supports index suffix passed as the first argument

UsersIndex.delete '01-2014' # deletes users_01-2014 index



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/chewy/index/actions.rb', line 76

def delete(suffix = nil)
  # Verify that the index_name is really the index_name and not an alias.
  #
  #   "The index parameter in the delete index API no longer accepts alias names.
  #   Instead, it accepts only index names (or wildcards which will expand to matching indices)."
  #   https://www.elastic.co/guide/en/elasticsearch/reference/6.8/breaking-changes-6.0.html#_delete_index_api_resolves_indices_expressions_only_against_indices
  index_names = client.indices.get_alias(index: index_name(suffix: suffix)).keys
  result = client.indices.delete index: index_names.join(',')
  Chewy.wait_for_status if result
  result
  # es-ruby >= 1.0.10 handles Elasticsearch::Transport::Transport::Errors::NotFound
  # by itself, rescue is for previous versions
rescue Elasticsearch::Transport::Transport::Errors::NotFound
  false
end

#delete!(suffix = nil) ⇒ Object

Deletes ES index. Raises elasticsearch-ruby transport error in case of error.

UsersIndex.delete # deletes users index

Supports index suffix passed as the first argument

UsersIndex.delete '01-2014' # deletes users_01-2014 index



101
102
103
104
105
# File 'lib/chewy/index/actions.rb', line 101

def delete!(suffix = nil)
  # es-ruby >= 1.0.10 handles Elasticsearch::Transport::Transport::Errors::NotFound
  # by itself, so it is raised here
  delete(suffix) or raise Elasticsearch::Transport::Transport::Errors::NotFound
end

#exists?Boolean

Checks index existance. Returns true or false

UsersIndex.exists? #=> true

Returns:

  • (Boolean)


14
15
16
# File 'lib/chewy/index/actions.rb', line 14

def exists?
  client.indices.exists(index: index_name)
end

#journalChewy::Journal

A Journal instance for the particular index

Returns:



214
215
216
# File 'lib/chewy/index/actions.rb', line 214

def journal
  @journal ||= Chewy::Journal.new(self)
end

#purge(suffix = nil) ⇒ Object

Deletes and recreates index. Supports suffixes. Returns result of index creation.

UsersIndex.purge # deletes and creates users index UsersIndex.purge '01-2014' # deletes users and users_01-2014 indexes, creates users_01-2014



113
114
115
116
117
# File 'lib/chewy/index/actions.rb', line 113

def purge(suffix = nil)
  delete if suffix.present?
  delete suffix
  create suffix
end

#purge!(suffix = nil) ⇒ Object

Deletes and recreates index. Supports suffixes. Returns result of index creation. Raises error in case of unsuccessfull creation

UsersIndex.purge! # deletes and creates users index UsersIndex.purge! '01-2014' # deletes users and users_01-2014 indexes, creates users_01-2014



126
127
128
129
130
# File 'lib/chewy/index/actions.rb', line 126

def purge!(suffix = nil)
  delete if suffix.present? && exists?
  delete suffix
  create! suffix
end

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

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:



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/chewy/index/actions.rb', line 178

def reset!(suffix = nil, apply_journal: true, journal: false, **import_options)
  result = if suffix.present?
    start_time = Time.now
    indexes = self.indexes
    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