2010-08-10 107 views
4

我使用下面的代碼(PyLucene,要準確)調用的Lucene:Lucene QueryParser將'AND OR'解釋爲命令?

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" 

在這種情況下,「AND OR 「儘管我正在使用queryparser.escape,但是卻跳過了查詢分析器。我如何避免以下錯誤消息?

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) 

回答

1

這不只是OR,這是AND OR

我用以下解決方法:

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

queryparser.parse只有逃跑的特殊字符(如圖this page)和葉「和或」不變,所以它不會工作你的情況。由於您大概也使用了StandardAnalyzer來分析您的文本,因此您的索引中的術語已經小寫。因此,您可以在將整個查詢字符串提交給queryparser之前將其更改爲小寫。小寫「和」和「或」不被認爲是運算符,所以「和或」不會使查詢分析器跳閘。

0

我意識到我寧願遲到了這裏,但把報價一輪搜索字符串是一個更好的選擇:

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