Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
Berkhan Berkdemir edited this pageJul 11, 2018 ·7 revisions

Usage Notes

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)docreate_table"articles",force:truedo |t|t.string"title"t.text"content"t.date"published_on"t.datetime"created_at"t.datetime"updated_at"endend

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

classArticle <ActiveRecord::BaseincludeElasticsearch::ModelincludeElasticsearch::Model::Callbacksmappingsdynamic:'false'doindexes:title,type:'string'endend

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 withArticle.__elasticsearch__.create_index! force: true

For full control on the payload overwrite the as_indexed_json method

classArticle <ActiveRecord::BaseincludeElasticsearch::ModelincludeElasticsearch::Model::Callbacksdefas_indexed_json(options={}){"title"=>title,"author_name"=>author.name}endend

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

Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp