solr 查询字符串中的否定运算符(NOT,-,!)不适用于括号

Negative operator(NOT,- , !) in solr query string doesn't work with parentheses

我正在使用 solr 6.6.0,这里是 collection.

中的文档
{"id":1,"content":test1"}
{"id":2,"content":test2"}
{"id":3,"content":test3"}

假设我想包含不包含 "test1" 和 "test2" 的文档,根据参考文献的 Grouping Terms to Form Sub-Queries 部分,按照以下方式编写查询字符串似乎是合法的指南.

content:((NOT "test1") AND (NOT "test2"))

查询结果符合预期return只有文档#3,但实际结果是空的。

或者,如果将上述查询更改为以下内容,"NOT expressions" 不带括号,则预期结果为 returned.

content:(NOT "test1" AND NOT "test2")

我的问题是,为什么第一个查询字符串没有按预期方式工作?

Solr 当前检查 "pure negative" 查询并插入 *:*(匹配所有文档)以便后一种格式(不带括号)正常工作。

请参阅下面来自 org.apache.solr.search.QueryUtils.java

的代码片段
/** Fixes a negative query by adding a MatchAllDocs query clause.
  * The query passed in *must* be a negative query.
  */
 public static Query fixNegativeQuery(Query q) {
   BooleanQuery newBq = (BooleanQuery)q.clone();
   newBq.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
   return newBq;
 }

所以NOT "test"被solr转化为(*:* NOT "test")

但是 Solr 只检查顶级查询,所以这意味着像 (NOT "test1") 这样的查询没有改变,因为纯否定查询不在顶级。 这就是为什么前一种格式(带括号的格式)无法按预期工作的原因。

因此,我们一般可以得出结论,使用 NOT 运算符的正确方法是 (*:* NOT some_expression) 形式,而不是单一的 NOT some_expression.