且构网

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

根据一个字段值过滤 elasticsearch 结果以仅包含唯一文档

更新时间:2022-12-20 11:53:07

您需要一个 top_hits 聚合.

You need a top_hits aggregation.

对于您的具体情况:

{
  "query": {
    "multi_match": {
      ...
    }
  },
  "aggs": {
    "top-uids": {
      "terms": {
        "field": "uid"
      },
      "aggs": {
        "top_uids_hits": {
          "top_hits": {
            "sort": [
              {
                "_score": {
                  "order": "desc"
                }
              }
            ],
            "size": 1
          }
        }
      }
    }
  }
}

上面的查询确实执行您的 multi_match 查询并根据 uid 聚合结果.对于每个 uid 存储桶,它只返回一个结果,但是在存储桶中的所有文档都根据 _score 按降序排序之后.

The query above does perform your multi_match query and aggregates the results based on uid. For each uid bucket it returns only one result, but after all the documents in the bucket were sorted based on _score in descendant order.