先决条件:
class Customer < ActiveRecord::Base
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
end
问题:为模型建立索引的正确方法是什么?
我的假设:
bundle exec rake environment elasticsearch:import:model CLASS='Customer'
够了吗?即使我更改了模型的映射或添加了新字段,此方法也能正常工作吗?
最佳答案
添加elasticsearch做现有模型:
callbacks module
将为所有新记录建立索引或更新您已添加的现有记录。include Elasticsearch::Model::Callbacks
as_indexed_json
上定义以下方法来指定要在该模型中建立索引的字段def as_indexed_json(options={})
as_json(
only: [:first_name, :last_name, :address],
include: [products: { only: :name }]
)
end
This is a very good article explainging everything,当然每次需要索引新字段时,都需要更新
as_indexed_json
方法并运行rake任务。再次重建索引。 import
调用model
方法来为该模型重新索引或创建新索引。 关于ruby-on-rails - 将Elasticsearch索引添加到现有的Rails应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46513458/