How to use the ElasticSearch Java API to build an Aggregation from JSON

Updating this old thread as @monde shared with me a solution he found on SOF:

 val aggsRequestBuilder = new SearchRequestBuilder(client)
  .setIndices(index())
  .setQuery(QueryBuilders.wrapperQuery(query.toString())
  .setAggregations(agg.toString().getBytes())