# Cannot search double quotes

**URL:** <https://discuss.elastic.co/t/cannot-search-double-quotes/123429>\
**Category:** Elasticsearch\
**Created:** [March 11, 2018, 2:37pm UTC](https://discuss.elastic.co/t/cannot-search-double-quotes/123429 "2018-03-11T14:37:24Z")\
**Posts on this page:** 4\
**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:** [March 11, 2018, 2:37pm UTC](https://discuss.elastic.co/t/cannot-search-double-quotes/123429/1 "2018-03-11T14:37:24Z")

</div>

Hi folk,

Elastic Search version 2.4  
Java version 1.8

Following error when search via `"`  
`java.lang.IllegalArgumentException: expected '"' at position 7`.

> **Search via double quotes query body**
>
> ```
> {
> "from":0,
> "size":10,
> "query":{
> "bool":{
> "must":{
> "match_all":{
> 
> }
> },
> "filter":[
> {
> "bool":{
> "filter":{
> "regexp":{
> "ppsn":{
> "value":".*\"\"\".*",
> "flags_value":65535
> }
> }
> }
> }
> },
> {
> "bool":{
> "must":{
> "term":{
> "deleted":"false"
> }
> }
> }
> }
> ]
> }
> }
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![yaauie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yaauie/32/23363_2.png) [@yaauie](https://discuss.elastic.co/u/yaauie)\
**Post date:** [March 11, 2018, 11:40pm UTC](https://discuss.elastic.co/t/cannot-search-double-quotes/123429/2 "2018-03-11T23:40:16Z")

</div>

The character `"` carries special semantic meaning in the Lucene regexp engine that means something like "treat everything until the next `"` as a literal character, not as a pattern expression", or if already in a literal expression, means "this is the end of the literal expression" ([docs](https://www.elastic.co/guide/en/elasticsearch/reference/2.4/query-dsl-regexp-query.html)):

Let's start with the expression in your JSON query:

```json
".*\"\"\".*"

```

After being parsed into the string it represents, we get:

```auto
.*""".*

```

This is given to the Lucene regexp engine, which parses it to mean:

- `.*` any sequence, followed by
- `"` a literal sequence (everything until the next `"`)
  - `"` literal sequence ends

- `"` a literal sequence (everything until the next `"`)
  - `.` a literal dot
  - `*` a literal asterisk
  - _UNEXPECTED END_: no matching close `"`.

You _may_ be able to escape the literal double-quote inside the literal-sequence by prefixing it with a backslash (e.g., `.*"\"".*`, which itself would get escaped again when being converted to JSON to be `".*\"\\\"\".*"`), but the escaping of double-quotes _inside_ double-quote sequence isn't clearly documented, so that may or may not work:

> `" <Unicode string without double-quotes> "	(a string)`  
> -- [Lucene Atomaton Regexp](http://lucene.apache.org/core/7_0_1/core/org/apache/lucene/util/automaton/RegExp.html)

My guess is that you're taking arbitrary input and simply concatenating (`.*"` + _input_ + `".*`). You may be able to avoid double-quote entirely by concatenating (`.*` + _quote(input)_ + `.*`), where _quote_ is some function that escapes all characters with special meaning by prefixing them with a backslash (`\`).

---

<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:** [March 12, 2018, 7:45am UTC](https://discuss.elastic.co/t/cannot-search-double-quotes/123429/3 "2018-03-12T07:45:44Z")

</div>

Hi @yaauie,  
thanks for nice and deeply explanation.

> [@My guess](#):
>
> I think we can escape `"` via `\` backslash, and other reserved characters via `""` or replace all reserved symbols via `\` and it will works fine.
> 
> String quotes= StringUtils.replaceAll(regexValue, """, "\\"");  
> String asterisks = StringUtils.replaceAll(escapeAllQuotes, "\_", ""_""); // + - = && || \> \< ! ( ) { } ^ ~ \* ? : \ /  
> String regexResult= asterisks.toLowerCase();  
> QueryBuilders.regexpQuery(columnName, "._" + regexResult + "._");

regards

---

<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:** [April 9, 2018, 7:45am UTC](https://discuss.elastic.co/t/cannot-search-double-quotes/123429/4 "2018-04-09T07:45:49Z")

</div>

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