Skip to content
Matteo Melani edited this page Jul 17, 2014 · 6 revisions

Usage Notes

Given a model here is how you can control the index composition, attributes that are searchable, and the payload.

For full control on the index composition define the mappings in the model class and pass the option dynamic set to false:

class Article
   include Elasticsearch::Model
   include Elasticsearch::Model::Callbacks
   mappings dynamic: 'false' do
     indexes :title, analyzer: 'english', index_options: 'offsets'
   end
end

Note that the payload will be composed by all the Article attributes because we have not overwritten as_indexed_json. If you set the mappings but the dynamic option is set to true then all the Article attributes will be used to build the index.

To verify this behavior make sure you create the index with Article.__elasticsearch__.create_index! force: true

For full control on the payload overwrite the as_indexed_json method

class Article
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks
  
  def as_indexed_json(options={})
  {
    "title" => title,
    "author_name" => author.name
  }
  end  
end

Note that the as_indexed_json must return an Hash where the keys are strings.

Clone this wiki locally