太阳黑子配置文件,匹配搜索关联的模型属性结果

时间:2013-10-09 13:19:26

标签: ruby-on-rails solr sunspot associated-object

想要轻松搜索所有相关的模型属性 在被问到之前仍有问题:

个人资料模型

has_one :match

searchable do
  integer       :id
  string        :country
  string        :state
  string        :city
end

匹配模型

belongs_to :profile

searchable do
  integer :id
  string :looking_for_education do
   match.looking_for_education
  end      
  integer :age_from
  integer :age_to
end

ProfilesController#索引

def index

  @search = Sunspot.search Profile do

    with(:country, params[:country]) # this is a profile attribute
    with(:state,   params[:state])   # this is a profile attribute   
    with(:looking_for_education, "high school") # this should search *inside* 
                                                #the match attribute's, 
                                                #where **match** belongs_to 
                                                #**profile**
  end

  @profiles = @search.results

end

编辑#1

用第一个答案建议重新编写可搜索的块,其中包含:looking_for_education do block。仍未通过#

的未定义方法`looking_for'

为索引添加整数:id仍然是同一个问题:(

2 个答案:

答案 0 :(得分:1)

问题是您尝试同时搜索ProfileMatch,但模型会被编入索引,而Sunspot.search Profile do仅搜索Profile文档。

您需要configure the documents在一个文档中包含所需的所有信息。一种方法是将Profile作为包含所有信息的文档:

class Profile
  has_one :match

  searchable do
    string :country
    string :state
    string :city
    string :looking_for_education do
      match.looking_for_education
    end
  end

答案 1 :(得分:0)

的解决方案:

我终于找到了问题,我的开发数据库中有一些问题,其中Profile没有匹配。 +匹配表中缺少一些profile_id,修好后再重新编好了。