elasticsearch聚合系列之histogram

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010503427/article/details/81592196

histogram与terms聚合类似,都是数据分组,区别是terms是按照field的值分组,而histogram可以按照指定的间隔对field进行分组

解释一波

1、对age进行间隔为10的分组,分组内对age进行avg计算

2、interval:设置间隔

3、min_doc_count:设置阈值,最小doc_count数,即组内doc_count的值。6.2.4系列不能小于0

4、extended_bounds:设置min和max。即分组内key的范围。题中设置min为-1,响应中从-10开始,是因为间隔设置为10,所有为0-10=-10,如果设置min为-3、-4、-7等,还是从-10开始。若min设置为-12,则从-20开始。max是同样的道理。

 

请求:

POST bank/_doc/_search
{
  "size":0,
  "aggs": {
    "hitogram_agg": {
      "histogram": {
        "field": "age",
        "interval": 10,
        "min_doc_count": 0,
        "extended_bounds": {
          "min": -1,
          "max": 60
        }
      },
      "aggs": {
        "avg_age": {
          "avg": {
            "field": "age"
          }
        }
      }
    }
  }
}

 

响应:

{
  "took": 9,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 15,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "hitogram_agg": {
      "buckets": [
        {
          "key": -10,
          "doc_count": 0,
          "avg_age": {
            "value": null
          }
        },
        {
          "key": 0,
          "doc_count": 0,
          "avg_age": {
            "value": null
          }
        },
        {
          "key": 10,
          "doc_count": 0,
          "avg_age": {
            "value": null
          }
        },
        {
          "key": 20,
          "doc_count": 3,
          "avg_age": {
            "value": 25.333333333333332
          }
        },
        {
          "key": 30,
          "doc_count": 12,
          "avg_age": {
            "value": 34.333333333333336
          }
        },
        {
          "key": 40,
          "doc_count": 0,
          "avg_age": {
            "value": null
          }
        },
        {
          "key": 50,
          "doc_count": 0,
          "avg_age": {
            "value": null
          }
        },
        {
          "key": 60,
          "doc_count": 0,
          "avg_age": {
            "value": null
          }
        }
      ]
    }
  }
}

 

阅读更多

没有更多推荐了,返回首页