将多个应该和必须查询组合为子查询

时间:2014-11-04 02:01:18

标签: elasticsearch

是否可以合并两个不同的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。

1 个答案:

答案 0 :(得分:4)

很长一段时间,但它应该先将葡萄酒命名为法国白葡萄酒和加拿大红葡萄酒,并以相同的分数,其他法国和加拿大第二,并且得分相同,意大利最后得分相同:

{
  "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
          }
        }
      ]
    }
  }
}