Does Lucene QueryParser interpret "AND OR" as a command?

I call Lucene using the following code (more precisely, PyLucene):

analyzer = StandardAnalyzer(Version.LUCENE_30) queryparser = QueryParser(Version.LUCENE_30, "text", analyzer) query = queryparser.parse(queryparser.escape(querytext)) 

But consider whether this is the contents of a querytext :

 querytext = "THE FOOD WAS HONESTLY NOT WORTH THE PRICE. MUCH TOO PRICY WOULD NOT GO BACK AND OR RECOMMEND IT" 

In this case, "AND OR" sends the request, although I use queryparser.escape . How to avoid the following error message?

  Java stacktrace: org.apache.lucene.queryParser.ParseException: Cannot parse 'THE FOOD WAS HONESTLY NOT WORTH THE PRICE. MUCH TOO PRICY WOULD NOT GO BACK AND OR RECOMMEND IT': Encountered " <OR> "OR "" at line 1, column 80. Was expecting one of: <NOT> ... "+" ... "-" ... "(" ... "*" ... <QUOTED> ... <TERM> ... <PREFIXTERM> ... <WILDTERM> ... "[" ... "{" ... <NUMBER> ... <TERM> ... "*" ... at org.apache.lucene.queryParser.QueryParser.parse(QueryParser.java:187) .... at org.apache.lucene.queryParser.QueryParser.generateParseException(QueryParser.java:1759) at org.apache.lucene.queryParser.QueryParser.jj_consume_token(QueryParser.java:1641) at org.apache.lucene.queryParser.QueryParser.Clause(QueryParser.java:1268) at org.apache.lucene.queryParser.QueryParser.Query(QueryParser.java:1207) at org.apache.lucene.queryParser.QueryParser.TopLevelQuery(QueryParser.java:1167) at org.apache.lucene.queryParser.QueryParser.parse(QueryParser.java:182) 
+4
source share
3 answers

This is not just OR , it is AND OR .

I am using the following workaround:

 query = queryparser.parse(queryparser.escape(querytext.replace("AND OR", "AND or"))) 
+1
source

queryparser.parse only escapes special characters (as shown in this page ) and leaves "AND OR" unchanged, so this will not work in your case. Since you supposedly also used StandardAnalyzer to parse your text, the terms in your index are already lowercase. This way you can change the entire query string to lowercase, and then pass it to queryparser. "Lower case" and "and" or "are not considered operators, therefore," and "will not disable request-request."

+1
source

I understand that I'm attending the party quite late here, but adding quotes around the search bar is the best option:

 querytext = "\"THE FOOD WAS ... \"" 
0
source

All Articles