# How to add additional value in bucket aggregation?

**URL:** <https://discuss.elastic.co/t/how-to-add-additional-value-in-bucket-aggregation/269666>\
**Category:** Elasticsearch\
**Created:** [April 9, 2021, 6:49am UTC](https://discuss.elastic.co/t/how-to-add-additional-value-in-bucket-aggregation/269666 "2021-04-09T06:49:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zeusfsx](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zeusfsx/32/59433_2.png) [@zeusfsx](https://discuss.elastic.co/u/zeusfsx)\
**Post date:** [April 9, 2021, 6:49am UTC](https://discuss.elastic.co/t/how-to-add-additional-value-in-bucket-aggregation/269666/1 "2021-04-09T06:49:40Z")

</div>

Hi, I have documents in the index with the field sentimental\_keywords:

```auto
sentimental_keywords: [
{
    rate: 0.2857143,
    keyword: "manager"
},
...
{
    rate: -0.333333343,
    keyword: "project"
},
]

```

When I do terms aggregations :

```auto
"aggs": {
     sentimental_keywords": {
            "terms": {
                "field" : "sentimental_keywords.keyword.keyword",
                "size": 100
            }
}

```

I get the result:

```auto
'buckets': [
    {'key': 'manager', 'doc_count': 444},
    {'key': 'project', 'doc_count': 373}
]

```

I also want to add each term (key), the value of the rate, for example, get next result:

```auto
'buckets': [
    {'key': 'manager', 'doc_count': 444, 'rate': { 'value': 0.2857143}},
    {'key': 'project', 'doc_count': 373, 'rate': { 'value': -0.333333343}}
]

```

Do you have any suggestions?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 9, 2021, 7:07am UTC](https://discuss.elastic.co/t/how-to-add-additional-value-in-bucket-aggregation/269666/2 "2021-04-09T07:07:40Z")

</div>

I'm not sure if that would fit with what you're looking for as aggregations are meant to aggregate documents and not providing individual values.

So what about a `avg` or a `max` agg as a sub aggregation of your `terms` agg?

---

<div class="post-metadata">

**Author:** ![zeusfsx](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zeusfsx/32/59433_2.png) [@zeusfsx](https://discuss.elastic.co/u/zeusfsx)\
**Post date:** [April 9, 2021, 7:14am UTC](https://discuss.elastic.co/t/how-to-add-additional-value-in-bucket-aggregation/269666/3 "2021-04-09T07:14:32Z")

</div>

I've tried to use sub aggregation for a rate like `max` or `avg` , it returns the average or max rate for a document where term from top-level `aggs` meets.

---

<div class="post-metadata">

**Author:** ![zeusfsx](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zeusfsx/32/59433_2.png) [@zeusfsx](https://discuss.elastic.co/u/zeusfsx)\
**Post date:** [April 9, 2021, 8:07am UTC](https://discuss.elastic.co/t/how-to-add-additional-value-in-bucket-aggregation/269666/4 "2021-04-09T08:07:33Z")

</div>

I solved it by using sub aggregate terms for rate value

```auto
"aggs": {
    "sentimental_keywords": {
            "terms": {
                "field" : "sentimental_keywords.keyword.keyword",
                "size": 10
            },
            "aggs": {
                "rate": {
                    "terms": {
                        "field": "calculation.sentimental_keywords.rate",
                        "size": 1
                    }
                }
            }
            
        }
    }

```

In the result I get what I wanted:

```auto
{
     'key': 'project',
     'doc_count': 371,
     'rate': {
           'doc_count_error_upper_bound': 66,
           'sum_other_doc_count': 1789,
           'buckets': [{'key': -0.333333343, 'doc_count': 371}]
     }
}

```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 9, 2021, 8:56am UTC](https://discuss.elastic.co/t/how-to-add-additional-value-in-bucket-aggregation/269666/5 "2021-04-09T08:56:29Z")

</div>

I'm surprised that this will work with more data.

---

<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:** [May 7, 2021, 8:56am UTC](https://discuss.elastic.co/t/how-to-add-additional-value-in-bucket-aggregation/269666/6 "2021-05-07T08:56:40Z")

</div>

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