> Documentation index for AI agents: https://docs.pagerduty.com/llms.txt

# Elasticsearch: Search Metrics

Search APM and OpenTelemetry metric indices in Elasticsearch


> **Availability:** See the [Incident Workflow Actions Overview table](/workflow-actions) for the plans this workflow is available on.

## Description

The action searches the metric indices that match **Index Pattern** using a full **Query DSL** `_search` request body or a **Lucene** query string, and returns the matching hits together with any aggregations. It is built for aggregations such as latency percentiles, error rate, and throughput over time, which help you see when and how a service degraded during an incident.

PagerDuty does _not_ inject a default time range. For time-bounded results, add a `range` filter on `@timestamp` to your Query DSL body, or a range such as `@timestamp:[now-1h TO now]` to your Lucene query.

### Prerequisites

- An Elasticsearch cluster reachable from PagerDuty over HTTPS.
- An Elasticsearch **API key** with read permission on the indices that match your **Index Pattern**.
- An Elasticsearch connection in PagerDuty. See the [Elasticsearch subsection in Workflow Integrations](/ai-automation/automation/incident-workflows/workflow-integrations#elasticsearch).

## Instructions

1. If you have not done so, follow the instructions to [Create an Incident Workflow](/ai-automation/automation/incident-workflows#create-an-incident-workflow).
2. When the instructions prompt you to [add actions](/ai-automation/automation/incident-workflows#add-actions), select **this action**.
3. Enter the following **Inputs** and click **Save**.
4. Continue following instructions to **Publish** the Workflow.
5. When the action runs, you will see the **Outputs** listed below.

## Inputs

> **Field References:** Fields with the **\{+\}** icon accept [Field References](/ai-automation/automation/incident-workflows#field-references), which can be useful for referencing incident data or outputs created in prior workflow steps. To add Field References, click **\{+\}**, or enter `{{`, and select relevant fields. Refer to the [Field References](/ai-automation/automation/incident-workflows#field-references) article for more information.

<br />

| Name | Description |
| :--- | :--- |
| **Connection** | The Elasticsearch Workflow Integration connection (Elasticsearch URL and API Key configured in PagerDuty). If you have not yet configured an Elasticsearch integration, see the [Elasticsearch Integration](/ai-automation/automation/incident-workflows/workflow-integrations#elasticsearch) instructions. |
| **Index Pattern** | Index pattern to search. Default: `metrics-*`, which covers classic APM (`metrics-apm.*`), OpenTelemetry-native (`metrics-*.otel-*`), and custom metric indices. |
| **Query Type** | **Query DSL** (default) or **Lucene**. See Query Types below. |
| **Query** | For Query DSL, the full `_search` request body as JSON, for example `{"size": 0, "query": {...}, "aggs": {...}}`. For Lucene, a query string, for example `service.name:checkout AND metricset.name:service_transaction`. |
| **Max Results** | Maximum number of hits to return. Allowed range: **1–1000**. Default: **100**. |

> **Query Types**
>
> **Query DSL** — **Query** must be a JSON object containing the **full** `_search` request body, so you can use `query`, `aggs`, `sort`, `_source`, and `size`. PagerDuty sends it to `POST /<Index Pattern>/_search`. If the body sets `size`, it is capped at **Max Results**; if it does not, **Max Results** is used. Set `"size": 0` for an aggregation-only search that returns no hits. Invalid JSON, or a `size` that is not a non-negative integer, produces an error.
>
> **Lucene** — Sent to `_search` as a `query_string` query with your **Query** text, sorted newest first by `@timestamp`, with **Max Results** as the request size. Aggregations are not available with Lucene; switch to Query DSL to use them.

### Example

The following Query DSL body returns the average transaction duration for the `checkout` service in five-minute buckets over the last hour. The `metricset.name` filter limits the search to service transaction metrics, and the `metricset.interval` filter keeps APM's 10m and 60m rollups from counting the same transactions again:

```json
{
  "size": 0,
  "query": {
    "bool": {
      "filter": [
        { "term": { "service.name": "checkout" } },
        { "term": { "metricset.name": "service_transaction" } },
        { "term": { "metricset.interval": "1m" } },
        { "range": { "@timestamp": { "gte": "now-1h" } } }
      ]
    }
  },
  "aggs": {
    "over_time": {
      "date_histogram": { "field": "@timestamp", "fixed_interval": "5m" },
      "aggs": { "latency": { "avg": { "field": "transaction.duration.summary" } } }
    }
  }
}
```

The example assumes the classic Elastic APM (APM Server and ECS) schema. With native OpenTelemetry ingest, field names differ (for example, `duration` instead of `transaction.duration.us`).

## Outputs

| Name | Description |
| :--- | :--- |
| **Results** | JSON array of hits returned by Elasticsearch. Empty when the search uses `"size": 0` or returns no hits. |
| **Aggregations** | The raw `aggregations` object returned by Elasticsearch. Empty when the query has no aggregations. |
| **Total Hits** | Total matching documents. This is a lower bound when Elasticsearch stops counting at its `track_total_hits` limit. |
| **Search Duration** | Query execution time as reported by Elasticsearch (for example `18ms`). |
| **Result** | **Success** or **Failed**. |
| **Result Summary** | Brief description of what the action did or why it failed. It also notes when **Index Pattern** matched no index and when results were capped at **Max Results**. |
| **Error** | Populated when the action fails (for example authentication, an invalid index pattern, or invalid Query DSL). |

<br />
