# Configure elastic search query to alert when the average of Total time taken exceeds a threshold

**URL:** https://discuss.elastic.co/t/configure-elastic-search-query-to-alert-when-the-average-of-total-time-taken-exceeds-a-threshold/350274
**Category:** Kibana
**Tags:** elastic-stack-alerting
**Created:** [January 3, 2024, 6:28am UTC](https://discuss.elastic.co/t/configure-elastic-search-query-to-alert-when-the-average-of-total-time-taken-exceeds-a-threshold/350274 "2024-01-03T06:28:13Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Vanya](https://avatars.discourse-cdn.com/v4/letter/v/7ea924/32.png) [@Vanya](https://discuss.elastic.co/u/Vanya)
#### Post date: [January 3, 2024, 6:28am UTC](https://discuss.elastic.co/t/configure-elastic-search-query-to-alert-when-the-average-of-total-time-taken-exceeds-a-threshold/350274/1 "2024-01-03T06:28:13Z")

</div>

Hi All,  
I am trying to write a search query for Kibana rules for it to alert when the average of the total time taken exceeds a certain thereshold. Have tried using aggegators, filters and also scripts but on testing the results are provided only for the code written in the query block and aggregators/scripts don't work. Here is one of the queries I had tried which did not work.  
Here, I am matching the name and type to get set of record under it for which average time needs to be calculated based on total time for each of the records.

```auto
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "abc*"
          }
        },
        {
          "match": {
            "type": "A"
          }
        },
          {
          "match": {
            "avg_time": "2000"
          }
        }
      ]
    }
  },
"aggs": {
"avg_time": {
"avg": { "field": "TotalTime"}
}
}
}

```

---

<div class="post-metadata">

### Author: ![lukas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas/32/6812_2.png) [@lukas](https://discuss.elastic.co/u/lukas)
#### Post date: [January 4, 2024, 10:40pm UTC](https://discuss.elastic.co/t/configure-elastic-search-query-to-alert-when-the-average-of-total-time-taken-exceeds-a-threshold/350274/2 "2024-01-04T22:40:16Z")

</div>

Have you tried using the [bucket selector](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-selector-aggregation.html) aggregation?

It would be something like this:

```auto
{
  "aggs": {
    "avg_time": {
      "avg": {
        "field": "TotalTime"
      },
      "aggs": {
        "avg_time_filter": {
          "bucket_selector": {
            "buckets_path": {
              "avgTime": "avg_time"
            },
            "script": "params.avgTime > 2000"
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![Vanya](https://avatars.discourse-cdn.com/v4/letter/v/7ea924/32.png) [@Vanya](https://discuss.elastic.co/u/Vanya)
#### Post date: [January 15, 2024, 6:02am UTC](https://discuss.elastic.co/t/configure-elastic-search-query-to-alert-when-the-average-of-total-time-taken-exceeds-a-threshold/350274/3 "2024-01-15T06:02:47Z")

</div>

Hi @lukas

Thanks for your reply. I tested it out and a few more solutions around bucket selector too but the aggregators are not working within the rules configured for alerts. Since having "query" section is a mandate, whenever I add the query section, it gives results only of the query and if referenced with aggregators, it returns result of query + aggregators resulting in extra documents in the results.  
My requirement is to match the documents with a particular name and type and calculate the average time for those documents and return result if the average time is above 2000. And on this particular result I want to add an alert to notify users when the 2k threshold is breached.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [February 12, 2024, 6:03am UTC](https://discuss.elastic.co/t/configure-elastic-search-query-to-alert-when-the-average-of-total-time-taken-exceeds-a-threshold/350274/4 "2024-02-12T06:03:33Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
