返回聚合的类型

有时需要知道聚合的确切类型,以便解析结果。 参数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_timetop_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
                                  }
                                }
                            ]
                        }
                    }
                }
            ]
        }
    },
    ...
}

名称tweets_over_time现在包含了date_histogram前缀。

名称top_users现在包含了top_hits前缀。

对于某些聚合,返回的类型可能与请求中提供的类型不同。

terms、significant_terms 和 percentiles(百分位数)聚合就是这种情况,其中返回的类型还包含有关目标字段类型的信息: lterms(用于长整形字段上的词项聚合), sigsterms(用于字符串字段上的重要词项聚合), tdigest_percentiles(用于基于 TDigest 算法的百分位数聚合)。