布尔运算符的奇怪Solr/Lucene行为 [英] Weird Solr/Lucene behaviors with boolean operators

查看:59
本文介绍了布尔运算符的奇怪Solr/Lucene行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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尝试解决此问题,如果要求执行一个布尔查询,该布尔查询仅在最顶层包含否定子句 ,则会添加一个匹配所有文档的查询(即:*:*)

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,该嵌套的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆