Aggregating even if array is empty

I am trying to do simple aggregation in elastic search using an array my query is as follows

{
     "size":0,
     "aggs":{
          "byTags":{
            "terms": {
              "field": "tags"
              }
      }
    }
}

this works fine but there is possibility of tags array being empty. If it is empty I want all the documents with empty array value for tags grouped together under the group "unclassified" how do I proceed thanks in advance.

You could add a missing aggregation to your request. This will count the documents that have no values for tags (or are missing the tags field completely):

{
  "size": 0,
  "aggs": {
    "byTags": {
      "terms": {
        "field": "tags"
      }
    },
    "unclassified": {
      "missing": {
        "field": "tags"
      }
    }
  }
}
1 Like

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