Ruby on rails 太阳黑子解决方案如何正确搜索多个模型?所有在线示例都失败
如何正确搜索太阳黑子Solr中的多个模型 配置文件模型Ruby on rails 太阳黑子解决方案如何正确搜索多个模型?所有在线示例都失败,ruby-on-rails,ruby-on-rails-3,sunspot-rails,sunspot-solr,Ruby On Rails,Ruby On Rails 3,Sunspot Rails,Sunspot Solr,如何正确搜索太阳黑子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 匹配型号 ha
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
匹配型号
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
档案控制器#索引
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
卸下电源开关使用(: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?也许我可以在那里找到答案。如果这方面的任何答案导致了这个问题,我也会接受赏金,thx!这就是我在搜索多个模型时所做的
Sunspot.search [Model1, Model2] do
....
end
@莫伊斯·萨拉戈萨正确回答了你的问题,但你想做的事情比你想象的要多 第一个错误:
Using a with statement like
with(:age).between(params[:age_from]..params[:age_to])
undefined method `gsub' for nil:NilClass
很可能是因为参数[:age_from]和/或参数[:age_to]为零而生成的。我不能保证这一点,因为您尚未显示堆栈跟踪。只有当它们存在时,您才能通过筛选器进行修复:
带(:age).between(params[:age\u from]…params[:age\u to]),如果params[:age\u from].present?和params[:age\u to].present
第二个错误
与您的视图相关。我假设您正在使用rails帮助程序的一个部分对象帮助程序呈现集合或对象,而没有指定部分对象(同样,没有代码,这是一个比其他任何东西都好的猜测):
或
当Rails没有指定分部时,它会根据对象类型猜测分部名称。在这种情况下,如果您的对象是例如类Educator(可能是Profile的子类),Rails将查找分部“educators/\u Educator.html.erb”。请确保根据对象类型和此属性呈现正确的分部将确保您呈现所需内容。您找到的用于搜索多个模型的方法是正确的。但是,您搜索的含义似乎与您的意图不符。您似乎在试图说: 给我所有的
Profile
记录,包括这些country
和state
值,和匹配的记录有此查找教育值
但是,您的搜索显示:
给我所有类型为Profile
或Match
的记录,其中包含国家
、州
和查找教育
值
由于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
此搜索将仅返回Profile
记录,同时仍然反映每个概要文件的匹配关联的属性,因为我们在对概要文件编制索引时存储了这些记录
请注意,在为模型编制索引时,这会增加复杂性。如果匹配项记录了更改,则现在需要重新为其关联的配置文件编制索引以反映这些更改。这里是使用with
在匹配字符串上搜索不同模型的答案
searchable(:auto_index => AppConfig.solr.auto_index) do
string :category_name, :stored => true
text :content, :stored => true
text :title
string :company_id, :stored => true
time :published_on
end
search do |q|
if params[:keyword].present?
q.fulltext params[:keyword] do
fields(:deal_data)
end
end
if (ids = params["company_id"]).present?
ids = ids.split(",")
q.with(:company_id,ids) #here company id formate should be ["***","***"]
end
end
你能告诉我错误的细节吗,我是说在哪个文件,哪一行?@Muntasim谢谢你的评论,我更新了帖子,对此我很抱歉。你知道为什么会发生这种情况以及如何正确处理吗?这对我来说还是失败字符串:查找教育{match.looking_查找教育}语法不正确
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
searchable(:auto_index => AppConfig.solr.auto_index) do
string :category_name, :stored => true
text :content, :stored => true
text :title
string :company_id, :stored => true
time :published_on
end
search do |q|
if params[:keyword].present?
q.fulltext params[:keyword] do
fields(:deal_data)
end
end
if (ids = params["company_id"]).present?
ids = ids.split(",")
q.with(:company_id,ids) #here company id formate should be ["***","***"]
end
end