Module: Elasticsearch::Model::Indexing::ClassMethods

Included in:
Proxy::ClassMethodsProxy
Defined in:
lib/elasticsearch/model/indexing.rb

Instance Method Summary collapse

Instance Method Details

#create_index!(options = {}) ⇒ Object

Creates an index with correct name, automatically passing ‘settings` and `mappings` defined in the model

Examples:

Create an index for the ‘Article` model


Article.__elasticsearch__.create_index!

Forcefully create (delete first) an index for the ‘Article` model


Article.__elasticsearch__.create_index! force: true

Pass a specific index name


Article.__elasticsearch__.create_index! index: 'my-index'


232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/elasticsearch/model/indexing.rb', line 232

def create_index!(options={})
  options = options.clone

  target_index = options.delete(:index)    || self.index_name
  settings     = options.delete(:settings) || self.settings.to_hash
  mappings     = options.delete(:mappings) || self.mappings.to_hash

  delete_index!(options.merge index: target_index) if options[:force]

  unless index_exists?(index: target_index)
    options.delete(:force)
    self.client.indices.create({ index: target_index,
                                 body: {
                                   settings: settings,
                                   mappings: mappings }
                               }.merge(options))
  end
end

#delete_index!(options = {}) ⇒ Object

Deletes the index with corresponding name

Examples:

Delete the index for the ‘Article` model


Article.__elasticsearch__.delete_index!

Pass a specific index name


Article.__elasticsearch__.delete_index! index: 'my-index'


277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/elasticsearch/model/indexing.rb', line 277

def delete_index!(options={})
  target_index = options.delete(:index) || self.index_name

  begin
    self.client.indices.delete index: target_index
  rescue Exception => e
    if e.class.to_s =~ /NotFound/ && options[:force]
      client.transport.logger.debug("[!!!] Index does not exist (#{e.class})") if client.transport.logger
      nil
    else
      raise e
    end
  end
end

#index_exists?(options = {}) ⇒ Boolean

Returns true if the index exists

Examples:

Check whether the model’s index exists


Article.__elasticsearch__.index_exists?

Check whether a specific index exists


Article.__elasticsearch__.index_exists? index: 'my-index'

Returns:

  • (Boolean)


261
262
263
264
265
# File 'lib/elasticsearch/model/indexing.rb', line 261

def index_exists?(options={})
  target_index = options[:index] || self.index_name

  self.client.indices.exists(index: target_index, ignore: 404)
end

#load_settings_from_io(settings) ⇒ Object



213
214
215
# File 'lib/elasticsearch/model/indexing.rb', line 213

def load_settings_from_io(settings)
  YAML.load(settings.read)
end

#mapping(options = {}, &block) ⇒ Object Also known as: mappings

Defines mappings for the index

The ‘mappings` and `settings` methods are accessible directly on the model class, when it doesn’t already define them. Use the ‘__elasticsearch__` proxy otherwise.

Examples:

Define mapping for model


class Article
  mapping dynamic: 'strict' do
    indexes :foo do
      indexes :bar
    end
    indexes :baz
  end
end

Article.mapping.to_hash

# => { :article =>
#        { :dynamic => "strict",
#          :properties=>
#            { :foo => {
#                :type=>"object",
#                :properties => {
#                  :bar => { :type => "string" }
#                }
#              }
#            },
#           :baz => { :type=> "string" }
#        }
#    }

Define index settings and mappings


class Article
  settings number_of_shards: 1 do
    mappings do
      indexes :foo
    end
  end
end

Call the mapping method directly


Article.mapping(dynamic: 'strict') { indexes :foo, type: 'long' }

Article.mapping.to_hash

# => {:article=>{:dynamic=>"strict", :properties=>{:foo=>{:type=>"long"}}}}


145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/elasticsearch/model/indexing.rb', line 145

def mapping(options={}, &block)
  @mapping ||= Mappings.new(options)

  @mapping.options.update(options) unless options.empty?

  if block_given?
    @mapping.instance_eval(&block)
    return self
  else
    @mapping
  end
end

#refresh_index!(options = {}) ⇒ Object

Performs the “refresh” operation for the index (useful e.g. in tests)

Examples:

Refresh the index for the ‘Article` model


Article.__elasticsearch__.refresh_index!

Pass a specific index name


Article.__elasticsearch__.refresh_index! index: 'my-index'

See Also:



304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/elasticsearch/model/indexing.rb', line 304

def refresh_index!(options={})
  target_index = options.delete(:index) || self.index_name

  begin
    self.client.indices.refresh index: target_index
  rescue Exception => e
    if e.class.to_s =~ /NotFound/ && options[:force]
      client.transport.logger.debug("[!!!] Index does not exist (#{e.class})") if client.transport.logger
      nil
    else
      raise e
    end
  end
end

#settings(settings = {}, &block) ⇒ Object

Define settings for the index

You can read settings from any object that responds to :read as long as its return value can be parsed as either YAML or JSON.

Examples:

Define index settings


Article.settings(index: { number_of_shards: 1 })

Article.settings.to_hash

# => {:index=>{:number_of_shards=>1}}

Define index settings from YAML file


# config/elasticsearch/articles.yml:
#
# index:
#   number_of_shards: 1
#

Article.settings File.open("config/elasticsearch/articles.yml")

Article.settings.to_hash

# => { "index" => { "number_of_shards" => 1 } }

Define index settings from JSON file


# config/elasticsearch/articles.json:
#
# { "index": { "number_of_shards": 1 } }
#

Article.settings File.open("config/elasticsearch/articles.json")

Article.settings.to_hash

# => { "index" => { "number_of_shards" => 1 } }


199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/elasticsearch/model/indexing.rb', line 199

def settings(settings={}, &block)
  settings = YAML.load(settings.read) if settings.respond_to?(:read)
  @settings ||= Settings.new(settings)

  @settings.settings.update(settings) unless settings.empty?

  if block_given?
    self.instance_eval(&block)
    return self
  else
    @settings
  end
end