Type to search.

ES|QL TS_COLLAPSE command

Serverless Preview

Elastic Cloud Serverless projects are autoscaled environments, fully managed by Elastic and available on Elastic Cloud.

Availability

  • Preview

    This functionality is in technical preview and is ready for evaluation. Use with caution in production; it is not recommended for mission-critical workloads. Elastic will work to fix any issues, but features in technical preview are not subject to the support SLA of official GA features. Specific Support terms apply.

Serverless interfaces and procedures might differ from classic Elastic Stack deployments.

Stack Preview 9.5+

The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats.

Availability

  • Preview since 9.5

    This functionality is in technical preview and is ready for evaluation. Use with caution in production; it is not recommended for mission-critical workloads. Elastic will work to fix any issues, but features in technical preview are not subject to the support SLA of official GA features. Specific Support terms apply.

Unless stated otherwise on the page, this functionality is available when your Elastic Stack is deployed on Elastic Cloud Hosted, Elastic Cloud Enterprise, Elastic Cloud on Kubernetes, and self-managed environments.

This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.

TS_COLLAPSE is a processing command that collapses the expanded, per-step output of a PROMQL command into one row per time series. The metric column and the step column become multi-valued fields; dimension columns remain single-valued.

ES|QL
... | TS_COLLAPSE

None.

A PROMQL command produces one row per (series, step) pair, with dimension columns (for example host, cluster) repeated on every row. TS_COLLAPSE merges all rows that belong to the same series into a single row:

  • The metric column (the named result column, for example rate) and the step column become multi-valued (MV) fields whose elements are ordered chronologically.
  • Dimension columns keep a single value per row because they are identical for all steps of a given series.
  • Missing or null samples are omitted from both MV fields, so the result is sparse rather than null-filled.

The MV fields are positionally aligned: step[i] corresponds to the metric value at index i. Applying MV functions independently to one of the two columns—for example MV_SORT—will break this alignment.

TS_COLLAPSE can only appear immediately after a PROMQL command. Other processing commands can follow TS_COLLAPSE.

A PROMQL query without TS_COLLAPSE returns one row per (series, step) pair. Adding TS_COLLAPSE produces one row per series, with the metric and step columns as positionally aligned MV arrays:

ES|QL
PROMQL index=k8s step=1m start="2024-05-10T00:11:00.000Z" end="2024-05-10T00:14:00.000Z" cost=(round(sum(avg_over_time(network.cost[1m])), 0.0001))
| TS_COLLAPSE
cost:double step:datetime
[13.25, 36.375, 37.9375, 39.5417] [2024-05-10T00:11:00.000Z, 2024-05-10T00:12:00.000Z, 2024-05-10T00:13:00.000Z, 2024-05-10T00:14:00.000Z]

When the PROMQL query groups results by a dimension (for example cluster), TS_COLLAPSE returns one row per unique dimension value, with metric values and steps collapsed into MV arrays:

ES|QL
PROMQL index=k8s step=1m start="2024-05-10T00:02:00.000Z" end="2024-05-10T00:04:00.000Z" clients=(avg by (cluster) (last_over_time(network.eth0.currently_connected_clients[1m])))
| TS_COLLAPSE
| SORT cluster
clients:double step:datetime cluster:keyword
[396.5, 632.5, 742.0] [2024-05-10T00:02:00.000Z, 2024-05-10T00:03:00.000Z, 2024-05-10T00:04:00.000Z] prod
[440.0, 565.0, 454.0] [2024-05-10T00:02:00.000Z, 2024-05-10T00:03:00.000Z, 2024-05-10T00:04:00.000Z] qa
[205.0, 357.0] [2024-05-10T00:03:00.000Z, 2024-05-10T00:04:00.000Z] staging

Post-process with EVAL

Use EVAL with MV functions to compute per-series aggregates over the collapsed arrays, then sort or filter on the results:

ES|QL
PROMQL index=k8s step=1m start="2024-05-10T00:02:00.000Z" end="2024-05-10T00:04:00.000Z" clients=(avg by (cluster) (last_over_time(network.eth0.currently_connected_clients[1m])))
| TS_COLLAPSE
| EVAL avg_clients = MV_AVG(clients)
| SORT avg_clients DESC
| LIMIT 10
clients:double step:datetime cluster:keyword avg_clients:double
[396.5, 632.5, 742.0] [2024-05-10T00:02:00.000Z, 2024-05-10T00:03:00.000Z, 2024-05-10T00:04:00.000Z] prod 590.3333333333334
[440.0, 565.0, 454.0] [2024-05-10T00:02:00.000Z, 2024-05-10T00:03:00.000Z, 2024-05-10T00:04:00.000Z] qa 486.3333333333333
[205.0, 357.0] [2024-05-10T00:03:00.000Z, 2024-05-10T00:04:00.000Z] staging 281.0