# Filter by a regexp inside a terms aggregation

**URL:** https://discuss.elastic.co/t/filter-by-a-regexp-inside-a-terms-aggregation/227573
**Category:** Elasticsearch
**Created:** [April 11, 2020, 12:23am UTC](https://discuss.elastic.co/t/filter-by-a-regexp-inside-a-terms-aggregation/227573 "2020-04-11T00:23:48Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![misterone](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@misterone](https://discuss.elastic.co/u/misterone)
#### Post date: [April 11, 2020, 12:23am UTC](https://discuss.elastic.co/t/filter-by-a-regexp-inside-a-terms-aggregation/227573/1 "2020-04-11T00:23:48Z")

</div>

Hi,  
Is there a way to group by field1 only when field1 matches the following logic :  
field1 contains ALL of the keywords in this list : [keyword1, keyword2, keyword3]  
I have a working example for 1 keyword but I can't do it with more than 1:

```auto
"aggs": {
        "xxx": {
            "terms": {
                "field": "field1",
                "include": ".*keyword1.*"
            }
        }
    }

```

I would like to implement the regexp on this stackoverflow discussion but it doesn't work because the regexp syntax of Lucene is different :

> <https://stackoverflow.com/questions/4389644/regex-to-match-string-containing-two-names-in-any-order>

---

<div class="post-metadata">

### Author: ![misterone](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@misterone](https://discuss.elastic.co/u/misterone)
#### Post date: [April 11, 2020, 3:32am UTC](https://discuss.elastic.co/t/filter-by-a-regexp-inside-a-terms-aggregation/227573/2 "2020-04-11T03:32:57Z")

</div>

Ideally I would like to add the flags : "ALL" to the regexp to be able to do something like this : ```

```auto
"aggs": {
        "xxx": {
            "terms": {
            	"flags": "ALL",
                "field": "field1",
                "include": ".+keyword1.+&.+keyword2.+"
            }
        }
    }

```

---

<div class="post-metadata">

### Author: ![misterone](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@misterone](https://discuss.elastic.co/u/misterone)
#### Post date: [April 11, 2020, 4:05pm UTC](https://discuss.elastic.co/t/filter-by-a-regexp-inside-a-terms-aggregation/227573/3 "2020-04-11T16:05:42Z")

</div>

Other options would be to :

- filter this on the back end side. But isn't it less performant ? Elastic is going to aggregate against every possible field where in fact I might be only interested in some of them.
- I could also try a bucket selection aggregation, but I would hope there is a better way.

---

<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: [May 9, 2020, 4:17pm UTC](https://discuss.elastic.co/t/filter-by-a-regexp-inside-a-terms-aggregation/227573/4 "2020-05-09T16:17:35Z")

</div>

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