简体   繁体   中英

Elasticsearch nested aggregation with JAVA

How to create the following nested agg query with ES JAVA API

Let's say my query is looking like the following :

 GET /agg_vitaly_test_api_2016-11-01/_search
    {
     "size": 0,
     "query": {
          "range": {
            "time": {
              "gte": "1477962000000",
              "lte": "1477965600000"
            }
          }
     },
      "aggs" : {
           "group_by_time" : {
               "date_histogram" : {
                   "field" : "time",
                   "interval" : "hour"
               },

               "aggs": {
                 "sum_player_load": {
                   "sum": {
                     "field": "playerload"
                   }
                 }
           }
       }
    }

the java code should be somethink like this :

SearchRequestBuilder searchRequestBuilder = FETCH_CLIENT.prepareSearch().setIndices(indexName).setTypes(pixelType.getType()).setSize(0);

           // here need to complete the nested aggregation....
            AggregationBuilder aggb = addAggregation(groupBy);

         searchRequestBuilder.addAggregation(aggb);

  // ....
 SearchResponse res = searchRequestBuilder.execute().actionGet();

please any suggestions ? :)

BR

Your addAggregation method would be like:

public AggregationBuilder addAggregation(){
        return AggregationBuilders.dateHistogram("group_by_time").field("time").interval(DateHistogram.Interval.HOUR)
                                                                        .subAggregation(AggregationBuilders.sum("sum_player_load").field("playerload"));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM