I am performing a Query String Query on a Text Field. When I use a query with wrong syntax (e.g., "/xyz") I get an exception indicating that ElasticSearch failed to parse the query on all shards:
Caused by: java.util.concurrent.ExecutionException: RemoteTransportException[[PZCU_1Z][127.0.0.1:9300][indices:data/read/search]]; nested: SearchPhaseExecutionException[all shards failed]; nested: QueryShardException[Failed to parse query [/xyz]]; nested: NotSerializableExceptionWrapper[parse_exception: Cannot parse '/xyz': Lexical error at line 1, column 5. Encountered: <EOF> after : "/xyz"]; nested: NotSerializableExceptionWrapper[token_mgr_error: Lexical error at line 1, column 5. Encountered: <EOF> after : "/xyz"];
Is there a way to detect that this query has a wrong syntax before performing the query itself. Something like parsing the query in advance or matching it against a regular expression?
Update:
Sorry if my question was unclear. I am aware of the fact that this query has a wrong syntax as it uses some of the reserved characters and it, actually, was intended to be used as a regex query. What I need though is to provide a meaningful error message to the client indicating that his query has a wrong syntax before performing the query.
try-catch