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.
com.liferay.portal.search.aggregation.Aggregationsto 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 fieldin the case of the above
PercentilesAggregation), see the
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.
Get an instance of
SearchRequestBuilder searchRequestBuilder = searchRequestBuilderFactory.builder();
com.liferay.portal.search.searcher.SearchRequestinstance from the builder, then add the aggregation to it and run its
SearchRequest searchRequest = searchRequestBuilder.addAggregation(percentilesAggregation).build();
Perform a search using the
Searcherservice and the
SearchRequestto get a
To satisfy the dependencies of the example code here, get a reference to
@Reference protected Searcher searcher; @Reference SearchRequestBuilderFactory searchRequestBuilderFactory;
What you’ll do with the
SearchResponse returned by the
depends on the type of aggregation and your specific use case. A separate
article will be written to demonstrate how to process the response.