# Use regexp and terms together in query

**URL:** <https://discuss.elastic.co/t/use-regexp-and-terms-together-in-query/210590>\
**Category:** Elasticsearch\
**Created:** [December 4, 2019, 8:06pm UTC](https://discuss.elastic.co/t/use-regexp-and-terms-together-in-query/210590 "2019-12-04T20:06:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lorrenrules](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lorrenrules/32/58794_2.png) [@lorrenrules](https://discuss.elastic.co/u/lorrenrules)\
**Post date:** [December 4, 2019, 8:06pm UTC](https://discuss.elastic.co/t/use-regexp-and-terms-together-in-query/210590/1 "2019-12-04T20:06:22Z")

</div>

Hi,

I am trying to use both a regexp and a terms query in the body of my query, and can't figure out how to make it work.

Goal:

```auto
             query: {
                regexp: {
                    'canonicalUrl.keyword': {
                        value: '.*targetString.*'
                    },
                },
                terms: {
                    'metros.keyword': ['atlanta-ga']
                }
            }

```

It works with one or the other, but not together. I get a `parsing_exception` I have tried wrapping in a bool and filter, which also hasn't worked. Anyone have any suggestions, or know whether or not this is possible?

---

<div class="post-metadata">

**Author:** ![pjanzen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pjanzen/32/13756_2.png) [@pjanzen](https://discuss.elastic.co/u/pjanzen)\
**Post date:** [December 4, 2019, 8:49pm UTC](https://discuss.elastic.co/t/use-regexp-and-terms-together-in-query/210590/2 "2019-12-04T20:49:47Z")

</div>

I have tried it on my own data and the following query gives me result, however if I use 'terms' I am getting a parsing error.

```
{
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "regexp": {
            "error": "auth*"
          }
        },
        {
          "term": {
            "protocol": "pop3"
          }
        }
      ]
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![lorrenrules](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lorrenrules/32/58794_2.png) [@lorrenrules](https://discuss.elastic.co/u/lorrenrules)\
**Post date:** [December 4, 2019, 9:19pm UTC](https://discuss.elastic.co/t/use-regexp-and-terms-together-in-query/210590/3 "2019-12-04T21:19:55Z")

</div>

@pjanzen Hey, thanks for the response. Is your `protocol` value an array? For my case, I am checking to see if a particular string lives in the array, and the only way I've found that to work is to use `terms`. If you know another way that works, I'm all ears.

---

<div class="post-metadata">

**Author:** ![pjanzen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pjanzen/32/13756_2.png) [@pjanzen](https://discuss.elastic.co/u/pjanzen)\
**Post date:** [December 5, 2019, 6:39pm UTC](https://discuss.elastic.co/t/use-regexp-and-terms-together-in-query/210590/4 "2019-12-05T18:39:36Z")

</div>

I have tried it where I have a list and this works too. (tags is a list)

```
{
  "size": 10,
  "query": {
    "bool": {
      "must": [
        {
          "regexp": {
            "mailUserStatus": "active*"
          }
        },
        {
          "term": {
            "tags": "mcldapm"
          }
        }
      ]
    }
  }
}
```

---

<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:** [January 2, 2020, 6:39pm UTC](https://discuss.elastic.co/t/use-regexp-and-terms-together-in-query/210590/5 "2020-01-02T18:39:39Z")

</div>

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