如何在Lucene查询中正确转义OR和AND? [英] How to properly escape OR and AND in lucene query?
问题描述
当我在查询中传递"state:OR"时,lucene给出了一个错误,因为它认为"OR"为 boolean子句的关键字,但实际上,我是俄勒冈州(州)的缩写.
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.
我已经看到引用OR使得查询变为'state:"OR"'使它起作用.
I have seen that quoting OR so the query becomes 'state:"OR"' makes it work.
但这听起来不是一个很好的方法,因为我必须对Lucene使用的每个关键字字符串进行替换:AND OR NOT和其他?我没有多少
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
我尝试直接构造查询而不是执行queryParser.parse(),但是看来这并没有通过分析器,这是一个大问题.
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.
推荐答案
有很多方法可以避免这种情况,更清洁的方法是避免AND,OR和&不能带有反斜杠,例如:
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
这篇关于如何在Lucene查询中正确转义OR和AND?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!