class ElasticsearchHelper {

    protected Aggregations getAggregationsFromNestedAggregation(Nested nestedAggregation, SearchResponse searchResponse)
    {
        // Retrieve the aggregations.
        Aggregations aggregations = nestedAggregation.getAggregations();

        // Fail if the retrieved object is null.
        if (aggregations == null)
        {
            // Log the error along with the nested aggregation contents.
            LOGGER.error("Failed to retrieve aggregations from the nested aggregation. searchResponse={} nestedAggregation={}",
                jsonHelper.objectToJson(searchResponse), jsonHelper.objectToJson(nestedAggregation));

            // Throw an exception.
            throw new IllegalStateException("Invalid search result.");
        }

        return aggregations;
    }

}