Search service failing to parse query when using value containing "OR"
[Background]
There was a problem with query parsing in search Api query builder. When [POST] query request is sent from /query
endpoint in Search Service
with this query below in the request payload, it is giving 400 error. There was a problem while parsing this query, the problem was with the string which contains "OR" or "AND" confusing it as an operator in the compilation not as a part of the string.
{
"kind": "osdu:wks:reference-data--UnitOfMeasure:*",
"limit": 30,
"query": "data.Code.keyword:\"GOR\" OR (nested(data.NameAlias, (AliasName.keyword:(\"FOO\"))))",
"returnedFields": [
"id"
],
"queryAsOwner": false,
"offset": 0
}
will result in search service to fail:
{"code":400,"reason":"Bad Request","message":"token_mgr_error: Lexical error at line 1, column 21. Encountered: after : \"\\\"G\""}'
Any other value than the string contains "OR" or "AND" such "GOR, GAND" works fine such as "GIF", "GAD" and etc. The logic problem was in parseQueryNodesFromQueryString method inside QueryParserUtil
class, while compiling and matching the patterns with "OR" or "AND" in pattern compiling.
To solve the issue, white space or bracket such following regex was added [\s)]
before and after "OR" and "AND" while Pattern compilation making sure it looks for an "OR" or "AND" operator with space before and after in the sent request query. And, for orPositions or andPositions, to compare current "OR" or "AND" position with space with query character position, to match it, for matcher start position 1 should be added.