class ElasticsearchHelper {

    protected Aggregations getAggregationsFromSearchResponse(SearchResponse searchResponse)
    {
        // Retrieve the aggregations.
        Aggregations aggregations = searchResponse.getAggregations();

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

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

        return aggregations;
    }

}