因为co_author_ids是一个多值键,我有足够的理由相信没有办法做到这一点。尽管使用单值键可以通过使用函数查询使用solr排序来实现这种级联效果。 http://wiki.apache.org/solr/FunctionQuery#Sort_By_Function与adjust_solr-PARAMS aong 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
,现在在这种情况下,你想有一个连锁效应,并希望给予更多的优惠,以精确的匹配与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
所以这将排序,如果值的基础上(author_id_i =#(编号),1,0),作为回报,将会把所有的记录与auhtor_id为相同的用户在上面。
不知何故,我是越来越使用问题IF函数,所以我代替(practicaaly两者相同):
@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
我偶然发现了这也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
总的来说,我会说你可以做很多很酷的事情p [“排序”],但在这种特殊情况下,它不能做(恕我直言),因为它是多值字段 例如: Using multivalued field in map function Solr function query that operates on count of multivalued field
我想他们可能只是提供了针对多领域包括功能,我们可以只写 p["sort"] ="if(include(co_authors_ids,#{id}), 1, 0) desc"
但截至目前其不可能(再次恕我直言)
你碰巧得到了一个解决方案,因为这正是我所期待的。 – 2013-07-30 07:45:27
我没有找到Solr的解决方案。我结束了移动到Elasticsearch搜索和使用恒定分数查询:http://www.elasticsearch.org/guide/reference/query-dsl/constant-score-query/ – brupm 2013-07-30 16:01:06
耶在这个特殊的情况下solr失败,但如果你想要在单值索引索引上实现这种级联效果,那么可以通过在solr params中使用函数进行排序来完成。如果我的答案中有答案,以防其他人浪费时间用solr实现这一点:) – 2013-07-31 14:46:05