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

原文链接 : 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,反映每个聚合的内部类型:

  1. {
  2. "aggregations": {
  3. "date_histogram#tweets_over_time": {
  4. "buckets" : [
  5. {
  6. "key_as_string" : "2009-01-01T00:00:00.000Z",
  7. "key" : 1230768000000,
  8. "doc_count" : 5,
  9. "top_hits#top_users" : {
  10. "hits" : {
  11. "total" : 5,
  12. "max_score" : 1.0,
  13. "hits" : [
  14. {
  15. "_index": "twitter",
  16. "_type": "tweet",
  17. "_id": "0",
  18. "_score": 1.0,
  19. "_source": {
  20. "date": "2009-11-15T14:12:12",
  21. "message": "trying out Elasticsearch",
  22. "user": "kimchy",
  23. "likes": 0
  24. }
  25. }
  26. ]
  27. }
  28. }
  29. }
  30. ]
  31. }
  32. },
  33. ...
  34. }

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

Note

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