Elasticsearch 'must not' with OR operator

wvt8vs2t  于 6个月前  发布在  ElasticSearch
关注(0)|答案(1)|浏览(56)

我正在寻找下面做一个Elasticsearch查询,
->按条件X过滤
->获取不满足条件A或条件B的记录
为了做到这一点,我最初在下面,然后意识到'must_not'在条件A和B之间做AND。

{
    "query": {
        "bool": {
            "filter": [
                {
                    "term": {
                        "district_id": {
                            "value": 123456
                        }
                    }
                }
            ],
            "must_not": [
                {
                    "match": {
                        "district_name": {
                            "query": "Mydist"
                        }
                    }
                },
                {
                    "term": {
                        "state_id": {
                            "value": 123
                        }
                    }
                }
            ]
        }
    }
}

字符串
建议的一些解决方案是相当混乱,我想知道是否有一个易于理解的解决方案.我在版本7.13.

5w9g7ksd

5w9g7ksd1#

您几乎已经完成了。您只需要将两个must_not子句嵌入到顶级bool/should中。您要查找的查询如下:

{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "district_id": {
              "value": 123456
            }
          }
        }
      ],
      "minimum_should_match": 1,
      "should": [
        {
          "bool": {
            "must_not": [
              {
                "match": {
                  "district_name": {
                    "query": "Mydist"
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must_not": [
              {
                "term": {
                  "state_id": {
                    "value": 123
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

字符串

相关问题