带有布尔运算符的奇怪 Solr/Lucene 行为 [英] Weird Solr/Lucene behaviors with boolean operators
问题描述
我在使用 Solr/Lucene 查询语法中的 - 运算符时遇到了奇怪的行为.如果我执行查询
I'm crashing into a weird behavior with - operators in Solr/Lucene query syntax. If I execute the query
-text AND -text
我得到了所有预期的结果(很多),但是如果我加上一些括号,比如
I get all expected results (lot), but if I put some parenthesis like
-text AND (-text)
或
(-text) AND (-text)
然后我根本没有得到任何结果.我不明白为什么.你对这种行为有什么解释吗?
then I get no results at all. I can't understand why. Do you have an explanation for this behavior?
提前致谢.
推荐答案
这个问题已经在Solr 邮件列表.他们还在官方常见问题解答中添加了一个条目,内容为:
The question have been answered very well in Solr mailing list. They have also added an entry in the offical FAQ, that says:
布尔查询必须至少有一个正"查询.表达式(即;必须或应该)以匹配.Solr 试图对此提供帮助,如果要求执行仅包含否定子句在最顶层的 BooleanQuery,它会添加一个匹配所有文档的查询(即:*:*)
Boolean queries must have at least one "positive" expression (ie; MUST or SHOULD) in order to match. Solr tries to help with this, and if asked to execute a BooleanQuery that does contains only negatived clauses at the topmost level, it adds a match all docs query (ie: *:*)
如果顶级 BoolenQuery 在其内部某处包含一个仅包含否定子句的嵌套 BooleanQuery,则不会修改该嵌套查询,并且它(根据定义)不匹配任何文档——如果需要,意味着外部查询将不匹配.
If the top level BoolenQuery contains somewhere inside of it a nested BooleanQuery which contains only negated clauses, that nested query will not be modified, and it (by definition) an't match any documents -- if it is required, that means the outer query will not match.
只有否定"的表达式值总是返回 0 结果,除了在最顶层,解析器在查询的开头默默地添加一个 *:*
.
So expressions with only "negative" values return always 0 results, except at the topmost level, where the parser silently add a *:*
at the beginning of the query.
因此 -text AND -text
被转换为 *:* -text AND -text
所以它有结果,而(-text)
不会转换为 (*:* -text)
,因为它不在最顶层,所以 (-text)
没有给出任何结果.
Therefore -text AND -text
is transformed to *:* -text AND -text
and so it has results, while
(-text)
isn't transformed to (*:* -text)
, because it is not at the topmost level, and so (-text)
gives no results.
这篇关于带有布尔运算符的奇怪 Solr/Lucene 行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!