# Aggregating even if array is empty

**URL:** <https://discuss.elastic.co/t/aggregating-even-if-array-is-empty/96014>\
**Category:** Elasticsearch\
**Created:** [August 6, 2017, 10:54am UTC](https://discuss.elastic.co/t/aggregating-even-if-array-is-empty/96014 "2017-08-06T10:54:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafi993](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rafi993/32/23892_2.png) [@Rafi993](https://discuss.elastic.co/u/Rafi993)\
**Post date:** [August 6, 2017, 10:54am UTC](https://discuss.elastic.co/t/aggregating-even-if-array-is-empty/96014/1 "2017-08-06T10:54:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [August 15, 2017, 3:32pm UTC](https://discuss.elastic.co/t/aggregating-even-if-array-is-empty/96014/2 "2017-08-15T15:32:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 12, 2017, 3:32pm UTC](https://discuss.elastic.co/t/aggregating-even-if-array-is-empty/96014/3 "2017-09-12T15:32:57Z")

</div>

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