Creating Aggregations

Each aggregation has a different purpose and should be processed differently once returned from the search engine, but you add the aggregation information to the search request in a similar way between all aggregations.

Instantiate and Construct the Aggregation

  1. Use the to instantiate the aggregation you’ll construct. For example,

    PercentilesAggregation percentilesAggregation =
        aggregations.percentiles("percentiles", Field.PRIORITY);

    To discover what fields each aggregation must have (e.g., Sting name, String field in the case of the above PercentilesAggregation), see the Aggregations interface.

  2. Build out the aggregation to get the desired response. This looks different for each aggregation type, but Elasticsearch’s documentation on the aggregation type explains it well (such as Percentiles Aggregations) combined with the setters in Liferay’s corresponding interface.

    For example, use the setPercentilesMethod(PercentilesMethod.HDR) method to use the High Dynamic Range Histogram for calculating the percentiles.


Once the aggregation itself is in good shape, feed it to the search query.

Build the Search Query

  1. Get an instance of from the SearchRequestBuilderFactory service:

    SearchRequestBuilder searchRequestBuilder = 
  2. Get a instance from the builder, then add the aggregation to it and run its build method:

    SearchRequest searchRequest =

Execute the Search Query

  1. Perform a search using the Searcher service and the SearchRequest to get a

  2. To satisfy the dependencies of the example code here, get a reference to and

    protected Searcher searcher;
    SearchRequestBuilderFactory searchRequestBuilderFactory;

Process the response

What you’ll do with the SearchResponse returned by the call depends on the type of aggregation and your specific use case. A separate article will be written to demonstrate how to process the response.

« AggregationsStatistical Aggregations »
¿Fue útil este artículo?
Usuarios a los que les pareció útil: 0 de 0