# Regex + phrase search

**URL:** <https://discuss.elastic.co/t/regex-phrase-search/111143>\
**Category:** Elasticsearch\
**Created:** [December 11, 2017, 4:27pm UTC](https://discuss.elastic.co/t/regex-phrase-search/111143 "2017-12-11T16:27:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bb1234](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@bb1234](https://discuss.elastic.co/u/bb1234)\
**Post date:** [December 11, 2017, 4:27pm UTC](https://discuss.elastic.co/t/regex-phrase-search/111143/1 "2017-12-11T16:27:20Z")

</div>

Hello everybody.  
I'm trying to create a search query that would allow me to find only documents e.g. with the string like this:  
"some kind of text 1000 other text 200 new text 3"  
"some kind of text 2000 other text 320 new text 30"

My documents are just clear text.

I've tried phrase matching like this:  
`"query": { "query_string": { "default_field": "textdata", "query": "\"some kind of text 1000 other text 200 new text 3\"" }`  
Works perfectly, but obviously matches only exact string #1.  
If I try :  
`"query": { "query_string": { "default_field": "textdata", "query": "some kind of text <1000-3000> other text <200-300> new text <0-100>" }`  
also seems to work, but in this case all info about phrase and proximity is lost

If I try to do  
`"query": { "query_string": { "default_field": "textdata", "query": "\"some kind of text <1000-3000> other text <200-300> new text <0-100>\"" }`  
It doesn't find anything.

Is there a way to do it? Basically use regex for some tokens and enforce proximity rules on all tokens in query ?

Thank you!`Preformatted text`

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [December 12, 2017, 5:30pm UTC](https://discuss.elastic.co/t/regex-phrase-search/111143/2 "2017-12-12T17:30:50Z")

</div>

There is no way to run such a query. Some people might argue you could use span queries, but this would perform terribly.

---

<div class="post-metadata">

**Author:** ![bb1234](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@bb1234](https://discuss.elastic.co/u/bb1234)\
**Post date:** [December 12, 2017, 7:27pm UTC](https://discuss.elastic.co/t/regex-phrase-search/111143/3 "2017-12-12T19:27:23Z")

</div>

Hi Adrien  
Thank you. I'm not worried about performance too much at this point. My index is relatively small and I can afford longer time searches (obviously there is a limit, but that's something I'll worry about later).  
I'll read up on span queries , but if you have an example that would fit my needs by any chance really appreciate it.

Thank you.

---

<div class="post-metadata">

**Author:** ![bb1234](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@bb1234](https://discuss.elastic.co/u/bb1234)\
**Post date:** [December 12, 2017, 9:27pm UTC](https://discuss.elastic.co/t/regex-phrase-search/111143/4 "2017-12-12T21:27:57Z")

</div>

Was trying span queries and what seems to be an obvious approach, tried query like this:

```
` "query": {

"span_near": {

  "clauses": [

    {

      "span_multi": {

      "regexp":{

          "textdata": "[0-9]{6}"

                    }

      }

    }, 

    {

      "span_term": {

          "textdata": "word1"

      }

    }, 

    {

      "span_term": {

          "textdata": "word2"

      }

    }, 

    {

      "span_term": {

          "textdata": "word3"

      }

    }, 

    {

      "span_term": {

          "textdata": "word4"

      }

    }, 

    {

      "span_term": {

          "textdata": "word5"

      }

    }

  ], 

  "in_order": true, 

  "slop": 1

}

```

}`

I get error like this:

{"error":{"root\_cause":[{"type":"parsing\_exception","reason":"[span\_multi] query does not support [regexp]","line":1,"col":385}],"type":"parsing\_exception","reason":"[span\_multi] query does not support [regexp]","line":1,"col":385},"status":400}

While doc says :  
The `span_multi` query allows you to wrap a `multi term query` (one of wildcard, fuzzy, prefix, range or regexp query) as a `span query`, so it can be nested

[https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-multi-term-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-multi-term-query.html)

---

<div class="post-metadata">

**Author:** ![bb1234](https://avatars.discourse-cdn.com/v4/letter/b/258eb7/32.png) [@bb1234](https://discuss.elastic.co/u/bb1234)\
**Post date:** [December 12, 2017, 9:49pm UTC](https://discuss.elastic.co/t/regex-phrase-search/111143/5 "2017-12-12T21:49:58Z")

</div>

Ok found it.  
That multi span needs to be replaced with:

```
` {

      "span_multi": {

      "match":{"regexp":{

          "textdata": "[0-9]{6,}"

                    }

      }}

    },`

```

Thank you!

---

<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 9, 2018, 9:50pm UTC](https://discuss.elastic.co/t/regex-phrase-search/111143/6 "2018-01-09T21:50:13Z")

</div>

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