为什么弹性搜索不会为关联编制索引

时间:2016-11-15 11:57:26

标签: ruby-on-rails elasticsearch

class Doctor < ActiveRecord::Base

  has_many :doctors_doctor_branches, foreign_key: "doctor_id", dependent: :destroy
  has_many :doctor_branches, through: :doctors_doctor_branches

  def as_indexed_json(options={})
    as_json(
      include: {
        doctor_branches: {only: [:id, :name]}
      }
    )
  end
end

Doctor和DoctorBranch之间的关系是正确的,如

Doctor.find(1).doctor_branches

给我AR数组。但是当我做的时候

response = Doctor.__elasticsearch__.search 'Yoga' 

我没有得到回应的结果。

1 个答案:

答案 0 :(得分:0)

您创建了索引?为此,您需要运行如下代码:

client     = Doctor.gateway.client
index_name = Doctor.index_name
settings = Doctor.settings.to_hash
mappings = Doctor.mappings.to_hash
client.indices.create index: index_name,
                      body: {
                        settings: settings.to_hash,
                        mappings: mappings.to_hash }