ElasticSearch bool should_not过滤

nog*_*o0d 4 boolean range elasticsearch

我是弹性搜索的新手,所以我的例子是:

bool过滤器有3个部分:

must
    All of these clauses must match. The equivalent of AND. 
must_not
    All of these clauses must not match. The equivalent of NOT. 
should
    At least one of these clauses must match. The equivalent of OR. 

如何预先形成一个should_not查询?

提前致谢 :)

Prz*_*wak 15

为了获得类似"should_not"的内容,请尝试以下查询:

GET /bank/account/_search
{
   "size": 2000,
   "query": {
      "bool": {
          "must": {
             "match_all": {}
          }, 
         "should": {
            "bool": {
               "must_not": {
                  "match": {
                     "city": "XYZ"
                  }
               }
            }
         }
      }
   }
}

在这种情况下,需要"必须"条款来检索所有结果(城市"XYZ"的结果也是如此),但该特定结果的分数降低.


Bro*_*keB 5

这取决于您希望“ should_not”发挥作用的精确程度。

由于should大致等效于布尔OR(即,返回A或B或C为true的文档),因此想到“ should_not”的一种方法大致等效于NOT(A OR B OR C)。在布尔逻辑中,这与NOT A AND NOT B AND NOT C相同。在这种情况下,只需将所有子句添加到must_notbool过滤器的这一部分即可实现“ should_not”行为。

  • 这意味着完成should_not要求您始终将子句包装在另一个bool查询中。这可行,但从API角度来看很烦人。您只需要否定子句,但是您正在使用Bool查询来完成它。当您只有一个子句时,这感觉很奇怪。例如,您想将文档返回到(A∨B∨(¬C))。在elasticsearch中已弃用了NOT查询,并在Bool查询中将其替换为must_not之类的调用,但它们从未添加should_not。 (2认同)

Nis*_*ant 0

您可以应用一个 not 过滤器,然后应用一个should 过滤器来执行should_not 操作。

  "filter": {
       "not": {
          "filter": {
              "bool": {
                  "should": [
                     {}
                  ]
              }
          }
       }
   }
Run Code Online (Sandbox Code Playgroud)