# Regexp Query cannot escape reserved symbols

**URL:** <https://discuss.elastic.co/t/regexp-query-cannot-escape-reserved-symbols/108527>\
**Category:** Elasticsearch\
**Created:** [November 21, 2017, 7:46am UTC](https://discuss.elastic.co/t/regexp-query-cannot-escape-reserved-symbols/108527 "2017-11-21T07:46:39Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hayk\_Hovhanisyan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hayk_hovhanisyan/32/77342_2.png) [@Hayk\_Hovhanisyan](https://discuss.elastic.co/u/Hayk_Hovhanisyan)\
**Post date:** [November 21, 2017, 7:46am UTC](https://discuss.elastic.co/t/regexp-query-cannot-escape-reserved-symbols/108527/1 "2017-11-21T07:46:39Z")

</div>

**My environment:**

_ElasticSearch "2.4.4"_  
_Java "1.8.0\_151"_

Hi there, please clarify for me part of escaping reserved symbols . ? + \* | { } [] ( ) "   
I used [backslash](https://www.elastic.co/guide/en/elasticsearch/reference/2.4/query-dsl-regexp-query.html#allowed-characters), but result still empty.

I stored displayName= AB\*CD, and I am trying search b\*c

peace of code which responsible to adding filter:

queryBuilder.filter(boolQuery().must(regexpQuery(columnName, ".\*" + regexValue.toLowerCase() + ".\*")));

```
{  
    "from":0,
    "size":10,
    "query":{  
        "bool":{  
            "must":{  
                "match_all":{  

                }
            },
            "filter":[  
                {  
                    "bool":{  
                        "must":{  
                            "regexp":{  
                                "displayName":{  
                                    "value":".*b\\*c.*",
                                    "flags_value":65535
                                }
                            }
                        }
                    }
                },
                {  
                    "terms":{  
                        "deleted":[  
                            "false"
                        ]
                    }
                }
            ]
        }
    },
    "_source":{  
        "includes":[  
            "id",
            "displayName"
        ],
        "excludes":[  

        ]
    },
    "sort":[  
        {  
            "displayName":{  
                "order":"asc"
            }
        }
    ]
}

```

1. What's wrong here?
2. Do we need use **whitespace analyzer** instead of **standart analyzer**?
3. Do we need change **tokinizer** as well?

---

<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:** [November 21, 2017, 1:37pm UTC](https://discuss.elastic.co/t/regexp-query-cannot-escape-reserved-symbols/108527/2 "2017-11-21T13:37:42Z")

</div>

The standard analyzer splits on `*` so a regular expression that expects to find a `*` would never match any documents. Indeed, a whitespace tokenizer might work.

I'd also like to point out that regexp queries are super slow, especially if there are wildcards in the beginning like here, so I would advise to do things differently if possible. For instance, this particular use-case could be solved by indexing 3-grams.

---

<div class="post-metadata">

**Author:** ![Hayk\_Hovhanisyan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hayk_hovhanisyan/32/77342_2.png) [@Hayk\_Hovhanisyan](https://discuss.elastic.co/u/Hayk_Hovhanisyan)\
**Post date:** [November 22, 2017, 2:28pm UTC](https://discuss.elastic.co/t/regexp-query-cannot-escape-reserved-symbols/108527/3 "2017-11-22T14:28:17Z")

</div>

Thanks for your quick answer Adrien.  
As I understand if we use default analyzer which is standard analyzer, there is no way for searching with reserved symbols at all `. ? + * | { } [] ( ) " \`.

Solution 1: Use indexing 3-grams - it means add some n-gram(in our case 3 grams) tokenizer and so on.  
Solution 2: Use some Pattern tokenizer with our new analyzer?

One more question:

Does this part of documentation not about if we use standard analyzer?

_ **Allowed characters** _

**Any Unicode characters may be used in the pattern, but certain characters are reserved and must be escaped. The standard reserved characters are:**

`. ? + * | { } [] ( ) " \`  
If you enable optional features (see below) then these characters may also be reserved:

`# @ & < > ~`  
Any reserved character can be escaped with a backslash "\*" including a literal backslash character: "\"

**Additionally, any characters (except double quotes) are interpreted literally when surrounded by double quotes:**

`john"@smith.com"`

Thanks a lot for your time.

---

<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:** [November 22, 2017, 3:33pm UTC](https://discuss.elastic.co/t/regexp-query-cannot-escape-reserved-symbols/108527/4 "2017-11-22T15:33:32Z")

</div>

> [@Hayk\_Hovhanisyan](#):
>
> As I understand if we use default analyzer which is standard analyzer, there is no way for searching with reserved symbols at all

This is correct.

> [@Hayk\_Hovhanisyan](#):
>
> Does this part of documentation not about if we use standard analyzer?

This is a generic advice. Obviously it is irrelevant with analyzers that split on those chars. For instance if you field is mapped as a `keyword`, it applies.

---

<div class="post-metadata">

**Author:** ![Hayk\_Hovhanisyan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hayk_hovhanisyan/32/77342_2.png) [@Hayk\_Hovhanisyan](https://discuss.elastic.co/u/Hayk_Hovhanisyan)\
**Post date:** [November 22, 2017, 4:17pm UTC](https://discuss.elastic.co/t/regexp-query-cannot-escape-reserved-symbols/108527/5 "2017-11-22T16:17:36Z")

</div>

Thanks a lot Adrien, will think about solution.  
If will be any update from my side I will put here.

Thanks again for your time and support

---

<div class="post-metadata">

**Author:** ![Hayk\_Hovhanisyan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hayk_hovhanisyan/32/77342_2.png) [@Hayk\_Hovhanisyan](https://discuss.elastic.co/u/Hayk_Hovhanisyan)\
**Post date:** [December 20, 2017, 7:00am UTC](https://discuss.elastic.co/t/regexp-query-cannot-escape-reserved-symbols/108527/6 "2017-12-20T07:00:05Z")

</div>

Hi Adrien one more question about searching.

When we use standard analyzer and have index analyzed, with symbol [space] there is no way for searching as well?  
For instance, firstname [space] surname(ex: Hayk Hovhannisyan), is not returning any results.

if the user enter for example "Ha Hov" then it should find "Hayk Hovhannisyan" as well. And need relations AND.

At this moment I used REGEXP query , and have analyzed field for that.  
What can you suggest me for that?

thanks in advance  
regards Hayk Hovhannisyan

---

<div class="post-metadata">

**Author:** ![Hayk\_Hovhanisyan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hayk_hovhanisyan/32/77342_2.png) [@Hayk\_Hovhanisyan](https://discuss.elastic.co/u/Hayk_Hovhanisyan)\
**Post date:** [December 21, 2017, 12:41pm UTC](https://discuss.elastic.co/t/regexp-query-cannot-escape-reserved-symbols/108527/7 "2017-12-21T12:41:31Z")

</div>

What you think about this ?

One idea is instead of regexp match using Fuzzy Query Match and play with fuzziness and operator properties.  
regards Hayk Hovhannisyan

---

<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 21, 2017, 2:05pm UTC](https://discuss.elastic.co/t/regexp-query-cannot-escape-reserved-symbols/108527/8 "2017-12-21T14:05:50Z")

</div>

The usual way that this would be done would be to use an edge-ngram filter in the index analyzer (but not in the search analyzer) and then use regular match queries for searching.

---

<div class="post-metadata">

**Author:** ![Hayk\_Hovhanisyan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hayk_hovhanisyan/32/77342_2.png) [@Hayk\_Hovhanisyan](https://discuss.elastic.co/u/Hayk_Hovhanisyan)\
**Post date:** [December 22, 2017, 8:56am UTC](https://discuss.elastic.co/t/regexp-query-cannot-escape-reserved-symbols/108527/9 "2017-12-22T08:56:27Z")

</div>

Hi Adrien thanks for quick response.

Did you mean ?  
[Index Time Search As You Type](https://www.elastic.co/guide/en/elasticsearch/guide/current/_index_time_search_as_you_type.html)

---

<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 22, 2017, 9:50am UTC](https://discuss.elastic.co/t/regexp-query-cannot-escape-reserved-symbols/108527/10 "2017-12-22T09:50:15Z")

</div>

Yes, this is a good example.

---

<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 19, 2018, 1:27pm UTC](https://discuss.elastic.co/t/regexp-query-cannot-escape-reserved-symbols/108527/12 "2018-01-19T13:27:11Z")

</div>

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