bru*_*upm 4 search solr ruby-on-rails sunspot
鉴于我想找到20个相关结果,我将如何提升any_of中的第一个条件(使用(:id).any_of(co_author_ids)),这样如果有20个符合所述条件的结果,它将返回而不是尝试根据第二个标准进行匹配?
@solr_search = User.solr_search do
paginate(:per_page => 20)
with(:has_email, true)
any_of do
with(:id).any_of(co_author_ids)
with(:hospitals_id).any_of(hopital_ids)
end
end
Run Code Online (Sandbox Code Playgroud)
最初我并不认为增强是必要的,因为我认为any_of会产生级联效应,但它看起来并不像那样.我知道在查询关键字和全文搜索时会对查询时间进行提升,但是无法使用with()方法.
由于co_author_ids是一个多值密钥,我有足够的理由相信没有办法实现这一点.虽然使用单值键,但可以通过使用函数查询使用solr排序来实现此级联效果.http://wiki.apache.org/solr/FunctionQuery#Sort_By_Function与adjust_solr-params http://sunspot.github.io/docs/Sunspot/DSL/Adjustable.html
示例:假设您有这样的查询:
@solr_search = User.solr_search do
paginate(:per_page => 20)
with(:has_email, true)
any_of do
with(:id,author_id) #assuming author id is a solr index
with(:hospitals_id).any_of(hopital_ids)
end
end
Run Code Online (Sandbox Code Playgroud)
现在在这种情况下你想要一个级联效果,并希望更多地优先考虑与author_id完全匹配,你可以这样做
@solr_search = User.solr_search do
paginate(:per_page => 20)
with(:has_email, true)
any_of do
with(:id,author_id) #assuming author id is a solr index
with(:hospitals_id).any_of(hopital_ids)
end
adjust_solr_params do |p|
p["sort"] = "if(author_id_i = #{id},1,0) desc" #note author_id_i solr eq of author_id
end
end
Run Code Online (Sandbox Code Playgroud)
因此,这将基于if(author_id_i =#{id},1,0)的值进行排序,并且作为回报,将所有与auhtor_id的记录放在最顶层的用户相同.
我不知何故使用IF函数遇到问题所以我改为使用(实际上它们都是相同的):
@solr_search = User.solr_search do
paginate(:per_page => 20)
with(:has_email, true)
any_of do
with(:id,author_id) #assuming author id is a solr index
with(:hospitals_id).any_of(hopital_ids)
end
adjust_solr_params do |p|
p[:sort] = "min(abs(sub(author_id_i,#{id})),1) asc"
end
end
Run Code Online (Sandbox Code Playgroud)
我也偶然发现了http://wiki.apache.org/solr/SpatialSearch,同时寻找一个解决方案,如果你想按距离排序,你可以这样做:
@solr_search = User.solr_search do
paginate(:per_page => 20)
with(:has_email, true)
any_of do
with(:id,author_id) #assuming author id is a solr index
with(:hospitals_id).any_of(hopital_ids)
end
adjust_solr_params do |p|
p[:pt] = "#{latitude_of_your_interest},#{longitude_of_your_interest}"
p[:sfield] = :author_location #your solr index which stores location of the author
p[:sort] = "geodist() asc"
end
end
Run Code Online (Sandbox Code Playgroud)
总的来说,我会说你可以用p ["sort"]做很多很酷的事情但是在这种特殊情况下它不能完成(imho)因为它是一个多值字段ex: 在map函数中使用多值字段 Solr函数查询操作关于多值字段的计数
我希望他们可以为多值字段提供一个包含函数,我们可以写
p["sort"] ="if(include(co_authors_ids,#{id}), 1, 0) desc"
但截至目前,这是不可能的(再次imho).