2014-11-04 34 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 
      } 
     } 
     ] 
    } 
    } 
}