# Nested terms aggregations - min\_doc\_count isn't returning key-wise(field in terms) 0 for empty buckets

**URL:** <https://discuss.elastic.co/t/nested-terms-aggregations-min-doc-count-isnt-returning-key-wise-field-in-terms-0-for-empty-buckets/194027>\
**Category:** Elasticsearch\
**Created:** [August 6, 2019, 2:13pm UTC](https://discuss.elastic.co/t/nested-terms-aggregations-min-doc-count-isnt-returning-key-wise-field-in-terms-0-for-empty-buckets/194027 "2019-08-06T14:13:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![saurabh-bhosale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/saurabh-bhosale/32/51758_2.png) [@saurabh-bhosale](https://discuss.elastic.co/u/saurabh-bhosale)\
**Post date:** [August 6, 2019, 2:13pm UTC](https://discuss.elastic.co/t/nested-terms-aggregations-min-doc-count-isnt-returning-key-wise-field-in-terms-0-for-empty-buckets/194027/1 "2019-08-06T14:13:09Z")

</div>

{

"size": 0,

"aggs" : {

```
"level_one_aggregation": {

      "date_range": {

          "field": "index_field_one",

          "format": "yyyy-MM-dd",

          "ranges": [{

              "from": "2019-01-01",

              "to": "2020-03-01", 

              "key": "custom-range"

          }],

          "keyed": "true"

      },

  "aggs": {

   "level_two_aggregation": {

        "date_histogram": {

            "field": "index_field_one",

            "interval": "month",

            "min_doc_count": "0",

            "extended_bounds": {

                "min": "2020-01-01",

                "max": "2020-03-01"

            },

            "keyed": "true",

            "order": { "_key": "asc" }

        },

        "aggs": {

            "level_three_aggregation": {

                "terms": {

                    "field": "index_field_two",

                    "include": [1,2,3,4,5],

                    "min_doc_count": "0",

                    "order": { "_key": "asc"}

                },

                "aggs": {

                    "level_three_first_child": {

                        "terms": {

                            "field": "field1",

                            "min_doc_count": "0"

                        }

                    },

                    'sum_of_level_three_first_child': {

                        'sum_bucket': {

                            'buckets_path': 'level_three_first_child&gt;_count'

                        }

                    },

                    'level_three_seond_child': {

                        'terms': {

                            'field': 'field2',

                            'min_doc_count': 0

                        }

                    },

                    'sum_of_level_three_second_child': {

                        'sum_bucket': {

                            'buckets_path': 'level_three_seond_child&gt;_count'

                        }

                    }

                }

            }

        }

    }

  }

}  

```

}

}

In single level terms aggregation the min\_doc\_count gives a zero count corresponding to each field in "index\_field\_two" but in the above snippet the min\_doc\_count gives an empty bucket but fails to give zero count corresponding to each field in index\_field\_two of level\_three\_aggregation for level\_three\_first\_child and level\_three\_second\_child. Have tried using missing, include paired with min\_doc\_count, changing its size(isn't relevant though). How can I get this working??

---

<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 3, 2019, 2:13pm UTC](https://discuss.elastic.co/t/nested-terms-aggregations-min-doc-count-isnt-returning-key-wise-field-in-terms-0-for-empty-buckets/194027/2 "2019-09-03T14:13:10Z")

</div>

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