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

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'


184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/elasticsearch/model/indexing.rb', line 184

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

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

  unless ( self.client.indices.exists(index: target_index) rescue false )
    self.client.indices.create index: target_index,
                               body: {
                                 settings: self.settings.to_hash,
                                 mappings: self.mappings.to_hash }
  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'


207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/elasticsearch/model/indexing.rb', line 207

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]
      STDERR.puts "[!!!] Index does not exist (#{e.class})"
    else
      raise e
    end
  end
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 defines 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"}}}}


133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/elasticsearch/model/indexing.rb', line 133

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

  if block_given?
    @mapping.options.update(options)

    @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:



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

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]
      STDERR.puts "[!!!] Index does not exist (#{e.class})"
    else
      raise e
    end
  end
end

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

Define settings for the index

Examples:

Define index settings


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

Article.settings.to_hash

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


156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/elasticsearch/model/indexing.rb', line 156

def settings(settings={}, &block)
  @settings ||= Settings.new(settings)

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

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