加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 非关键字搜索的Solr(太阳黑子)查询时间提升

发布时间:2020-12-17 02:36:45 所属栏目:百科 来源:网络整理
导读:鉴于我想找到20个相关结果,我将如何提升any_of中的第一个条件(使用(:id).any_of(co_author_ids)),这样如果有20个符合所述条件的结果,它将返回而不是尝试根据第二个标准进行匹配? @solr_search = User.solr_search do paginate(:per_page = 20) with(:has_e
鉴于我想找到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

最初我并不认为增强是必要的,因为我认为any_of会产生级联效应,但它看起来并不像那样.我知道在查询关键字和全文搜索时会对查询时间进行提升,但却无法使用with()方法.

解决方法

由于co_author_ids是一个多值密钥,我有足够的理由相信没有办法实现这一点.虽然使用单值键,但可以通过使用函数查询使用solr排序来实现此级联效果. http://wiki.apache.org/solr/FunctionQuery#Sort_By_Function aong with 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

现在在这种情况下你想要一个级联效果,并希望更多地优先考虑与author_id完全匹配,你可以这样做

@solr_search = User.solr_search do
  paginate(:per_page => 20)
  with(:has_email,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

因此,这将基于if(author_id_i =#{id},0)的值进行排序,并且作为回报,将把auhtor_id的所有记录与用户的顶部相同.

我不知何故使用IF函数遇到问题所以我改为使用(实际上它们都是相同的):

@solr_search = User.solr_search do
  paginate(:per_page => 20)
  with(:has_email,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,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 [“sort”]做很多很酷的事情但是在这种特殊情况下它不能完成(imho)因为它是一个多值字段
例如:
Using multivalued field in map function
Solr function query that operates on count of multivalued field

我希望他们可以为多值字段提供一个包含函数,我们可以写
p [“sort”] =“if(include(co_authors_ids,#{id}),0)desc”

但截至目前,这是不可能的(再次imho).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读