# How set default values with fields in Aggregation results' empty buckets?

**URL:** <https://discuss.elastic.co/t/how-set-default-values-with-fields-in-aggregation-results-empty-buckets/164597>\
**Category:** Elasticsearch\
**Created:** [January 17, 2019, 9:14am UTC](https://discuss.elastic.co/t/how-set-default-values-with-fields-in-aggregation-results-empty-buckets/164597 "2019-01-17T09:14:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![teemoGod](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/teemogod/32/40817_2.png) [@teemoGod](https://discuss.elastic.co/u/teemoGod)\
**Post date:** [January 17, 2019, 9:14am UTC](https://discuss.elastic.co/t/how-set-default-values-with-fields-in-aggregation-results-empty-buckets/164597/1 "2019-01-17T09:14:17Z")

</div>

Hi. Im using Elasticseach 6.4

following is my request.

```json
              "aggs": {
                "times": {
                  "date_histogram": {
                    "field": "ccr.key",
                    "interval": "minute",  
                    "format": "yyyyMMdd:HHmm"
                  },
                  "aggs": {
                    "count" : {
                      "terms": {
                        "field": "ccr.count"
                      }
                    }
                  }
                },...
              }

```

and following is response

```json
"times": {
            "buckets": [
              {
                "key_as_string": "20190117:1100",
                "key": 1547722800000,
                "doc_count": 1,
                "count": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                    {
                      "key": 263,
                      "doc_count": 1
                    }
                  ]
                }
              },
              {
                "key_as_string": "20190117:1101",
                "key": 1547722860000,
                "doc_count": 0,
                "count": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [] // i want to get 0 by default. but there isn't
                }
              }, ....}

```

but what i want to make response is following

```json
"times": {
            "buckets": [
              {
                "key_as_string": "20190117:1100",
                "key": 1547722800000,
                "doc_count": 1,
                "count": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                    {
                      "key": 263,
                      "doc_count": 1
                    }
                  ]
                }
              },
              {
                "key_as_string": "20190117:1101",
                "key": 1547722860000,
                "doc_count": 0,
                "count": {
                  "doc_count_error_upper_bound": 0,
                  "sum_other_doc_count": 0,
                  "buckets": [
                      {
                      "key": 0, // if response bucket is empty, set field 'key' to 0 (by default)
                      "doc_count": 0 // i don't using this field.
                    }] 
                }
              }, ....}

```

can i set it?

---

<div class="post-metadata">

**Author:** ![teemoGod](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/teemogod/32/40817_2.png) [@teemoGod](https://discuss.elastic.co/u/teemoGod)\
**Post date:** [January 18, 2019, 2:47am UTC](https://discuss.elastic.co/t/how-set-default-values-with-fields-in-aggregation-results-empty-buckets/164597/2 "2019-01-18T02:47:36Z")

</div>

i solved.

the problem was 'terms' aggregation.

'terms' aggregation returning buckets.

in this case, 'ccr.count' is integer and there is only 1 docs. (it can be not exist)

so i changed config 'terms' to 'avg' like this.

```json
"aggs": {
                "times": {
                  "date_histogram": {
                    "field": "ccr.key",
                    "interval": "minute",  
                    "format": "yyyyMMdd:HHmm"
                  },
                  "aggs": {
                    "count" : {
                      "avg": { // here
                        "field": "ccr.count"
                      }
                    }
                  }
                },...
              }

```

then, result will not be buckets. it return 'value' property.

if there isn't exist, returned 'null' value instead of 'empty buckets' like this.

```json
"times": {
            "buckets": [
              {
                "key_as_string": "20190116:0000",
                "key": 1547596800000,
                "doc_count": 1,
                "count": {
                  "value": 156 // when exist
                }
              },{
                "key_as_string": "20190116:0001",
                "key": 1547596860000,
                "doc_count": 0,
                "count": {
                  "value": null // when doesn't exist
                }
              },...]
}

```

in kibana or vega graph, 'null' and '0' are same value in the graph's Y-Axis.

following is comparison of them.  
**caution : following graphs are showing difference values. not same.**

(when using 'terms' aggregation)

 ![terms](https://us1.discourse-cdn.com/elastic/original/3X/9/e/9e60f281005c87d71e6a118ee5a4e9c9692367f1.jpeg)

in 00:00 ~ 10:00 , i cant know values are '0' exactly.  
**it is confusing because look like 'increased steadily'.**

(when using 'avg' aggregation)

 ![avg](https://us1.discourse-cdn.com/elastic/original/3X/2/d/2d7c2f5f6932190d6621956c8e3b00f209a7d104.jpeg)  
in 00:00 ~ 10:00,  
**i know that there values are '0' exactly**

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [January 18, 2019, 4:32am UTC](https://discuss.elastic.co/t/how-set-default-values-with-fields-in-aggregation-results-empty-buckets/164597/3 "2019-01-18T04:32:46Z")

</div>

Thanks for sharing the solution! 😃

---

<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:** [February 15, 2019, 4:32am UTC](https://discuss.elastic.co/t/how-set-default-values-with-fields-in-aggregation-results-empty-buckets/164597/4 "2019-02-15T04:32:47Z")

</div>

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