How to add additional value in bucket aggregation?

Hi, I have documents in the index with the field sentimental_keywords:

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

When I do terms aggregations :

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

I get the result:

'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:

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

Do you have any suggestions?

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?

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.

I solved it by using sub aggregate terms for rate value

"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:

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

1 Like

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

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