Home Ask Login Register

Developers Planet

Your answer is one click away!

moeTi February 2016

Elasticsearch Java API: query string validation

I'm using Elasticsearch Java API (version 2.1.1) with a Query String Query. The query string is entered by the user and may contain syntax errors. Right now it seems like the query is sent to the server, the response is a shardFailure and an exception is thrown.

Is there an easy way to validate the query in the API before sending it to the server? Right now I'm catching the exception, but it does not feel right to bother the server with these queries.

Query is built like this:


Example response:

QueryParsingException[Failed to parse query [error)]]; nested: NotSerializableExceptionWrapper[Cannot parse 'error)': Encountered " ")" ") "" at line 1, column 5.

edit: To be clear: It is totally ok for the query to contain special characters like brackets or quotes. I just want to check if the user entered a correct search query (e.g. all brackets/quotes in pairs).


Richa February 2016

You will have to parse your string and escape all special characters before passing the string to your method.


String escapeSpecialCharacters(String str) {

   str.replaceAll(/[^\sa-zA-Z0-9]/, "\\'");

You can also remove all special characters if you want.

Hope this helps.

Post Status

Asked in February 2016
Viewed 3,706 times
Voted 13
Answered 1 times


Leave an answer

Quote of the day: live life