Lucene QueryParser将’AND OR’解释为命令?

我使用以下代码调用Lucene(确切地说是PyLucene):

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

但请考虑这是否是querytext的内容:

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

在这种情况下,即使我使用queryparser.escape,“AND OR”也会使查询者绊倒.如何避免以下错误消息?

    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)

最佳答案 它不只是OR,它是AND OR.

我使用以下解决方法:

query = queryparser.parse(queryparser.escape(querytext.replace("AND OR", "AND or")))
点赞