gpt4 book ai didi

ruby-on-rails - sunspot solr如何正确搜索多个模型?在线所有示例均失败

转载 作者:行者123 更新时间:2023-12-04 16:31:40 27 4
gpt4 key购买 nike

如何在 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
这失败了:
 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]) 行然后它尝试
然后它尝试加载
view app/views/educators/educator.html.haml 
不存在的(我只使用
/app/views/profiles/_profile.html.haml 
显示个人资料
编辑#1:
什么是 ruby​​ on rails 中使用 sunspot 和 solr 以更高级的方式查看的好的开源项目?也许我可以在那里找到答案。如果这个方向的任何答案产生了这个问题,也将被接受赏金,谢谢!

最佳答案

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

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



但是,您的搜索说:

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



因为都没有 Profile也不是 Match在各自的 searchable 中包含所有这些字段块,没有单个记录可以匹配您指定的条件。

如果我对您上面的预期行为是正确的,那么您需要在配置文件的 searchable 中包含配置文件的关联匹配信息。块,像这样:
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 在对配置文件进行索引时如何填充这些值。

这将允许您仅使用 Profile 来编写搜索。模型:
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记录,同时仍然反射(reflect)每个配置文件的匹配关联的属性,因为我们在配置文件索引时存储了它们。

请注意,这会增加您为模型编制索引时的复杂性。如果 Match记录更改,其关联的配置文件现在需要重新索引以反射(reflect)这些更改。

关于ruby-on-rails - sunspot solr如何正确搜索多个模型?在线所有示例均失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17300137/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com