# Need some help with aggregation for this particular case

**URL:** <https://discuss.elastic.co/t/need-some-help-with-aggregation-for-this-particular-case/44328>\
**Category:** Elasticsearch\
**Created:** [March 14, 2016, 12:56pm UTC](https://discuss.elastic.co/t/need-some-help-with-aggregation-for-this-particular-case/44328 "2016-03-14T12:56:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![garyg](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@garyg](https://discuss.elastic.co/u/garyg)\
**Post date:** [March 14, 2016, 12:56pm UTC](https://discuss.elastic.co/t/need-some-help-with-aggregation-for-this-particular-case/44328/1 "2016-03-14T12:56:17Z")

</div>

After adding a few aggregations and filters to take into account the business logic, I am now getting the following structure back from elastic search. The only addition I want to make is to get sum of all the blog\_score elements in the buckets collection. Is it possible to write an aggregation that will do that. I am also pasting the java code that leads me to the following result -

```
{
        "key" : "hour",
        "doc_count" : 14,
        "unique_blogs" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 0,
          "buckets" : [ {
            "key" : 2766875,
            "doc_count" : 2,
            "blog_score" : {
              "value" : 4.0
            }
          }, {
            "key" : 2766909,
            "doc_count" : 2,
            "blog_score" : {
              "value" : 4.0
            }
          }, {
            "key" : 2766857,
            "doc_count" : 1,
            "blog_score" : {
              "value" : 3.0
            }
          }, {
            "key" : 2766882,
            "doc_count" : 1,
            "blog_score" : {
              "value" : 5.0
            }
          }, {
            "key" : 2766883,
            "doc_count" : 1,
            "blog_score" : {
              "value" : 5.0
            }
          }, {
            "key" : 2766889,
            "doc_count" : 1,
            "blog_score" : {
              "value" : 5.0
            }
          }, {
            "key" : 2766917,
            "doc_count" : 1,
            "blog_score" : {
              "value" : 3.0
            }
          }, {
            "key" : 2766926,
            "doc_count" : 1,
            "blog_score" : {
              "value" : 3.0
            }
          }, {
            "key" : 2766932,
            "doc_count" : 1,
            "blog_score" : {
              "value" : 5.0
            }
          }, {
            "key" : 2766940,
            "doc_count" : 1,
            "blog_score" : {
              "value" : 5.0
            }
          }, {
            "key" : 2766941,
            "doc_count" : 1,
            "blog_score" : {
              "value" : 5.0
            }
          }, {
            "key" : 2766961,
            "doc_count" : 1,
            "blog_score" : {
              "value" : 5.0
            }
          } ]
        }

```

The Java code that is producing this result,

```
TermsBuilder aggregation = AggregationBuilders.terms(preFilterAggregationName).field("keywordValue").order(Terms.Order.count(sortAscending)).
subAggregation(AggregationBuilders.terms("unique_blogs").field("blogId").
subAggregation(AggregationBuilders.avg("blog_score").field("keywordScore")).size(0)).size(0).
subAggregation(AggregationBuilders.filter(keywordCountAggregationName).
filter(QueryBuilders.termQuery("sentiment", sentimentLabel))).size(0);
```

---

<div class="post-metadata">

**Author:** ![garyg](https://avatars.discourse-cdn.com/v4/letter/g/f6c823/32.png) [@garyg](https://discuss.elastic.co/u/garyg)\
**Post date:** [March 14, 2016, 3:13pm UTC](https://discuss.elastic.co/t/need-some-help-with-aggregation-for-this-particular-case/44328/2 "2016-03-14T15:13:18Z")

</div>

This looks a bit promising -

[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-sum-bucket-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-sum-bucket-aggregation.html)

But I am not sure how to use it from Java code.

---

<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:** [July 5, 2017, 11:08pm UTC](https://discuss.elastic.co/t/need-some-help-with-aggregation-for-this-particular-case/44328/3 "2017-07-05T23:08:38Z")

</div>


