太阳黑子solr如何正确搜索多个模型?所有在线示例均失败 [英] sunspot solr how to search multiple models correctly? All examples online fail

查看:116
本文介绍了太阳黑子solr如何正确搜索多个模型?所有在线示例均失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在SunSpot Solr中正确搜索多个模型?

How would one correctly search multiple models in SunSpot Solr?

个人资料模型

has_one :match

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

匹配模型

belongs_to :profile

searchable do
  string :looking_for_education
  integer :age_from
  integer :age_to
end

ProfilesController#Index

def index
  
  @search = Sunspot.search Profile, Match do

    with(:country, params[:country])
    with(:state,   params[:state])      
    with(:looking_for_education, params[:looking_for_education]) <= from the 2nd model
  end

  @profiles = @search.results

end

此操作失败,并显示以下信息:

This fails with:

 Using a with statement like 
  with(:age).between(params[:age_from]..params[:age_to])
 undefined method `gsub' for nil:NilClass

删除
with(:age).between(params [:age_from] .. params [:age_to])行,然后尝试

Removing the
with(:age).between(params[:age_from]..params[:age_to]) line then it tries to

然后尝试加载

view app/views/educators/educator.html.haml 

不存在(我仅使用

/app/views/profiles/_profile.html.haml 

显示个人资料

在红宝石上有哪些好的开源项目,可以使用黑子和solr进行更高级的研究?也许我可以在那里找到答案.如果这导致此问题,那么任何朝着这个方向的答案都将被赏金!

What are good opensource projects in ruby on rails that use sunspot and solr in a bit more advanced way to have a look at? Maybe I can find the answer there. Any answer in this direction will also be accepted the bounty if it yields in resulting this issue, thx!

推荐答案

找到的用于搜索多个模型的方法是正确的.但是,看来您搜索的含义不是您想要的.似乎您要说的是:

The method you've found for searching multiple models is correct. However, it appears that the meaning of your search is not what you intended. It looks as if you're trying to say:

给我所有具有这些countrystate值的Profile记录,,其Match记录具有该looking_for_education

Give me all Profile records with these country and state values, and whose Match record has this looking_for_education value

但是,您的搜索显示:

为我提供所有Profile Match类型的记录,这些记录具有全部的这些countrystatelooking_for_education

Give me all records of type Profile or Match that have all of these country, state and looking_for_education values

因为ProfileMatch都没有在其各自的searchable块中具有所有这些字段,所以没有单个记录可以匹配您指定的条件.

Because neither Profile nor Match have all of these fields in their respective searchable blocks, no single record can match the conditions you specify.

如果我对您的上述预期行为是正确的,则需要在配置文件的searchable块中包括与配置文件相关的匹配信息,如下所示:

If I'm correct about your intended behaviour above, then you need to include the profile's associated match information in the profile's searchable block, like so:

class Profile < ActiveRecord::Base
  has_one :match

  searchable do
    string(:country)
    string(:state)
    string(:city)
    string(:looking_for_education) { match.looking_for_education }
    integer(:age_from)             { match.age_from              }
    integer(:age_to)               { match.age_to                }
  end
end

在这里,我们已经告诉Sunspot对配置文件的匹配关联的属性建立索引,就好像它们位于配置文件本身上一样.在各个块中,我们告诉Sunspot如何在对配置文件建立索引后填充这些值.

Here, we've told Sunspot to index properties of the profile's match association as if they lived on the profile itself. In the respective blocks, we've told Sunspot how to populate these values when the profile is indexed.

这将允许您仅使用Profile模型进行搜索:

This will allow you to write your search using only the Profile model:

def index
  @search = Sunspot.search Profile do
    with(:country, params[:country])
    with(:state,   params[:state])      
    with(:looking_for_education, params[:looking_for_education])
    with(:age).between(params[:age_from]..params[:age_to])
  end

  @profiles = @search.results
end

此搜索将仅返回Profile条记录,同时仍反映每个配置文件的匹配关联的属性,因为我们在对配置文件建立索引时将它们存储了.

This search will return only Profile records, while still reflecting the properties of each profile's match association, because we stored them when the profile was indexed.

请注意,当您建立模型索引时,这会增加复杂性.如果Match记录发生更改,则现在需要重新索引其关联的配置文件以反映这些更改.

Note that this increases complexity when you index your models. If a Match record changes, its associated profile now needs to be reindexed to reflect those changes.

这篇关于太阳黑子solr如何正确搜索多个模型?所有在线示例均失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆