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

ElasticSearch search for for empty fields

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

I have added a new nested object field to a elastic search index. I want to update the field with some default value like [ {"key": "xyz", "value": "val1"}]. I am trying to query the index where the filed value is empty or null, but was not successful.

I have tried this

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

sample data

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

i want to query ids 2,3

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

If you want to find documents that are missing a nested object field, you can use the must_not boolean query with the exists query.

Adding a working example with index data, mapping, search query, and search result

Index Mapping:

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

Index Data:

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

Search Query:

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

Search Result:

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