【问题标题】:Filter on field value or not exists过滤字段值或不存在
【发布时间】:2021-04-07 19:51:26
【问题描述】:

我正在尝试过滤特定值或它不存在的字段。

我有特定值的部分

{
  "query": {
    "match": {
      "app.serviceType": {
        "query": "MY_VALUE",
        "type": "phrase"
      }
    }
  }
}

我还想在字段 serviceType 根本不存在的情况下添加此内容。

基本上我想要这样的:

serviceType == "MY_VALUE" || string.IsNullOrEmpty(serviceType)

【问题讨论】:

标签: elasticsearch


【解决方案1】:

此请求必须与您的用例匹配:

 {
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "exists": {
                  "field": "serviceType"
                }
              },
              {
                "match_phrase": {
                    "serviceType": "MY_VALUE"
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must_not": [
              {
                "exists": {
                  "field": "serviceType"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

【讨论】:

  • 这没有用,但它确实让我很接近。我不得不将查询开头的第一个must 更改为should
【解决方案2】:

根据之前的答案(没有用但让我很接近),我能够让它工作。

{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "exists": {
                  "field": "app.serviceType"
                }
              },
              {
                "match_phrase": {
                  "app.serviceType": "MY_VALUE"
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must_not": [
              {
                "exists": {
                  "field": "app.serviceType"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-14
    • 2019-03-06
    • 2017-06-04
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多