2016-01-28 94 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」的結果也是),但是對於這個特定的分數減少了。