3.6.8.返回聚合的类型(Returning the type of the aggregation)

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

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

curl -XGET 'localhost:9200/twitter/tweet/_search?typed_keys&pretty' -H 'Content-Type: application/json' -d'
{
  "aggregations": {
    "tweets_over_time": {
      "date_histogram": {
        "field": "date",
        "interval": "year"
      },
      "aggregations": {
        "top_users": {
            "top_hits": {
                "size": 1
            }
        }
      }
    }
  }
}
'

请求返回中,聚合名称将分别更改为date_histogram#tweets_over_time和top_hits#top_users,反映每个聚合的内部类型:

{
    "aggregations": {
        "date_histogram#tweets_over_time": { #1
            "buckets" : [
                {
                    "key_as_string" : "2009-01-01T00:00:00.000Z",
                    "key" : 1230768000000,
                    "doc_count" : 5,
                    "top_hits#top_users" : {  # 2
                        "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算法的百分点聚合)。

Last updated