Rails with Sunspot 添加 Child 模型属性

Rails with Sunspot add Child Model Attributes

所以,我已经弄乱了这个问题几个小时,我想我已经尝试了关于这个问题的所有内容。这是我的第一个问题。

所以,我正在尝试搜索模型(位置):branch_name、:branch_address1 等,以及他们使用的 Child 模型(员工):first_name在该模型中作为它自己的属性。因此,如果位置员工姓名在搜索查询中,则期望的效果将是位置(作为结果)将 return。我正在尝试使用如下代码实现此目的:

class Location < ActiveRecord::Base
  belongs_to :partner
  has_many :employees, dependent: :destroy

  validates_presence_of :branch_name, :branch_address1, :branch_country, :branch_zip_code
  accepts_nested_attributes_for :employees, allow_destroy: true, :reject_if > proc { |attributes| attributes['first_name'].blank? }

  searchable do
    text :branch_name, :branch_address1, :branch_city, :branch_zip_code
    text :employee_attributes do
      :first_name
      if :employee_attributes
      end
    end
  end

end

我也想使用 parent 模型(合作伙伴)名称,但我也无法使用它。

我可以在位置 table 中创建一个新列来捕获每个新员工的姓名,还是可以在该模型中搜索 children?

我在rails用过solr,遇到过类似的问题。 就像你问的关于新专栏的问题,我已经向 solr

添加了一个新字段

pseudo code

searchable do
 text :first_name, :stored => true
 string  :partner_name, :stored => true do
   partner.name
 end
end

请注意,存储字段会增加成本。

在我的例子中,我不得不触发多个查询,耗时 47 秒,存储值帮助我避免了多个查询,单个查询耗时 5 秒。

您需要重新编制索引才能获得结果。