2015-11-13 84 views
1
 POST /test/topic/_search 
     { 
      "query": { 
      "bool": { 
       "must": [ 
       { 
        "multi_match": { 
        "query": "Predisposition", 
        "fields": [ 
         "_all" 
        ] 
        } 
       }, 
       { 
        "multi_match": { 
        "query": "thrombosis", 
        "fields": [ 
         "_all" 
        ] 
        } 
       } 
       ], 
       "should": [ 
       { 
        "multi_match": { 
        "query": "cancer", 
        "fields": [ 
         "_all" 
        ] 
        } 
       } 
       ] 
      } 
      } 
     } 

我的上述查詢的理解是,它必須在predispositionANDthrombosisORcancer匹配,但是我只得到的匹配predispositionANDthrombosis的文檔了一把,我期待大量cancer文件但零。我錯過了什麼?布爾查詢工作不正常

回答

2

must需要總是匹配。 should只會提高分數如果它匹配。

另外,還有一種情況是沒有must語句,在這種情況下至少有一個should必須匹配。

我認爲你正在尋找的下面,而不是:

{ 
    "query": { 
    "bool": { 
     "should": [ 
     { 
      "bool": { 
      "must": [ 
       { 
       "multi_match": { 
        "query": "Predisposition", 
        "fields": [ 
        "_all" 
        ] 
       } 
       }, 
       { 
       "multi_match": { 
        "query": "thrombosis", 
        "fields": [ 
        "_all" 
        ] 
       } 
       } 
      ] 
      } 
     }, 
     { 
      "bool": { 
      "must": [ 
       { 
       "multi_match": { 
        "query": "cancer", 
        "fields": [ 
        "_all" 
        ] 
       } 
       } 
      ] 
      } 
     } 
     ] 
    } 
    } 
} 
2

您正在搜索的方式是,文件必須有傾向和血栓形成無論癌症,因爲他們在裏面必須過濾。 你基本上需要來包裝你must clauseshould clause這樣

{ 
    "query": { 
    "bool": { 
     "should": [ 
     { 
      "bool": { 
      "must": [ 
       { 
       "multi_match": { 
        "query": "predisposition", 
        "fields": "_all" 
       } 
       }, 
       { 
       "multi_match": { 
        "query": "thrombosis", 
        "fields": "_all" 
       } 
       } 
      ] 
      } 
     }, 
     { 
      "multi_match": { 
      "query": "cancer", 
      "fields": "_all" 
      } 
     } 
     ] 
    } 
    } 
} 

這會給你想要的結果。