Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly escape OR and AND in lucene query?

When I passed in a query "state:OR" lucene gave an error because it considers "OR" as a keyword for boolean clause, but here I actually man the abbreviation of Oregon, the state.

I have seen that quoting OR so the query becomes 'state:"OR"' makes it work.

but this doesn't sound like a very good approach, since I'll have to do a string substitution for EACH of the keywords that lucene uses: AND OR NOT and others?? I don't how many

I tried directly constructing the query instead of doing queryParser.parse(), but it seems that this does not go through the analyzers, which is a big problem.

like image 913
teddy teddy Avatar asked Apr 26 '12 16:04

teddy teddy


2 Answers

There are a number of ways to escape this, the cleaner is to escape AND, OR, & NOT with leading backslashes eg:

\\AND \\OR \\NOT

alternately, the code parser will not parse their lowercase equivalents as operators

like image 171
adam Avatar answered Oct 17 '22 10:10

adam


There are only 3 standalone keywords in the Lucene query syntax -- AND, OR, and NOT. ("TO" is also used, but is only recognized inside of a range query.)

It may help that your quoting code only needs to recognize the Lucene keywords actually used as terms in your application (like the "OR" above in your example).

like image 4
Mark Leighton Fisher Avatar answered Oct 17 '22 08:10

Mark Leighton Fisher