ElasticSearch query_string fails to parse query with some characters
I know I am late enough but I am posting here and I hope it'll help others. As we know from the Elasticsearch documentation here ES has some reserved characters.
The reserved characters are: + - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ /
So, now you've two possible solutions to fix it. These are working perfectly for me when I encountered special character issue
Solution 1: Wrap your special characters with \\
"query": { "bool": { "must": [ { "match": { "country_code.keyword": "IT" } }, { "query_string": { "default_field": "display", "query": "Magomadas \\(OR\\), Italy" } } ] } }
Solution 2: Use simple_query_string
with no change on your query
but it doesn't support default_field
, so you can use fields
instead.
"query": { "bool": { "must": [ { "match": { "country_code.keyword": "IT" } }, { "simple_query_string": { "fields": ["display"], "query": "Magomadas (OR), Italy" } } ] } }
I was reading the documentation and the query_string
is more strict. The following are reserved characters: + - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ /
So, like jhilden said, I would have to escape them or use simple_query_string
instead.
Docs: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html
As mentioned in prev answers, some characters need to be escaped;
+ - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ /
💡
"query": "my:name*&&"
should be"query": "my\\:name\\*\\&&"
Regex to the rescue ✨
With the help of a simple regex, we can easily escape these characters
Python
import redef escape_elasticsearch_query(query): return re.sub('(\+|\-|\=|&&|\|\||\>|\<|\!|\(|\)|\{|\}|\[|\]|\^|"|~|\*|\?|\:|\\|\/)', '\\\\\\1', query)query = 'my:name*&&'escaped_query = escape_elasticsearch_query(query)print(escaped_query)
output:
my\:name\*\&&
Javascript
function escapeElasticsearchQuery(query) { return query.replace(/(\+|\-|\=|&&|\|\||\>|\<|\!|\(|\)|\{|\}|\[|\]|\^|"|~|\*|\?|\:|\\|\/)/g, '\\$&');}let query = 'my:name*&&';let escapedQuery = escapeElasticsearchQuery(query);console.log(escapedQuery);
output:
my\:name\*\&&