elasticsearch-aggs-top_hits,分组查询,并返回每组数据中特定排序的源数据

对数据分组并返回分组中特定排序的多少个

Top Hits Aggregation

  • top_hits指标聚合器跟踪正在聚合的最相关文档。 此聚合器旨在用作子聚合器,以便可以按桶聚合最匹配的文档。
  • top_hits聚合器可以有效地用于通过桶聚合器按特定字段对结果集进行分组。 一个或多个存储桶聚合器确定结果集被切入的属性。

选项

  • from - 要获取的第一个结果的偏移量。
  • size - 每个桶返回的最大匹配匹配数的最大数量。默认情况下,返回前三个匹配的匹配。
  • sort - 如何对最匹配的匹配进行排序。默认情况下,命中按主查询的分数排序。

例子

请求:
GET ztjy-growth-info-alpha_*/_search
{"query": {
  "match": {
    "schoolId": "uOdq9KBA7xYpbmvC4Bf"
  }
}, 
  "size": 0,
  "aggs": {
    "userId111": {
      "terms": {
        "field": "userId",
        "size": 10 
      }, 
      "aggs": {
        "publishTime111": {
          "top_hits": {
            "sort": [
              {
                "publishTime": {
                  "order": "desc"
                }
              }
            ],
            "size": 1,
             "_source": {
              "includes": ["publishTime","feedId"]
            }
          }
        }
      }
    }
  }
}
结果
{
  "took": 17,
  "timed_out": false,
  "_shards": {
    "total": 20,
    "successful": 20,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 555,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "userId111": {
      "doc_count_error_upper_bound": 6,
      "sum_other_doc_count": 187,
      "buckets": [
        {
          "key": "9d43450b8d9d4ce06a10",
          "doc_count": 307,
          "publishTime111": {
            "hits": {
              "total": 307,
              "max_score": null,
              "hits": [
                {
                  "_index": "ztjy-growth-info-alpha_2",
                  "_type": "growth_feed_info",
                  "_id": "10-1494489",
                  "_score": null,
                  "_source": {
                    "publishTime": 1575862793007,
                    "feedId": 1174312823360864300
                  },
                  "sort": [
                    1575862793007
                  ]
                }
              ]
            }
          }
        },
        {
          "key": "49f5874d546638f306c9",
          "doc_count": 61,
          "publishTime111": {
            "hits": {
              "total": 61,
              "max_score": null,
              "hits": [
                {
                  "_index": "ztjy-growth-info-alpha_2",
                  "_type": "growth_feed_info",
                  "_id": "24-1440124",
                  "_score": null,
                  "_source": {
                    "publishTime": 1572959152208,
                    "feedId": 1162134068869742600
                  },
                  "sort": [
                    1572959152208
                  ]
                }
              ]
            }
          }
        }
      ]
    }
  }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值