且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Elasticsearch排除字段值最高的问题

更新时间:2023-02-19 21:09:30

我认为您可以摆脱排序通过 top_hits 中的两个字段:通过活动和通过已收集。基本上,您希望 true 首先出现且相等时,然后按 collected 进行排序。诸如此类的内容始终会显示 active:true 文档,该文档按收集的排序。

I think you can get away with sorting by two fields in your top_hits: by active and by collected. Basically, you want trues to be first and when equal, then sort by collected. Something like the following will always show the active:true documents sorted by collected.

此解决方案的唯一缺点是,如果您没有任何活动文档,则 top_hits 将显示一个活动文档:错误的文档。

The only downside to this solution is that if you don't have any active documents, top_hits will show one active:false document.

{
  "size": 0,
  "aggs": {
    "group": {
      "terms": {
        "field": "country"
      },
      "aggs": {
        "group_docs": {
          "top_hits": {
            "size": 1,
            "sort": [
              {
                "active": {
                  "order": "desc"
                }, 
                "collected": {
                  "order": "desc"
                }
              }
            ]
          }
        }
      }
    }
  }
}