2014-11-04 61 views
0

是否可以合併兩個不同的bool過濾器?例如,給定下列文件:將多個應該查詢和必須查詢作爲子查詢

| Name    | Location | 
|=====================|==========| 
| Bordeaux Red Wine | France | 
| Bordeaux Red Wine | France | 
| Bordeaux White Wine | France | 
| Niagara Red Wine | Canada | 
| Niagara White Wine | Canada | 
| Naples Red Wine  | Italy | 
| Naples White Wine | Italy | 

下面的查詢返回具有更強的重量紅葡萄酒的法國紅葡萄酒:

{ 
    "bool": { 
    "must": [ 
     { "term": { "name" : "red" } } 
    ], 
    "should": [ 
     { "term": { "location": "France" }}, 
    ], 
    } 
} 

下面的查詢返回的白葡萄酒具有更強的權重加拿大白葡萄酒:

{ 
    "bool": { 
    "must": [ 
     { "term": { "name" : "white" } } 
    ], 
    "should": [ 
     { "term": { "location": "Canada" }}, 
    ], 
    } 
} 

是否有可能交織結果而不運行多個查詢?我期待找到一個將法國白葡萄酒和加拿大紅葡萄酒評分爲1.0,其他法國和加拿大葡萄酒評分爲0.5,意大利葡萄酒評分爲0.0的查詢。

回答

2

相當長,但它應該首先,用等得分,其他法國和加拿大第二並用等得分,意大利最後用相等得分訂購葡萄酒,法國白人和加拿大紅:

{ 
    "query": { 
    "bool": { 
     "should": [ 
     { 
      "constant_score": { 
      "query": { 
       "bool": { 
       "should": [ 
        { 
        "bool": { 
         "must": [ 
         { 
          "term": { 
          "name": "white" 
          } 
         }, 
         { 
          "term": { 
          "location": "france" 
          } 
         } 
         ] 
        } 
        }, 
        { 
        "bool": { 
         "must": [ 
         { 
          "term": { 
          "name": "red" 
          } 
         }, 
         { 
          "term": { 
          "location": "canada" 
          } 
         } 
         ] 
        } 
        } 
       ] 
       } 
      }, 
      "boost": 1 
      } 
     }, 
     { 
      "constant_score": { 
      "query": { 
       "bool": { 
       "should": [ 
        { 
        "bool": { 
         "must": [ 
         { 
          "term": { 
          "location": "france" 
          } 
         } 
         ] 
        } 
        }, 
        { 
        "bool": { 
         "must": [ 
         { 
          "term": { 
          "location": "canada" 
          } 
         } 
         ] 
        } 
        } 
       ] 
       } 
      }, 
      "boost": 0.5 
      } 
     }, 
     { 
      "constant_score": { 
      "query": { 
       "bool": { 
       "should": [ 
        { 
        "bool": { 
         "must": [ 
         { 
          "term": { 
          "location": "italy" 
          } 
         } 
         ] 
        } 
        } 
       ] 
       } 
      }, 
      "boost": 0 
      } 
     } 
     ] 
    } 
    } 
}