Aggregating even if array is empty


(Rafi) #1

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.


(Abdon Pijpelink) #2

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

(system) #3

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