原文链接 : https://www.elastic.co/guide/en/elasticsearch/reference/5.4/returning-aggregation-type.html

    译文链接 : Returning the type of the aggregation(返回聚合的类型)

    贡献者 : 程威ApacheCNApache中文网

    有的时候你需要知道聚合的确切类型才能够解析得到的结果。typed_keys参数可用于在响应中更改聚合名称,以至于可以以内部类型为前缀。

    考虑下面的名为tweets_over_time的 date_histogram 聚合,其中有一个名为top_users的子’top_hits`聚合:

    1. curl -XGET 'localhost:9200/twitter/tweet/_search?typed_keys&pretty' -H 'Content-Type: application/json' -d'
    2. {
    3. "aggregations": {
    4. "tweets_over_time": {
    5. "date_histogram": {
    6. "field": "date",
    7. "interval": "year"
    8. },
    9. "aggregations": {
    10. "top_users": {
    11. "top_hits": {
    12. "size": 1
    13. }
    14. }
    15. }
    16. }
    17. }
    18. }
    19. '

    请求返回中,聚合名称将分别更改为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" : 5,
                                "max_score" : 1.0,
                                "hits" : [
                                    {
                                      "_index": "twitter",
                                      "_type": "tweet",
                                      "_id": "0",
                                      "_score": 1.0,
                                      "_source": {
                                        "date": "2009-11-15T14:12:12",
                                        "message": "trying out Elasticsearch",
                                        "user": "kimchy",
                                        "likes": 0
                                      }
                                    }
                                ]
                            }
                        }
                    }
                ]
            }
        },
        ...
    }
    

    | 1 | tweets_over_time 名称包含 date_histogram 前缀 |
    | 2 | top_users 包含 top_hits 前缀 |

    Note

    对于某些聚合,返回类型可能与请求提供的类型不同。这是对于Terms,重要的的Terms和百分比聚合,其中返回的类型还包含有关目标字段类型的信息:lterms(用于长字段中的术语聚合),sigsterms(对于String上的重要术语聚合字段),tdigest_percentiles(基于TDigest算法的百分点聚合)。