Elasticsearch provides a full Java API to play with aggregations. See the Aggregations guide.

Use the factory for aggregation builders (AggregationBuilders) and add each aggregation you want to compute when querying and add it to your search request:

SearchResponse sr = node.client().prepareSearch()
        .setQuery( /* your query */ )
        .addAggregation( /* add an aggregation */ )

Note that you can add more than one aggregation. See Search Java API for details.

To build aggregation requests, use AggregationBuilders helpers. Just import them in your class: