0

I'm using QueryParser::parse() method to get a query from a string search term for my ZendSearch Lucene index. But I've a problem with the following query:

+php +5.7.1)

This throws the QueryParserException with message:

Syntax Error: mismatched parentheses, every opening must have closing.

So I used QueryParser::escape() to escape the string search term before I pass it to QueryParser::parse() but then it escapes everything so this leads to this string:

\\+\\p\\h\\p\\ \\+\\5\\.\\7\\.\\1\\)

Now the QueryParserException has gone but also the possbility of using special chars like +, -, etc.

I look for a way to just escape special chars which will lead to a QueryParserException so in my case the ) should be escaped because there is no opening bracket ) in the query but my two + should stay untouched.

Is there any possbility to achieve this? Building the query itself without parsing is not an option because the search terms are user inputs.

I tried to use QueryParser::suppressQueryParsingExceptions() which probably would be the thing I'm looking for but it has no effect. The QueryParser still throws a QueryParserException although the default value for this is true.

TiMESPLiNTER
  • 5,741
  • 2
  • 28
  • 64

1 Answers1

0

You could use addcslashes

$escapedParenthesis = addcslashes('+php +5.7.1)','\\)');
user3632495
  • 149
  • 4
  • Well this does the trick in this specfic case. But if I have a query likes this `+php +(5.7.1)` it should not be escaped because it has an opening and a closing bracket. – TiMESPLiNTER Aug 11 '14 at 07:35
  • Can't you escape both? – user3632495 Aug 11 '14 at 07:38
  • No I need them if they occur both because this groups a part of a query and therefor is a valid term. But just a closing bracket not so this should get escaped. – TiMESPLiNTER Aug 11 '14 at 07:40
  • As I see it, you should just throw an error when the user enters an invalid syntax. Doing otherwise leads to this weird scenario, where you don't want to escape anything, unless it's invalid, in which case you escape only the part that's invalid. I think the code is trying to be too smart. – user3632495 Aug 11 '14 at 07:46
  • If you still want to go that way, you'll have to build your own parser. There are no such built in functionalities. – user3632495 Aug 11 '14 at 08:05