结合必须和应该 [英] Combine must and should

查看:81
本文介绍了结合必须和应该的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Elasticsearch查询时遇到了一些麻烦。 (我使用elasticsearch 5)。我想结合必须布尔查询,并且应该为了创建符合此条件的查询:

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

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

I hope someone could help me :D

感谢您的帮助

推荐答案

这对我有用:

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

这篇关于结合必须和应该的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆