在elasticsearch中组合两个bool查询

时间:2018-04-13 10:01:03

标签: elasticsearch

我想创建以下查询的等价物:

(country.id = 2000 AND natureservices.id = 2000 AND metiers.id = 1000) AND (shortname = "shortname" OR fullname = "fullname" OR categories.id = 1000 OR specialities.id = 2000)

在我的情况下,他必须向我显示单个结果,但当我尝试以下查询时,它会显示许多结果

{ "query": {
   "bool": {
        "must": [
            { "term": { "country.id": 2000 } },
            { "term": { "natureservices.id": 2000 } },
            { "term": { "metiers.id": 1000 } },
            {
                "bool": {
                    "should": [
                        { "term": { "shortname": "vgftQzSPwW" } },
                        { "term": { "fullname": "qcouWRzFNG" } },
                        { "term": { "categories.id": 1000 } },
                        { "term": { "specialities.id": 2000 } }
                    ], "minimum_should_match": 4
                }
            }
        ], "minimum_should_match": 3
    }
 } } 

1 个答案:

答案 0 :(得分:0)

请阅读here。我从字面上理解了您的查询,但我无法理解您为何以这种方式使用minimum_should_match子句。当你有多个子句而不是你在那里指定的数字时,通常会使用minimum_should_match。例如,如果您有4个条款子句,但由于某种原因,您只需要其中三个条款就可以完成,您可以将3分配为minimum_should_match的值。据我所知,使用minimum_should_match可能会产生问题,因此我已从您的查询中删除了minimum_should_match。您的查询的正确语法是:

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "country.id": 2000
                }
              },
              {
                "term": {
                  "natureservices.id": 2000
                }
              },
              {
                "term": {
                  "metiers.id": 1000
                }
              }
            ]
          }
        },
        {
          "bool": {
            "should": [
              {
                "term": {
                  "shortname": "vgftQzSPwW"
                }
              },
              {
                "term": {
                  "fullname": "qcouWRzFNG"
                }
              },
              {
                "term": {
                  "categories.id": 1000
                }
              },
              {
                "term": {
                  "specialities.id": 2000
                }
              }
            ]
          }
        }
      ]
    }
  }
} 
相关问题