Statistical Aggregations

Support for GroupBy and Stats aggregations were introduced in 7.0.

Cardinality Aggregations extend Liferay DXP’s metrics aggregation capabilities, providing an approximate (i.e., statistical) count of distinct values returned by a search query. For example, you could compute a count of distinct values of the tag field. Refer to the Elasticsearch documentation for more details.

While this functionality was available in the past directly in the portal kernel code, it’s been extracted and re-implemented in StatsRequest and StatsResponse, both introduced in the com.liferay.portal.search.api module to avoid modifying portal-kernel. StatsRequest provides the same statistical features that the legacy com.liferay.portal.kernel.search.Stats does, and adds the new cardinality option.

StatsRequest

The StatsRequest Provides a map of field names and the metric aggregations that are to be computed for each field.

  1. Get a reference to com.liferay.portal.search.searcher.SearchRequestBuilderFactory:

    @Reference
    SearchRequestBuilderFactory searchRequestBuilderFactory;
    
  2. Get an instance of com.liferay.portal.search.searcher.SearchRequestBuilder:

    SearchRequestBuilder searchRequestBuilder = searchRequestBuilderFactory.builder();
    
  3. Get a com.liferay.portal.search.searcher.SearchRequest instance from the builder:

    SearchRequest searchRequest = searchRequestBuilder.build();
    
  4. Get a reference to com.liferay.portal.search.stats.StatsRequestBuilderFactory:

    @Reference
    StatsRequestBuilderFactory statsRequestBuilderFactory;
    
  5. Get a com.liferay.portal.search.stats.StatsRequestBuilder instance and build com.liferay.portal.search.stats.StatsRequest with the desired metrics:

    StatsRequestBuilder statsRequestBuilder = 
        statsRequestBuilderFactory.getStatsRequestBuilder();
    StatsRequest statsRequest = statsRequestBuilder
        .cardinality(true)
        .count(true)
        .field(field)
        .max(true)
        .mean(true)
        .min(true)
        .missing(true)
        .sum(true)
        .sumOfSquares(true)
        .build();
    
  6. Set StatsRequest on the SearchRequest:

    searchRequest.statsRequests(statsRequest);
    
  7. Get a reference to com.liferay.portal.search.searcher.Searcher:

    @Reference
    protected Searcher searcher;
    
  8. Perform a search using Searcher and SearchRequest to get com.liferay.portal.search.searcher.SearchResponse:

    SearchResponse searcher.search(searchRequest);
    

Click here to see an example from Liferay’s codebase

StatsResponse

The stats response contains the metrics aggregations computed by the search engine for a given field.

  1. Get the map containing the metrics aggregations computed by the search engine:

    Map<String, StatsResponse> map = searchResponse.getStatsResponseMap();
    
  2. Get the StatsResponse for a given field:

    StatsResponse statsResponse = map.get(field);
    
  3. Get the desired metric, for example cardinality:

    statsResponse.getCardinality();
    

Click here to see an example from Liferay’s codebase

Using the Legacy Stats Object

The legacy com.liferay.portal.kernel.search.Stats object continues to be fully supported:

  1. Create a Stats instance with the desired metrics:

    Stats stats = new Stats() {
        {
            setCount(true);
            setField(field);
            setMax(true);
            setMean(true);
            setMin(true);
            setSum(true);
            setSumOfSquares(true);
        }
    };
    
  2. Set Stats on the SearchContext:

    searchRequestBuilder.withSearchContext(searchContext -> searchContext.addStats(stats));
    

Click here to see an example from Liferay’s codebase

External References

  • https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-aggregations-metrics.html
  • https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-aggregations-metrics-cardinality-aggregation.html
  • https://lucene.apache.org/solr/guide/7_5/the-stats-component.html

Search Engine Connector Support

  • Elasticsearch 6: Yes
  • Solr 7: Yes

New/Related APIs

These are the relevant APIs for building Statistics Aggregations:

API (FQCN)Provided by Artifact
com.liferay.portal.search.searcher.SearchRequestBuilder#statsRequests(StatsRequest... statsRequests)com.liferay.portal.search.api
com.liferay.portal.search.searcher.SearchResponse#getStatsResponseMap()com.liferay.portal.search.api
com.liferay.portal.search.stats.StatsRequestcom.liferay.portal.search.api
com.liferay.portal.search.stats.StatsRequestBuildercom.liferay.portal.search.api
com.liferay.portal.search.stats.StatsRequestBuilderFactorycom.liferay.portal.search.api
com.liferay.portal.search.stats.StatsResponsecom.liferay.portal.search.api
com.liferay.portal.kernel.search.Statsportal-kernel

Deprecated APIs

  • SearchSearchRequest#getStats()
  • SearchSearchRequest#setStats(Map<String, Stats> stats)
« Creating AggregationsModel Entity Indexing Framework »
Was this article helpful?
0 out of 0 found this helpful