# How to use Escape key value in query\_string

**URL:** <https://discuss.elastic.co/t/how-to-use-escape-key-value-in-query-string/327972>\
**Category:** Elasticsearch\
**Created:** [March 17, 2023, 8:17pm UTC](https://discuss.elastic.co/t/how-to-use-escape-key-value-in-query-string/327972 "2023-03-17T20:17:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon55421226](https://avatars.discourse-cdn.com/v4/letter/a/8491ac/32.png) [@anon55421226](https://discuss.elastic.co/u/anon55421226)\
**Post date:** [March 17, 2023, 8:17pm UTC](https://discuss.elastic.co/t/how-to-use-escape-key-value-in-query-string/327972/1 "2023-03-17T20:17:27Z")

</div>

So... according to the [elasticsearch/QueryStringQueryBuilder.java at v7.16.3 · elastic/elasticsearch · GitHub](https://github.com/elastic/elasticsearch/blob/v7.16.3/server/src/main/java/org/elasticsearch/index/query/QueryStringQueryBuilder.java#L855) code there should exists an escape parameter in the query\_string object, but how do i trigger it to escape my text in the query?

Locally I just tried to boot up a container image with podman

```bash
podman run --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.16.3

```

After i hade confirmed that it was running with

```bash
$ curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "query_string": {
      "query": "(new york city) OR (big apple)",
      "default_field": "content"
    }
  }
}'
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 0,
    "successful" : 0,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : []
  }
}

```

After the verify I modified the query with the characters that need to be escaped and added the escape flag

```bash
$ curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "query_string": {
      "query": "+ - = && || > < ! ( ) { } [] ^ \" ~ * ? : \ /",
      "default_field": "content",
      "escape" : true
    }
  }
}'
{
  "error" : {
    "root_cause" : [
      {
        "type" : "json_parse_exception",
        "reason" : "Unrecognized character escape ' ' (code 32)\n at [Source: (org.elasticsearch.common.io.stream.ByteBufferStreamInput); line: 5, column: 62]"
      }
    ],
    "type" : "json_parse_exception",
    "reason" : "Unrecognized character escape ' ' (code 32)\n at [Source: (org.elasticsearch.common.io.stream.ByteBufferStreamInput); line: 5, column: 62]"
  },
  "status" : 400
}

```

And also tried to just escape the "

```bash
$ curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "query_string": {
      "query": "+ - = && || > < ! ( ) { } [] ^ \" ~ * ? : \ /",
      "default_field": "content",
      "escape" : true
    }
  }
}'
{
  "error" : {
    "root_cause" : [
      {
        "type" : "json_parse_exception",
        "reason" : "Unrecognized character escape ' ' (code 32)\n at [Source: (org.elasticsearch.common.io.stream.ByteBufferStreamInput); line: 5, column: 62]"
      }
    ],
    "type" : "json_parse_exception",
    "reason" : "Unrecognized character escape ' ' (code 32)\n at [Source: (org.elasticsearch.common.io.stream.ByteBufferStreamInput); line: 5, column: 62]"
  },
  "status" : 400
}

```

So how does the `"escape": true` keyword work?

Also got this keyword from [query string - What is escape property in query\_string in elasticsearch - Stack Overflow](https://stackoverflow.com/questions/69149461/what-is-escape-property-in-query-string-in-elasticsearch) since we are currently doing string escaping in our local quarkus application before sending the request to our Elasticsearch server.

---

<div class="post-metadata">

**Author:** ![jba](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jba/32/118482_2.png) [@jba](https://discuss.elastic.co/u/jba)\
**Post date:** [March 18, 2023, 10:04pm UTC](https://discuss.elastic.co/t/how-to-use-escape-key-value-in-query-string/327972/2 "2023-03-18T22:04:18Z")

</div>

Why did you change your query from searching for two cities to search for `+ - = && || > < ! ( ) { } [] ^ \" ~ * ? : \ /`?

---

<div class="post-metadata">

**Author:** ![anon55421226](https://avatars.discourse-cdn.com/v4/letter/a/8491ac/32.png) [@anon55421226](https://discuss.elastic.co/u/anon55421226)\
**Post date:** [March 19, 2023, 5:49am UTC](https://discuss.elastic.co/t/how-to-use-escape-key-value-in-query-string/327972/3 "2023-03-19T05:49:02Z")

</div>

Should have writing about that yeah... But the change to `+ - = && || > < ! ( ) { } [] ^ \" ~ * ? : \ /` was because I rembered json standard formatting or a valid json key value pair containing text needs the `"` to be escaped if I want it to send `+ - = && || > < ! ( ) { } [] ^ \" ~ * ? : \ /` to a server, when the `"` was not escaped then the value for query was `+ - = && || > < ! ( ) { } [] ^ ` instead and that also caused a different error response.

And missed the point of your question completely 😅 going to revert it to the correct error and curl call.

---

<div class="post-metadata">

**Author:** ![anon55421226](https://avatars.discourse-cdn.com/v4/letter/a/8491ac/32.png) [@anon55421226](https://discuss.elastic.co/u/anon55421226)\
**Post date:** [March 19, 2023, 6:01am UTC](https://discuss.elastic.co/t/how-to-use-escape-key-value-in-query-string/327972/4 "2023-03-19T06:01:45Z")

</div>

No wait I'm just confused. The first test/call with the cities was just to validate that the server was up and I just copied the request from the elasticsearch homepage. My intention all along was to send `+ - = && || > < ! ( ) { } [] ^ \" ~ * ? : \ /` or `+ - = && || > < ! ( ) { } [] ^ " ~ * ? : \ /` to elasticsearch to try out the escape key value pair.

---

<div class="post-metadata">

**Author:** ![jba](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jba/32/118482_2.png) [@jba](https://discuss.elastic.co/u/jba)\
**Post date:** [March 19, 2023, 3:55pm UTC](https://discuss.elastic.co/t/how-to-use-escape-key-value-in-query-string/327972/5 "2023-03-19T15:55:38Z")

</div>

I think you are trying too much at the same time. If you are testing how the automatic escape mechanism works for say `(` and `)`, try with those first. Heck, try with just one. Then add the next. And the next.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [March 19, 2023, 4:56pm UTC](https://discuss.elastic.co/t/how-to-use-escape-key-value-in-query-string/327972/6 "2023-03-19T16:56:21Z")

</div>

What is it you are looking to achieve? Are you querying a keyword or text (analyzed) field?

---

<div class="post-metadata">

**Author:** ![anon55421226](https://avatars.discourse-cdn.com/v4/letter/a/8491ac/32.png) [@anon55421226](https://discuss.elastic.co/u/anon55421226)\
**Post date:** [March 20, 2023, 9:14am UTC](https://discuss.elastic.co/t/how-to-use-escape-key-value-in-query-string/327972/7 "2023-03-20T09:14:24Z")

</div>

What I want to achieve since I'm not the one who coded this in our current application is to use elastics built in replace function or escape functionality instead of building our own. In our case it would be quarrying a Json object stored in elastic.

---

<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 17, 2023, 9:15am UTC](https://discuss.elastic.co/t/how-to-use-escape-key-value-in-query-string/327972/8 "2023-04-17T09:15:05Z")

</div>

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