Unrecognized character escape in elasticsearch Unrecognized character escape in elasticsearch elasticsearch elasticsearch

Unrecognized character escape in elasticsearch


Regexp query in elasticsearch is not fully flexible.
For example \w matches any word character in normal regex convention, but in elasticsearch you can not represent \w since \ is a reserved character in elasticsearch.

To make \w valid in elasticsearch, we have to escape using \ which will convert your regex to \\\w. Now this \\\w alters the meaning of your regex.

It will match "\" followed by "w" rather than matching word character.

My suggestion is replace \w in your regex with [a-zA-Z0-9_]. This will work. And also you can not use ^ for a single character. Remove that in your regex and your query would be

 { "query": {   "constant_score": {     "filter": {       "bool": {         "must": [           {             "regexp": {               "displayName" : "(J[a-zA-Z0-9_]+| J([a-zA-Z0-9_]+))"             }           }         ]       }     }   } } }


Acc. to the Elasticsearch regex documentation, its syntax does not support shorthand character classes so common in other regex flavors, so you can't use \w, you can only use character classes (or bracket expressions) like [a-zA-Z] to match letters, or [a-zA-Z0-9_] to match what \w matches in JavaScript.

Next, ^ and $, also common in other flavors, are not supported by ES regex. The whole pattern is anchored by default, thus these are not even necessary.

Now, you want any word having J inside. There are several options:

  1. ".*J.*" will match any string that contains J
  2. ".*J[a-zA-Z].*" will match any string that contains J and then a letter
  3. "J[a-zA-Z].*|.* J[a-zA-Z].*" will match any string that starts with J and then a letter, and then any characters, or any string that contains a space, J, and any letter after it.