I may not be comprehending this correctly but when using the Query String Query to search my index, it states that reserved or special characters need to be escaped.
https://www.elastic.co/guide/en/elasticsearch/reference/2.1/query-dsl-query-string-query.html#_reserved_characters
The reserved characters are: + - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ /
Failing to escape these special characters correctly could lead to a syntax error which prevents your query from running."
Currently my index does not tokenize special characters since I am using just the standard analyzer but I do have both analyzed and not analyzed on all string fields.
So if I have these strings indexed:
quick fox
brown fox
When i run a query:
{
"query_string" : {
"query" : "(quick OR brown) AND fox",
}
}
The correct results come back. But based on the reserved characters documentation should the query with the parentheses "(" and ")" need to be escaped in the query string query? I am a bit confused on when to escape the reserved characters and when not to in the query?
Does it need to be?:
{
"query_string" : {
"query" : "\(quick OR brown\) AND fox",
}
}
I know that parentheses are used for grouping so it makes sense not to escape them. But which reserved characters are always needed to be escaped in Elasticsearch's queries? Only when the reserved characters are operators?
Reserved characters only need to be escaped if they are not part of the query syntax.
So in (quick OR brown) AND fox you don't escape anything.
But if for instance your field contains a reserved character that you want to search on, e.g. your field contains Hello! and ! is a reserved character equivalent to a NOT and you want to search for Hello!, then your query needs to be like this
`Hello\! AND blabla`
^^
||
escaped reserved char
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With