ES|QL time series aggregation functions
The first STATS under a TS source command supports
aggregation functions per time series. These functions accept up to two arguments.
The first argument is required and denotes the metric name of the time series.
The second argument is optional and allows specifying a sliding time window for
aggregating metric values. Note that this is orthogonal to time bucketing of
output results, as specified in the BY clause (e.g. through
TBUCKET).
For example, the following query calculates the average rate of requests per
host for every minute, using values over a sliding window of 10 minutes:
TS metrics
| WHERE TRANGE(1h)
| STATS AVG(RATE(requests, 10m)) BY TBUCKET(1m), host
Accepted window values are currently limited to multiples of the time bucket interval in the BY clause. If no window is specified, the time bucket interval is implicitly used as a window.
All window values are accepted, though there are performance optimizations for the cases where the window is a multiple of the time bucket interval.
It's currently not allowed to mix windows that are smaller than the time bucket for one metrics and larger than the time bucket for another metrics, in the same query.
When a time series aggregation function is used directly in STATS (that is, not
wrapped in an outer aggregation such as AVG() or SUM()), results are implicitly
grouped by every time series dimension and include a _timeseries column. You can
narrow or make this grouping explicit with the
WITHOUT
grouping function (
The inner function you pick depends on the field's
metric_type
mapping:
- Counters: monotonically increasing values that reset on process restart. Use
RATE,INCREASE, and the other counter-aware functions. These detect resets per time series and compute correct deltas; applying a gauge-only function such asAVG_OVER_TIMEto a counter is rarely what you want. - Gauges: point-in-time values that can move up or down. Use
LAST_OVER_TIME(the implicit default when no inner function is given),AVG_OVER_TIME,MAX_OVER_TIME, and the other*_OVER_TIMEvariants. Counter functions likeRATEreject gauge fields.
For the conceptual context behind the counter/gauge split, refer to When to use TS vs FROM.
The following time series aggregation functions are supported:
ABSENT_OVER_TIMECalculates the absence of a field over a time range.
AVG_OVER_TIMECalculates the average over time of a numeric field.
COUNT_OVER_TIMECalculates the count over time value of a field.
COUNT_DISTINCT_OVER_TIMECalculates the count of distinct values over time for a field.
DELTACalculates the absolute change of a gauge field in a time window.
DERIVCalculates the derivative over time of a numeric field using linear regression.
FIRST_OVER_TIMECalculates the earliest value of a field over a time window.
IDELTACalculates the absolute change between the last two data points of a gauge.
INCREASECalculates the absolute increase of a counter field in a time window.
IRATECalculates the per-second rate of increase between the last two data points.
LAST_OVER_TIMECalculates the latest value of a field over a time window.
MAX_OVER_TIMECalculates the maximum value of a field over a time window.
MIN_OVER_TIMECalculates the minimum value of a field over a time window.
PERCENTILE_OVER_TIMECalculates the percentile over time of a field.
PRESENT_OVER_TIMECalculates the presence of a field over a time range.
RATECalculates the per-second average rate of increase of a counter.
STDDEV_OVER_TIMECalculates the population standard deviation over time of a numeric field.
SUM_OVER_TIMECalculates the sum over time value of a field.
VARIANCE_OVER_TIMECalculates the population variance over time of a numeric field.