# How variable width histogram with nested aggregations works

**URL:** <https://discuss.elastic.co/t/how-variable-width-histogram-with-nested-aggregations-works/328219>\
**Category:** Elasticsearch\
**Created:** [March 22, 2023, 8:35am UTC](https://discuss.elastic.co/t/how-variable-width-histogram-with-nested-aggregations-works/328219 "2023-03-22T08:35:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rym](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rym/32/118698_2.png) [@rym](https://discuss.elastic.co/u/rym)\
**Post date:** [March 22, 2023, 8:35am UTC](https://discuss.elastic.co/t/how-variable-width-histogram-with-nested-aggregations-works/328219/1 "2023-03-22T08:35:52Z")

</div>

Hi,  
I want to use the `variable_width_histogram` combined with other aggregations, such as `min` or `max`

Here is an example of combinated aggregations on a numeric field:

```auto
"aggs": {
        "aggregated-items": {
            "variable_width_histogram": {
                "buckets": 5,
                "field": "freespace"
            },
            "aggs": {
                "max-aggs": {
                    "max": {
                        "field": "freespace"
                    }
                }
            }
        }
    }

```

Here is the result I got:

```auto
 "aggregations": {
        "aggregated-items": {
          "buckets": [
                {
                    "min": 100.0,
                    "key": 111.67054263565892,
                    "max": 124.0,
                    "doc_count": 258,
                    "max-aggs": {
                        "value": 200.0
                    }
                },
                {
                    "min": 124.0,
                    "key": 134.45454545454547,
                    "max": 145.0,
                    "doc_count": 198,
                    "max-aggs": {
                        "value": 200.0
                    }
                },
                {
                    "min": 145.0,
                    "key": 157.73333333333332,
                    "max": 171.0,
                    "doc_count": 270,
                    "max-aggs": {
                        "value": 200.0
                    }
                },
                {
                    "min": 172.0,
                    "key": 180.82323232323233,
                    "max": 191.0,
                    "doc_count": 198,
                    "max-aggs": {
                        "value": 200.0
                    }
                },
                {
                    "min": 192.0,
                    "key": 196.11764705882354,
                    "max": 200.0,
                    "doc_count": 85,
                    "max-aggs": {
                        "value": 198.0
                    }
                }
            ]
        }
    }

```

I don't understant why the `max-aggs` value is **200.0** in first buckets and why it's **198.0** in the last bucket. I would expect the `max-aggs` to be the same as the `max` of the corresponding bucket, ie **124.0** in the first, etc.

So, I would like to know how this `max-aggs` value is calulcated when used under a `variable_width_histogram` ?

Thanks !

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [March 22, 2023, 12:54pm UTC](https://discuss.elastic.co/t/how-variable-width-histogram-with-nested-aggregations-works/328219/2 "2023-03-22T12:54:10Z")

</div>

Hi,

Does "freespace" field have multiple values in array?  
As bucket aggregation works for bucketing `documents`, each `variable_width_histogram` bucket may contain documents with a value of "freespace" between the range. If the documents have other values for "freespace" field, the sub-aggregation uses all the values for aggregation.

If you have to aggregate such values separately, storing them in individual nested objects may be an solution.

---

<div class="post-metadata">

**Author:** ![rym](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rym/32/118698_2.png) [@rym](https://discuss.elastic.co/u/rym)\
**Post date:** [March 24, 2023, 1:24pm UTC](https://discuss.elastic.co/t/how-variable-width-histogram-with-nested-aggregations-works/328219/3 "2023-03-24T13:24:56Z")

</div>

Hi,

Thanks for your answer, now I understand better how it works when aggregating arrays. Infortunately, the "freespace" fields is not an array.

However, it works perfectly when I use an `histogram`. So I don't know why I don't have same max with `variable_width_histogram`.

---

<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:** [April 21, 2023, 1:25pm UTC](https://discuss.elastic.co/t/how-variable-width-histogram-with-nested-aggregations-works/328219/4 "2023-04-21T13:25:31Z")

</div>

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