弹性搜索 "A" 或 "empty"

ElasticSearch "A" or "empty"

我有如下索引index

{
  "docs": {
    "mappings": {
      "text": {
        "properties": {
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
          "code": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

“代码”可以有以下值:["a"]、["b"]、["a"、"b"] 或 []

我需要一个查询来使用以下代码解除值:“a”或 []。任何人都可以帮助解决这个查询,这是我到目前为止所尝试的 w/out 成功(这似乎是说“a”和“[]”;我想要“a”或“[]”)。

"bool": {
  "should": [
    {
      "match": {
        "code": "a"
      }
    }
  ],
  "must_not": [
    {
      "exists": {
        "field": "code"
      }
    }
  ]
}
  

OR 可以用 bool/should 实现,但是 bool/must 约束必须在 bool/should.

您应该尝试使用此查询:

{
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "match": {
            "code": "a"
          }
        },
        {
          "bool": {
            "must_not": [
              {
                "exists": {
                  "field": "code"
                }
              }
            ]
          }
        }
      ]
    }
  }
}