Combine must and should

淺唱寂寞╮ 提交于 2020-01-05 08:13:11

问题


I've some troubles with an elasticsearch query. (I use elasticsearch 5). I want to combine must bool query and should in order to create a query which match this condition :

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

I tried many queries but I still have a query malformed exception.

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

I hope someone could help me :D

Thanks for your help


回答1:


This works for me:

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


来源:https://stackoverflow.com/questions/48339694/combine-must-and-should

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!