如何将cumulative_sum与之前的聚合一起使用?

时间:2016-10-20 10:25:22

标签: elasticsearch

我想绘制一些事件的累积总和,每天。 cumulative sum aggregation似乎是要走的路,所以我尝试重用文档中给出的示例。

第一个聚合工作正常,以下查询

{
   "aggs": {
       "vulns_day" : {
           "date_histogram" :{
               "field": "HOST_START_iso",
               "interval": "day"
           }
       }
   }
}

给出回复,例如

        (...)
        {
           "key_as_string": "2016-09-08T00:00:00.000Z",
           "key": 1473292800000,
           "doc_count": 76330
        },
        {
           "key_as_string": "2016-09-09T00:00:00.000Z",
           "key": 1473379200000,
           "doc_count": 37712
        },
        (...)

然后我想通过

查询上面doc_count的累积总和
{
   "aggs": {
       "vulns_day" : {
           "date_histogram" :{
               "field": "HOST_START_iso",
               "interval": "day"
           }
       },
       "aggs": {
           "vulns_cumulated": {
               "cumulative_sum": {
                   "buckets_path": "doc_count"
               }
           }
       }
   }
}

但它会出错:

"reason": {
               "type": "search_parse_exception",
               "reason": "Could not find aggregator type [vulns_cumulated] in [aggs]",

我看到bucket_path应该指向要求和的元素,累积聚合的示例创建了一个特定的中间和,但我没有任何要求和(doc_count旁边)。

2 个答案:

答案 0 :(得分:0)

我想,您应该像这样更改您的查询:

{
  "aggs": {
    "vulns_day": {
      "date_histogram": {
        "field": "HOST_START_iso",
        "interval": "day"
      },
      "aggs": {
        "document_count": {
          "value_count": {
            "field": "HOST_START_iso"
          }
        },
        "vulns_cumulated": {
          "cumulative_sum": {
            "buckets_path": "document_count"
          }
        }
      }
    }
  }
}

答案 1 :(得分:0)

我找到了解决方案。由于doc_count似乎不可用,我尝试为时间参数检索stats,并使用其count值。它奏效了:

{
   "size": 0,
   "aggs": {
      "vulns_day": {
         "date_histogram": {
            "field": "HOST_START_iso",
            "interval": "day"
         },
        "aggs": {
          "dates_stats": {
              "stats": {
                  "field": "HOST_START_iso"
              }
          },
         "vulns_cumulated": {
            "cumulative_sum": {
               "buckets_path": "dates_stats.count"
            }
         }
      }
   }
   }
}