Method: Elasticsearch::Model::Indexing::ClassMethods#mapping

Defined in:
lib/elasticsearch/model/indexing.rb

#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