Link Search Menu Expand Document Documentation Menu

序列差分聚合

serial_diff聚合是一种父管道聚合,用于计算当前桶和前一个桶中指标值之间的差值。它将结果存储在当前桶中。

使用serial_diff聚合可以计算指定滞后时间段内指标值的变化。lag参数(一个正整数值)指定从当前桶中减去哪个前一个桶的值。默认的lag值为1,这意味着serial_diff从当前桶的值中减去紧邻前一个桶的值。

参数

serial_diff聚合接受以下参数。

参数 必需/可选 数据类型 描述
buckets_path 必需 字符串 要聚合的聚合桶的路径。参见桶路径
gap_policy 可选 字符串 应用于缺失数据的策略。有效值为skipinsert_zeros。默认值为skip。参见数据间隙
format 可选 字符串 一个DecimalFormat格式化字符串。以聚合的value_as_string属性返回格式化输出。
lag 可选 整数 从当前桶中减去的历史桶。必须是正整数。默认值为1

示例

以下示例使用OpenSearch Dashboards日志示例数据创建一个以一个月为间隔的日期直方图。sum子聚合计算每个月的总字节数。最后,serial_diff聚合从这些总和中计算每月总字节数的月度差值。

GET opensearch_dashboards_sample_data_logs/_search
{
   "size": 0,
   "aggs": {
      "monthly_bytes": {                  
         "date_histogram": {
            "field": "@timestamp",
            "calendar_interval": "month"
         },
         "aggs": {
            "total_bytes": {
               "sum": {
                  "field": "bytes"     
               }
            },
            "monthly_bytes_change": {
               "serial_diff": {                
                  "buckets_path": "total_bytes",
                  "lag": 1
               }
            }
         }
      }
   }
}

响应包含第二和第三个月的月度差值。(第一个月的serial_diff无法计算,因为没有前一个月可供比较)

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 10000,
      "relation": "gte"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "monthly_bytes": {
      "buckets": [
        {
          "key_as_string": "2025-03-01T00:00:00.000Z",
          "key": 1740787200000,
          "doc_count": 480,
          "total_bytes": {
            "value": 2804103
          }
        },
        {
          "key_as_string": "2025-04-01T00:00:00.000Z",
          "key": 1743465600000,
          "doc_count": 6849,
          "total_bytes": {
            "value": 39103067
          },
          "monthly_bytes_change": {
            "value": 36298964
          }
        },
        {
          "key_as_string": "2025-05-01T00:00:00.000Z",
          "key": 1746057600000,
          "doc_count": 6745,
          "total_bytes": {
            "value": 37818519
          },
          "monthly_bytes_change": {
            "value": -1284548
          }
        }
      ]
    }
  }
}

以下折线图显示了serial_diff聚合的结果。x轴表示时间,y轴表示每月总传输字节量的变化。折线上的每个数据点都反映了该月总字节量与前一个月的差值。例如,5,000,000的值表示系统比前一个月多传输了500万字节;负值表示减少。第一个月被排除在折线之外,因为没有前一个桶可供比较(差值未定义)。折线从第二个月开始,并延伸到所有可用数据。

Example serial difference aggregation visualization

此可视化有助于您快速发现数据量随时间变化的峰值、下降或趋势。

示例:多周期差分

使用更大的lag值可以将每个桶与过去更远的桶进行比较。以下示例计算每周字节数据的差异,滞后为4(意味着每个桶与4周前的桶进行比较)。这可以消除周期为4周的任何变化。

GET opensearch_dashboards_sample_data_logs/_search
{
   "size": 0,
   "aggs": {
      "monthly_bytes": {                  
         "date_histogram": {
            "field": "@timestamp",
            "calendar_interval": "week"
         },
         "aggs": {
            "total_bytes": {
               "sum": {
                  "field": "bytes"     
               }
            },
            "monthly_bytes_change": {
               "serial_diff": {                
                  "buckets_path": "total_bytes",
                  "lag": 4
               }
            }
         }
      }
   }
}

示例响应

响应包含一个每周桶列表。请注意,serial_diff聚合直到第五个桶(当一个滞后为4的桶可用时)才开始计算。

响应
{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 10000,
      "relation": "gte"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "monthly_bytes": {
      "buckets": [
        {
          "key_as_string": "2025-03-24T00:00:00.000Z",
          "key": 1742774400000,
          "doc_count": 249,
          "total_bytes": {
            "value": 1531493
          }
        },
        {
          "key_as_string": "2025-03-31T00:00:00.000Z",
          "key": 1743379200000,
          "doc_count": 1617,
          "total_bytes": {
            "value": 9213161
          }
        },
        {
          "key_as_string": "2025-04-07T00:00:00.000Z",
          "key": 1743984000000,
          "doc_count": 1610,
          "total_bytes": {
            "value": 9188671
          }
        },
        {
          "key_as_string": "2025-04-14T00:00:00.000Z",
          "key": 1744588800000,
          "doc_count": 1610,
          "total_bytes": {
            "value": 9244851
          }
        },
        {
          "key_as_string": "2025-04-21T00:00:00.000Z",
          "key": 1745193600000,
          "doc_count": 1609,
          "total_bytes": {
            "value": 9061045
          },
          "monthly_bytes_change": {
            "value": 7529552
          }
        },
        {
          "key_as_string": "2025-04-28T00:00:00.000Z",
          "key": 1745798400000,
          "doc_count": 1554,
          "total_bytes": {
            "value": 8713507
          },
          "monthly_bytes_change": {
            "value": -499654
          }
        },
        {
          "key_as_string": "2025-05-05T00:00:00.000Z",
          "key": 1746403200000,
          "doc_count": 1710,
          "total_bytes": {
            "value": 9544718
          },
          "monthly_bytes_change": {
            "value": 356047
          }
        },
        {
          "key_as_string": "2025-05-12T00:00:00.000Z",
          "key": 1747008000000,
          "doc_count": 1610,
          "total_bytes": {
            "value": 9155820
          },
          "monthly_bytes_change": {
            "value": -89031
          }
        },
        {
          "key_as_string": "2025-05-19T00:00:00.000Z",
          "key": 1747612800000,
          "doc_count": 1610,
          "total_bytes": {
            "value": 9025078
          },
          "monthly_bytes_change": {
            "value": -35967
          }
        },
        {
          "key_as_string": "2025-05-26T00:00:00.000Z",
          "key": 1748217600000,
          "doc_count": 895,
          "total_bytes": {
            "value": 5047345
          },
          "monthly_bytes_change": {
            "value": -3666162
          }
        }
      ]
    }
  }
}
剩余 350 字符

有问题?

想贡献?