2016-01-28 153 views
2

我在elasticsearch一个新手,所以我querion是:ElasticSearch布尔should_not过滤

有3个部分的bool过滤器:

 
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查询?

感谢提前:)

+0

请解释您的'should_not'的定义 – Richa

回答

0

您可以应用一个不过滤,然后应该筛选should_not操作。

"filter": { 
     "not": { 
      "filter": { 
       "bool": { 
        "should": [ 
        {} 
        ] 
       } 
      } 
     } 
    } 
+0

我使用的是弹性搜索2.1,我认为NOT Query在2.1中已被弃用,https://www.elastic.co/guide/en/elasticsearch/reference/current /query-dsl-not-query.html – nogo0d

2

这取决于您希望“should_not”功能如何。

由于should大致等价于一个布尔OR(即返回A或B或C为真的文档),那么认为“should_not”的一种方式大致等于NOT(A或B或C)。在布尔逻辑中,这与NOT A AND NOT B和NOT C相同。在这种情况下,只需将所有子句添加到bool过滤器的must_not部分即可实现“should_not”行为。

+1

这意味着完成should_not要求您始终将该子句包装在另一个bool查询中。这有效,但从API的角度来看令人讨厌。您只需要否定子句,但是您使用Bool查询来完成它。当你只有一个条款时,这感觉很奇怪。例如,您想要返回(A∨B∨(¬C))的文档。 NOT查询在elasticsearch中被弃用,并且在Bool查询中被替换为must_not,但他们从未添加过should_not。 – eferrari

6

为了得到这样的“should_not”请尝试以下查询:

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

在这种情况下,该条款“必须”要求取回所有结果(与城市“XYZ”的结果也是),但是对于这个特定的分数减少了。