# Aggregation numbers do not add up

**URL:** <https://discuss.elastic.co/t/aggregation-numbers-do-not-add-up/293825>\
**Category:** Elasticsearch\
**Created:** [January 9, 2022, 12:42pm UTC](https://discuss.elastic.co/t/aggregation-numbers-do-not-add-up/293825 "2022-01-09T12:42:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lark](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lark/32/100035_2.png) [@Lark](https://discuss.elastic.co/u/Lark)\
**Post date:** [January 9, 2022, 12:42pm UTC](https://discuss.elastic.co/t/aggregation-numbers-do-not-add-up/293825/1 "2022-01-09T12:42:01Z")

</div>

Hello friends!  
I'm trying to learn aggregations on Elastic for my company's backend.  
Our documents include a label field (which could be many different keywords), and I would like to aggregate and calculate certain things in small time steps.  
So, for exmaple:

```auto
GET /inferences/_search
{
  "size": 0,
  "aggs": {
    "histogram": {
      "date_histogram": {
        "field": "created_at",
        "fixed_interval": "5m"
      },
      "aggs": {
        "label_buckets": {
          "terms": {
            "field": "model_predictions.predictions.label.classification.value.keyword",
            "min_doc_count": 0
          }
        },
        "confidence": {
          "avg": {
            "field": "confidence"
          }
        }
      }
    }
  }
}

```

Currently in my demo environment I have docs with the labels "defect" and "yes". I would like the buckets to show all found labels, even if there are 0 results. What I actually get is:

```auto
  "aggregations" : {
    "histogram" : {
      "buckets" : [
        {
          "key_as_string" : "1970-01-19T23:45:00.000Z",
          "key" : 1640700000,
          "doc_count" : 2,
          "confidence" : {
            "value" : 0.2989208698272705
          },
          "label_buckets" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "defect",
                "doc_count" : 0
              }
            ]
          }
        },
        {
          "key_as_string" : "1970-01-19T23:50:00.000Z",
          "key" : 1641000000,
          "doc_count" : 8,
          "confidence" : {
            "value" : 0.6964685171842575
          },
          "label_buckets" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "defect",
                "doc_count" : 0
              }
            ]
          }
        },
        {
          "key_as_string" : "1970-01-19T23:55:00.000Z",
          "key" : 1641300000,
          "doc_count" : 6,
          "confidence" : {
            "value" : 1.0
          },
          "label_buckets" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "defect",
                "doc_count" : 0
              }
            ]
          }
        },
        {
          "key_as_string" : "1970-01-20T00:00:00.000Z",
          "key" : 1641600000,
          "doc_count" : 3,
          "confidence" : {
            "value" : 0.8757350246111552
          },
          "label_buckets" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : []
          }
        }
      ]
    }
  }

```

Two things stand out to me which I can't figure out:

1. Why do the labels not appear as buckets everywhere? The "defect" bucket only appears in 3/4 time buckets, and there are no "yes" buckets.
2. Why do the numbers not add up? Every time bucket has a positive `doc_count` but the actual label buckets only show 0.

Interestingly, the confidence aggregation works fine (I think, I haven't done the math myself yet, but given that its results are positive it must be calculating over something).

I'd love to know what I'm doing wrong here! Thanks!

---

<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 6, 2022, 12:42pm UTC](https://discuss.elastic.co/t/aggregation-numbers-do-not-add-up/293825/2 "2022-02-06T12:42:38Z")

</div>

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