# Is it possible to filter aggregation return result?

**URL:** <https://discuss.elastic.co/t/is-it-possible-to-filter-aggregation-return-result/359238>\
**Category:** Elasticsearch\
**Created:** [May 10, 2024, 7:58am UTC](https://discuss.elastic.co/t/is-it-possible-to-filter-aggregation-return-result/359238 "2024-05-10T07:58:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![J\_Kit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/j_kit/32/77810_2.png) [@J\_Kit](https://discuss.elastic.co/u/J_Kit)\
**Post date:** [May 10, 2024, 7:58am UTC](https://discuss.elastic.co/t/is-it-possible-to-filter-aggregation-return-result/359238/1 "2024-05-10T07:58:17Z")

</div>

Hi,

I got example 2 documents

doc: 1

```auto
{
...
author: ['mike', 'adam', 'julie']
}

```

doc: 2

```auto
{
...
author: ['mike', 'mike stephen', 'apple']
}

```

in keyword search scenario, user will search "mike" with query below

```auto
{
    "query": {
        "match": {
            "author": "mike"
        }
    },
    "aggs": {
        "field2_terms": {
            "terms": {
                "field": "author"
            }
        }
    }
}

```

Output for aggregation is

```auto
[
    {
        "key": "mike",
        "doc_count": 2
    },
    {
        "key": "adam",
        "doc_count": 1
    },
    {
        "key": "julie",
        "doc_count": 1
    },
    {
        "key": "mike stephen",
        "doc_count": 1
    },
    {
        "key": "apple",
        "doc_count": 1
    }
]

```

the question is how do I get only `MIKE*` for aggregation. I would like the aggregation result look like this. is it possible in elasticsearch query?

```auto
[
    {
        "key": "mike",
        "doc_count": 2
    },
    {
        "key": "mike stephen",
        "doc_count": 1
    },
]

```

---

<div class="post-metadata">

**Author:** ![demjened](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/demjened/32/109159_2.png) [@demjened](https://discuss.elastic.co/u/demjened)\
**Post date:** [May 10, 2024, 8:42pm UTC](https://discuss.elastic.co/t/is-it-possible-to-filter-aggregation-return-result/359238/2 "2024-05-10T20:42:33Z")

</div>

Hi J\_Kit,

Yes it's possible to [filter aggregation results with regular expressions](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_filtering_values_with_regular_expressions_2):

```json
GET agg-test/_search
{
  "query": {
    "match": {
      "author": "mike"
    }
  },
  "aggs": {
    "field2_terms": {
      "terms": {
        "field": "author",
        "include": "mike.*"
      }
    }
  }
}

```

Result:

```json
  ...
  "aggregations": {
    "field2_terms": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "mike",
          "doc_count": 2
        },
        {
          "key": "mike stephen",
          "doc_count": 1
        }
      ]
    }
  }

```
