Link Search Menu Expand Document Documentation Menu

累计和聚合

cumulative_sum 聚合是一种父聚合,它计算前一个聚合桶中的累计和。

累计和是给定序列的部分和序列。例如,序列 {a,b,c,…} 的累计和是 aa+ba+b+c 等。您可以使用累计和来可视化字段随时间变化的速率。

参数

cumulative_sum 聚合接受以下参数。

参数 必需/可选 数据类型 描述
buckets_path 必需 字符串 要聚合的聚合桶的路径。参见桶路径
format 可选 字符串 一个 DecimalFormat 格式化字符串。将格式化后的输出返回到聚合的 value_as_string 属性中。

示例

以下示例使用 OpenSearch Dashboards 电子商务样本数据创建一个间隔为一个月的时间直方图。sum 子聚合计算每个月的总字节数。最后,cumulative_sum 聚合计算每个月桶的累计字节数。

GET opensearch_dashboards_sample_data_logs/_search
{
  "size": 0,
  "aggs": {
    "sales_per_month": {
      "date_histogram": {
        "field": "@timestamp",
        "calendar_interval": "month"
      },
      "aggs": {
        "no-of-bytes": {
          "sum": {
            "field": "bytes"
          }
        },
        "cumulative_bytes": {
          "cumulative_sum": {
            "buckets_path": "no-of-bytes"
          }
        }
      }
    }
  }
}

示例响应

{
  "took": 8,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 10000,
      "relation": "gte"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "sales_per_month": {
      "buckets": [
        {
          "key_as_string": "2025-03-01T00:00:00.000Z",
          "key": 1740787200000,
          "doc_count": 480,
          "no-of-bytes": {
            "value": 2804103
          },
          "cumulative_bytes": {
            "value": 2804103
          }
        },
        {
          "key_as_string": "2025-04-01T00:00:00.000Z",
          "key": 1743465600000,
          "doc_count": 6849,
          "no-of-bytes": {
            "value": 39103067
          },
          "cumulative_bytes": {
            "value": 41907170
          }
        },
        {
          "key_as_string": "2025-05-01T00:00:00.000Z",
          "key": 1746057600000,
          "doc_count": 6745,
          "no-of-bytes": {
            "value": 37818519
          },
          "cumulative_bytes": {
            "value": 79725689
          }
        }
      ]
    }
  }
}
剩余 350 字符

有问题?

想贡献?