# Metric Aggregations on Multiple Filters

**URL:** https://discuss.elastic.co/t/metric-aggregations-on-multiple-filters/332662
**Category:** Elasticsearch
**Created:** [May 5, 2023, 3:48pm UTC](https://discuss.elastic.co/t/metric-aggregations-on-multiple-filters/332662 "2023-05-05T15:48:30Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![hi\_xavier](https://avatars.discourse-cdn.com/v4/letter/h/ac91a4/32.png) [@hi\_xavier](https://discuss.elastic.co/u/hi_xavier)
#### Post date: [May 5, 2023, 3:48pm UTC](https://discuss.elastic.co/t/metric-aggregations-on-multiple-filters/332662/1 "2023-05-05T15:48:30Z")

</div>

Hello,

Is there a way to run the a metric aggregation (value count for example) on multiple filters. For example, I'm currently able to get the counts of successes and errors all users are getting with the following aggregation:

```auto
aggs = {
    "f": {
        "filters": {
            "filters": {
                "start_count": {"term": {"type": "Success"}},
                "error_count": {"term": {"type": "Error"}}
            }
        },
        "aggs": {
            "counts": {"value_count": {"field": "type"}}
        }
    }
}

```

Now, lets say I wanted to get the count of success and errors for specific users Mary, Joe, and Tina. How would I use aggregations and filters to achieve this?

Thanks !

---

<div class="post-metadata">

### Author: ![Alexis\_Roberson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alexis_roberson/32/113233_2.png) [@Alexis\_Roberson](https://discuss.elastic.co/u/Alexis_Roberson)
#### Post date: [May 8, 2023, 5:49pm UTC](https://discuss.elastic.co/t/metric-aggregations-on-multiple-filters/332662/2 "2023-05-08T17:49:22Z")

</div>

So you're using the filters aggregation, which is a type of bucket aggregation. So that will give you the number of success and errors. If you want to find this count per user, you can use a top level query, like this:

```auto
POST new_user/_search
{
  "size":0,
  "query": { "match": { "name": "Mary" } },
  "aggs": {
    "f": {
      "filters": {
        "filters": {
          "success": { "term": { "type": "Success" } },
          "errors": { "term": { "type": "Error" } }
        }
      },
      "aggs": {
        "counts": { "value_count": { "field": "type" } }
      }
    }
  }
}

```

And your response should look something like this:

```auto
  "aggregations": {
    "f": {
      "buckets": {
        "errors": {
          "doc_count": 1,
          "counts": {
            "value": 1
          }
        },
        "success": {
          "doc_count": 2,
          "counts": {
            "value": 2
          }
        }
      }
    }
  }

```

---

<div class="post-metadata">

### Author: ![Alexis\_Roberson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alexis_roberson/32/113233_2.png) [@Alexis\_Roberson](https://discuss.elastic.co/u/Alexis_Roberson)
#### Post date: [May 8, 2023, 5:56pm UTC](https://discuss.elastic.co/t/metric-aggregations-on-multiple-filters/332662/3 "2023-05-08T17:56:45Z")

</div>

You can also read more about top level queries and the filters aggregation here:

> **[Filter aggregation | Elasticsearch Guide \[8.7\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/8.7/search-aggregations-bucket-filter-aggregation.html#use-top-level-query-to-limit-all-aggs)**

---

<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: [June 5, 2023, 5:57pm UTC](https://discuss.elastic.co/t/metric-aggregations-on-multiple-filters/332662/4 "2023-06-05T17:57:30Z")

</div>

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