结合必须而且应该

时间:2018-01-19 11:05:37

标签: elasticsearch elasticsearch-5

我对弹性搜索查询有些麻烦。 (我使用elasticsearch 5)。我想结合必须bool查询,并且应该为了创建符合这个条件的查询:

Get users which match (city = x) AND (school = y) AND (age = 12) AND (team = a OR b)

我尝试了很多查询,但我仍然遇到查询格式错误。

{
  "query": {
    "bool": {
      "must" : [
        {
          "match": {
            "city": "x"
          }
        },
        {
          "match" : {
            "school" : "y"
        }
        },
        {
          "match" : {
            "age" : 12
        },
          "bool": {
            "should": [
              {"term": {"team": "A"}},
              {"term": {"team": "B"}}
            ]
          }
        }
      ]
    }
  }
}

我希望有人可以帮助我:D

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

这对我有用:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "city": "x"
          }
        },
        {
          "match": {
            "school": "y"
          }
        },
        {
          "match": {
            "age": 12
          }
        },
        {
          "bool": {
            "should": [
              {
                "term": {
                  "team": "A"
                }
              },
              {
                "term": {
                  "team": "B"
                }
              }
            ]
          }
        }
      ]
    }
  }
}