Elasticsearch Aggregation

An aggregation summarizes your data as metrics, statistics, or other analytics. Aggregations help you answer questions like:

  • What’s the average load time for my website?
  • Who are my most valuable customers based on transaction volume?
  • What would be considered a large file on my network?
  • How many products are in each product category?

Elasticsearch organizes aggregations into three categories:

  • Metric aggregations that calculate metrics, such as a sum or average, from field values.
  • Bucket aggregations that group documents into buckets, also called bins, based on field values, ranges, or other criteria.
  • Pipeline aggregations that take input from other aggregations instead of documents or fields.

Use the query parameter to limit the documents on which an aggregation runs.

By default, searches containing an aggregation return both search hits and aggregation results. To return only aggregation results, set size to 0

SearchRequest searchRequest = new SearchRequest(index.toLowerCase());
searchRequest.allowPartialSearchResults(true);
searchRequest.indicesOptions(IndicesOptions.lenientExpandOpen());

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.from(0);
searchSourceBuilder.size(0);
searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

// @formatter:off

searchSourceBuilder.aggregation(
        AggregationBuilders
            .terms("propertyTypeKeys")
                .field("PropertyTypeKey").size(20)
                .order(BucketOrder.key(true)));

// @formatter:on

searchRequest.source(searchSourceBuilder);

log.info("sending search request to elastic...");

Map<String, Long> results = new HashMap<>();

try {
    SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

    log.info("search got response from elastic!");

    if (searchResponse.getAggregations() != null) {

        Terms propertyTypeKeys = searchResponse.getAggregations().get("propertyTypeKeys");

        for (Terms.Bucket bucket : propertyTypeKeys.getBuckets()) {
            log.info("keyAsString={}, docCount={}", bucket.getKeyAsString(), bucket.getDocCount());

            results.put(bucket.getKeyAsString(), bucket.getDocCount());
        }
        
    } else {
        log.info("no aggregations!");
    }

    log.info("sending reponse to client! ,size={}", searchResponse.getHits().getHits().length);

} catch (IOException e) {
    e.printStackTrace();
} catch (ElasticsearchException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}

You can specify multiple aggregations in the same request.

SearchRequest searchRequest = new SearchRequest(index.toLowerCase());
        searchRequest.allowPartialSearchResults(true);
        searchRequest.indicesOptions(IndicesOptions.lenientExpandOpen());

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.from(0);
        
        // set size to 0 to only return aggregations
        searchSourceBuilder.size(0);
        searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

        // @formatter:off

        searchSourceBuilder.aggregation(
                AggregationBuilders
                    .terms("propertyTypeKeys")
                        .field("PropertyTypeKey").size(20)
                        .order(BucketOrder.key(true)));
                searchSourceBuilder.aggregation(
                AggregationBuilders
                    .terms("propertySubTypeKeys")
                        .field("PropertySubTypeKey").size(100)
                        .order(BucketOrder.key(true)));
        
        // @formatter:on

        searchRequest.source(searchSourceBuilder);

        log.info("sending search request to elastic...");

        Map<String, Long> results = new HashMap<>();

        try {
            SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

            log.info("search got response from elastic!");

            if (searchResponse.getHits() != null && searchResponse.getHits().getHits() != null) {
                for (SearchHit hit : searchResponse.getHits().getHits()) {
                    log.info("source={}", hit.getSourceAsString());
                }
            }

            log.info("retreiving aggregations...");

            if (searchResponse.getAggregations() != null) {

                Terms propertyTypeKeys = searchResponse.getAggregations().get("propertyTypeKeys");

                for (Terms.Bucket bucket : propertyTypeKeys.getBuckets()) {
                    log.info("keyAsString={}, docCount={}", bucket.getKeyAsString(), bucket.getDocCount());

                    results.put(bucket.getKeyAsString(), bucket.getDocCount());
                }

                                Terms propertySubTypeKeys = searchResponse.getAggregations().get("propertySubTypeKeys");

                for (Terms.Bucket bucket : propertySubTypeKeys.getBuckets()) {
                    log.info("keyAsString={}, docCount={}", bucket.getKeyAsString(), bucket.getDocCount());

                    results.put(bucket.getKeyAsString(), bucket.getDocCount());
                }

            } else {
                log.info("no aggregations!");
            }

            log.info("sending reponse to client! ,size={}", searchResponse.getHits().getHits().length);

        } catch (IOException e) {
            log.error("Property search elastic search error. IOException, msg={}", e.getLocalizedMessage());

            e.printStackTrace();
        } catch (ElasticsearchException e) {
            log.error("Property search elastic search error. ElasticsearchStatusException, msg={},{},{}",
                    e.getDetailedMessage(), e.getLocalizedMessage(), e.getResourceId());

            e.printStackTrace();
        } catch (Exception e) {
            log.error("Property search elastic search error. Exception, msg={},{}", e.getMessage(),
                    e.getLocalizedMessage());

            e.printStackTrace();
        }

 




Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Leave a Reply

Your email address will not be published.