Sort with aggregation with another aggregation in Java REST API

I created the query below and I want to execute it with the Java Client provided but I can't really find a way.

GET asset-records-latest/_search
{
  "size": 0,
  "aggs": {
    "agg1": {
      "terms": {
        "field": "asset_id",
        "order": {
          "agg2": "desc"
        },
        "size": 10
      },
      "aggs": {
        "agg2": {
          "max": {
            "field": "risk_index_value"
          }
        }
      }
    }
  }
}

Welcome!

What about:

SearchResponse response = client.search(new SearchRequest("asset-records-latest").source(new SearchSourceBuilder()
        .size(0)
        .aggregation(AggregationBuilders.terms("agg1")
                .field("asset_id")
                .order(BucketOrder.aggregation("agg2", false))
                .subAggregation(AggregationBuilders.max("agg2").field("risk_index_value"))
        )
), RequestOptions.DEFAULT);

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