How to use Escape key value in query_string

So... according to the elasticsearch/QueryStringQueryBuilder.java at v7.16.3 · elastic/elasticsearch · GitHub 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

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

$ 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

$ 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 "

$ 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 since we are currently doing string escaping in our local quarkus application before sending the request to our Elasticsearch server.

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

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 :sweat_smile: going to revert it to the correct error and curl call.

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.

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.

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

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.

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