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.