title: Issue in extracting the sum aggregation in ElasticSearch multi field group by using JavaAPI
body: <p>Using ElasticSearch 5.2 and a group by is being done similer to </p>
  
  <pre><code>select city,institutionId, SUM(appOpenCount) from XYZ where ( time &gt; 123 &amp;&amp; appOpenCount &gt; 0 ) group by city, institutionId.
  </code></pre>
  
  <p>I have it working when i do using curl method, but when the same is being converted to java api i am missing something that is causing me not get the last part of sum aggregation.</p>
  
  <p>I have a <i>type</i> <b>temp_type</b> with mapping given below.</p>
  
  <pre><code>{
  "temp_index" : {
  "mappings" : {
    "temp_type" : {
      "properties" : {
        "appOpenCount" : {
          "type" : "integer"
        },
        "city" : {
          "type" : "keyword"
        }
        "institutionId" : {
          "type" : "keyword"
        },
        "time" : {
          "type" : "long"
        }
      }
    }
  }
  }
  }
  </code></pre>
  
  <p>and my aggregation XGET call looks like this.</p>
  
  <pre><code>curl -XGET "http://localhost:9200/temp_index/temp_type/_search?pretty" -d'
  {
  "size":0,
  "_source":false,
  "from" : 0,
  "query": {
       "bool": {
          "must": [
           {"range": { "time": { "gte": 1513744603000 } } },
           { "range": { "appOpenCount": { "gt": 0 } } }
           ]
        }
  },
  "aggregations": {
      "city-aggs": {
          "terms": { "field": "city"},
          "aggregations": {
               "intitution-agg": {
                   "terms": { "field": "institutionId" },
                   "aggregations": {
                          "appOpenCount": { "sum": { "field": "appOpenCount" }}}
                }
              }
         }
      }
  }'
  </code></pre>
  
  <p>The response is perfect ( the aggregated number mathematically makes sense )</p>
  
  <pre><code>{
   "took" : 57,
   "timed_out" : false,
   "_shards" : { ... },
   "hits" : {...  },
   "aggregations" : {
     "city-aggs" : {
       "doc_count_error_upper_bound" : 0,
       "sum_other_doc_count" : 0,
       "buckets" : [
         {
          "key" : "city-1",
          "doc_count" : 25,
          "intitution-agg" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
               "key" : "inst-1",
               "doc_count" : 5,
               "appOpenCount" : {
                "value" : 15.0
               }
              }
            ]
          }
        }
      ]
    }
  }
  </code></pre>
  
  <p>Using this as template i converted this to Java API call and it i am able to execute it and access city-agg key and institution-agg key but am not sure how to access the appOpenCount agg. Basically getting null for Sum aggregation.</p>
  
  <pre><code>// bool query
  BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
  List&lt;QueryBuilder&gt; mustQueries = boolQueryBuilder.must();
  mustQueries.add(QueryBuilders.rangeQuery("time").gte(startTime));
  mustQueries.add(QueryBuilders.rangeQuery("appOpenCount").gt(0));
  queryBuilder = boolQueryBuilder;
  
  // aggregationbuilder
  AggregationBuilder aggregationBuilder = null;
  TermsAggregationBuilder cityAggs = AggregationBuilders.terms("city-aggs").field("city");
  TermsAggregationBuilder institutionAggs = AggregationBuilders.terms(
                  "institution-agg").field("institutionId");
  SumAggregationBuilder fieldAggBuilder = AggregationBuilders.sum("appOpenCount").field("appOpenCount");
  aggregationBuilder = cityAggs.subAggregation(institutionAggs).subAggregation(fieldAggBuilder);
  
  // search call
  SearchResponse searchResponse = client.prepareSearch(indexName)
                  .setTypes(typeName)
                  .setQuery(queryBuilder)
                  .addAggregation(aggregationBuilder)
                  .setFrom(0)
                  .setSize(0)
                  .execute().actionGet();
  
  // Iterate the searchResponse
  Terms cityAggsTerms = searchResponse.getAggregations().get("city-aggs");
  List&lt;Terms.Bucket&gt; mainCityBuckets = cityAggsTerms.getBuckets();
  for (Terms.Bucket mainCityBucket : mainCityBuckets) {
      String cityName = mainCityBucket.getKeyAsString();
      LOGGER.info("CityName : " + cityName); // all good
      Terms institutionTerms = mainCityBucket.getAggregations().get("institution-agg");
      List&lt;Terms.Bucket&gt; institutionBuckets = institutionTerms.getBuckets();
      for (Terms.Bucket institutionBucket : institutionBuckets) {
          String institutionName = institutionBucket.getKeyAsString();
          LOGGER.info("InstitutionName : " + institutionName ); // all good
          Sum appOpenCountSum = institutionBucket.getAggregations().get("appOpenCount");
          if(appOpenCountSum != null) {
            double appOpenCount = appOpenCountSum.getValue();
            LOGGER.info("InstitutionName : " + institutionName +
                                  " and appOpenCount is " + appOpenCount);
          } else {
            LOGGER.info("appOpenCountSum is null");
          }
       } // institution for
    }// city for
  </code></pre>
  
  <p>How can i access the value of appOpenCount aggregation. I am hitting the case where my "appOpenCountSum" variable is null. Any help would be appreciated. I am able to access the <i> city-agg </i> and <i> institution-agg </i> and get proper values too. Not sure how to access the appOpenCount aggregation inside Term.Bucket</p>
  
  <p>I followed the example provided in elastic search docs for this </p>
  
  <p><a href="https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/_metrics_aggregations.html#java-aggs-metrics-sum" rel="nofollow noreferrer">https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/_metrics_aggregations.html#java-aggs-metrics-sum</a></p>
  
  <p>Have given in-depth breakdown and hopefully it helps others too.</p>
  
  <p><b>EDIT</b> : Issue was the way i was building the aggregation query in java. The fieldAggBuilder should be added to institutionAggs and not the way i had done previously. The corrected code below.</p>
  
  <pre><code>// aggregationbuilder
  AggregationBuilder aggregationBuilder = null;
  TermsAggregationBuilder cityAggs = AggregationBuilders.terms("cityaggs").field("city");
  TermsAggregationBuilder institutionAggs = AggregationBuilders.terms(
              "institution-agg").field("institutionId");
  SumAggregationBuilder fieldAggBuilder = 
  AggregationBuilders.sum("appOpenCount").field("appOpenCount");
  institutionAggs.subAggregation(fieldAggBuilder); // this was missing previously
  aggregationBuilder = cityAggs.subAggregation(institutionAggs);
  </code></pre>