原英文版地址: https://www.elastic.co/guide/en/elasticsearch/reference/7.7/returning-aggregation-type.html, 原文档版权归 www.elastic.co 所有
本地英文版地址: ../en/returning-aggregation-type.html
本地英文版地址: ../en/returning-aggregation-type.html
重要: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 当前版本文档。
有时需要知道聚合的确切类型,以便解析结果。
参数typed_keys
可用于更改响应中的聚合名称,以便以其内部类型作为前缀。
考虑以下名为tweets_over_time
的日期直方图(date_histogram)
聚合,它有一个名为top_users
的子top_hits聚合:
GET /twitter/_search?typed_keys { "aggregations": { "tweets_over_time": { "date_histogram": { "field": "date", "calendar_interval": "year" }, "aggregations": { "top_users": { "top_hits": { "size": 1 } } } } } }
在响应中,聚合的名称将分别更改为date_histogram#tweets_over_time
和top_hits#top_users
,以反映每个聚合的内部类型:
{ "aggregations": { "date_histogram#tweets_over_time": { "buckets" : [ { "key_as_string" : "2009-01-01T00:00:00.000Z", "key" : 1230768000000, "doc_count" : 5, "top_hits#top_users" : { "hits" : { "total" : { "value": 5, "relation": "eq" }, "max_score" : 1.0, "hits" : [ { "_index": "twitter", "_type": "_doc", "_id": "0", "_score": 1.0, "_source": { "date": "2009-11-15T14:12:12", "message": "trying out Elasticsearch", "user": "kimchy", "likes": 0 } } ] } } } ] } }, ... }
对于某些聚合,返回的类型可能与请求中提供的类型不同。
terms、significant_terms 和 percentiles(百分位数)聚合就是这种情况,其中返回的类型还包含有关目标字段类型的信息:
lterms
(用于长整形字段上的词项聚合),
sigsterms
(用于字符串字段上的重要词项聚合),
tdigest_percentiles
(用于基于 TDigest 算法的百分位数聚合)。