Warm tip: This article is reproduced from serverfault.com, please click

其他-ElasticSearch搜索空字段

(其他 - ElasticSearch search for for empty fields)

发布于 2020-11-28 07:41:05

我在弹性搜索索引中添加了一个新的嵌套对象字段。我想用一些默认值更新字段,例如[{“ key”:“ xyz”,“ value”:“ val1”}]。我正在尝试查询字段值为空或null的索引,但未成功。

我已经试过了

          "bool": {
            "must_not": {
              "exists": {
                "field": "PropertyFields"
              }
            }
          }

样本数据

[{“ Id”:1,“ subjectPropertyFields”:[{“ value”:“ xyz”,“ key”:“ zzz”}]},{“ Id”:2},{“ Id”:3}]

我想查询ID 2,3

Questioner
Ajay Kumar
Viewed
11
ESCoder 2020-11-28 16:21:30

如果要查找缺少嵌套对象字段的文档,则可以将must_not布尔查询与存在查询结合使用

添加带有索引数据,映射,搜索查询和搜索结果的工作示例

索引映射:

  {
  "mappings": {
    "properties": {
      "subjectPropertyFields": {
        "type": "nested"
      }
    }
  }
}

索引数据:

{
    "Id": 1,
    "subjectPropertyFields": [
      {
        "value": "xyz",
        "key": "zzz"
      }
    ]
  }
  {
    "Id": 2
  }
  {
    "Id": 3
  }

搜索查询:

 {
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "subjectPropertyFields",
            "query": {
              "bool": {
                "filter": {
                  "exists": {
                    "field": "subjectPropertyFields"
                  }
                }
              }
            }
          }
        }
      ]
    }
  }
}

搜索结果:

"hits": [
      {
        "_index": "65047473",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.0,
        "_source": {
          "Id": 2
        }
      },
      {
        "_index": "65047473",
        "_type": "_doc",
        "_id": "3",
        "_score": 0.0,
        "_source": {
          "Id": 3
        }
      }
    ]