How to properly escape OR and AND in lucene query?
Asked Answered
M

3

8

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.

Monoplane answered 26/4, 2012 at 16:53 Comment(1)
You could analyze the term before constructing the query manuallyLamb
K
9

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

Kumler answered 25/5, 2012 at 16:17 Comment(1)
So I was doing something like this searchTerm.replaceAll("\\b(AND|OR|NOT)\\b", "\\\\\\\\$1"); to escape a literal AND used in a user entered search box which ultimately ends up in a solr query - what I found was that escaping the AND by pre-pending the \\ works in that there is no longer a parse exception but it doesn't actually search for the literal string. In Solr speak, the parameter name:\\AND in a query doesn't find any results when it should return entries with the name "Jim and john". Lower case might be the way to go, at least in solr!Gnomon
P
5

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).

Palaearctic answered 27/4, 2012 at 11:49 Comment(0)
A
-1

Just to make a resume, it may help other peoples when looking for the answer for this problem.

The answer that should be the right one is the comment provided by icyitscold

We need to use the reserved words (OR, AND, NOT) in lower case ( and, or, to ), it doesn't give any error and do correctly the search.

Acentric answered 29/7, 2020 at 19:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.