Elastic search query to check if property is empty/non-empty

Hello,

I was trying to find out all my documents which has property with empty value or non-empty value. The type of this property is "keyword". I'm using ES 6.8.1 version.

I couldn't find anything in the documentation to query for this scenario. Can someone help ?

After trying the must and filter properties, I was able to query the documents that has null/nullable values.

Posting the queries so anyone can find them useful

Query to fetch all the documents that has the property in the document

GET index_name/_count
{
  "query": {
    "bool": {
      "must": {
        "exists": { "field": "your document property name"}
        }
    }
  }
}

Query to fetch all the documents that have the property AND the property contains empty value

GET index_name/_count
{
  "query": {
    "bool": {
      "must": {
        "exists": { "field": "your document property name"}
        },
        "filter": {
          "term": {"your document property name": ""}
      }
    }
  }
}

Query to fetch all the documents that have the property AND the property contains NON-Empty value

GET index_name/_count
{
  "query": {
    "bool": {
      "must": {
        "exists": { "field": "your document property name"}
        },
        "must_not": {
          "term": {"your document property name": ""}
      }
    }
  }
}

Thank you !

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.