Elasticsearch:如何搜索,排序,限制结果然后再次排序? [英] Elasticsearch: How to search, sort, limit the results then sort again?

查看:71
本文介绍了Elasticsearch:如何搜索,排序,限制结果然后再次排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与多级排序无关.

我需要先根据距离选择结果,将其限制为50,然后再按价格对这50个结果进行排序.

I need my results first selected by distance, limited to 50, then those 50 sorted by price.

select * 
from
(
select top 50 * from mytable order by distance asc)
)
order by price asc

本质上,第二种排序放弃了内部排序的顺序-但是内部排序用于磨练前50个结果.

Essentially, the second sort throws away the ordering of the inner sort - but the inner sort is used to hone in on the top 50 results.

针对此类问题,我看到的其他答案是第二级排序,这不是我想要的.

The other answers I've seen for this sort of question looks at second-level sorting, which is not what I'm after.

顺便说一句:我看过汇总-前N个结果,但是我不确定我可以对汇总结果排序应用排序.还查看了 rescore ,但我不知道将排序"放在哪里

BTW: I've looked at aggregations - Top N results, but I'm not sure I can apply a sort on the aggregation result sort. Also looked at rescore, but I don't know where to put my 'sorts'

推荐答案

通过热门匹配,您可以根据主要查询的排序方式在单独的字段( price )上进行排序(在距离).在此处参阅文档,了解如何在排名靠前的agg中指定排序方式.

A top hits aggregation will allow you to sort on a separate field, in your case price from the main query sort (on distance). See the documentation here for how to specify sorting in the top hits agg.

它看起来有点像这样(假定 distance 是一种双精度型;如果是地理位置类型,请使用

It'll look a little like this (which assumes distance is a double type; if it's a geo-location type, use the documentation provided by Volodymyr Bilyachat.)

{
   "sort":[
      {
         "distance":"asc"
      }
   ],
   "query":{
      "match_all":{}
   },
   "size":50,
   "aggs":{
      "top_price_hits":{
         "top_hits":{
            "sort":[
               {
                  "price":{
                     "order":"asc"
                  }
               }
            ],
            "size":50
         }
      }
   }
}

但是,如果主要查询只需要50个结果,为什么不只在应用程序客户端排序呢?这将是一种更好的方法,因为将热门匹配用作次要排序是对其目的的轻微滥用.

However, if there are only 50 results that you want from your primary query, why don't you just sort in the application client side? This would be a better approach as using a top hits aggregation for a secondary sort is a slight abuse of its purpose.

应用程序内方法将更加健壮.

The in-application approach would be more robust.

这篇关于Elasticsearch:如何搜索,排序,限制结果然后再次排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆