-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Given a model here is how you can control the index composition (the attributes that are searchable), and the payload (the document that is returned with a search result).
Assume you have the following schema.rb
ActiveRecord::Schema.define(version: 20140715234531) do
create_table "articles", force: true do |t|
t.string "title"
t.text "content"
t.date "published_on"
t.datetime "created_at"
t.datetime "updated_at"
end
end
For full control on the index composition define the mappings in the model class and pass the option dynamic set to false:
class Article < ActiveRecord::Base
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
mappings dynamic: 'false' do
indexes :title, type: 'string'
end
end
Note that the payload will be composed by all the Article attributes because we have not overwritten as_indexed_json. If you define 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< ActiveRecord::Base
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.