Need some help with aggregation for this particular case


(gary) #1

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);

(gary) #2

This looks a bit promising -

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.


(system) #3