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.
-
Get a reference to
com.liferay.portal.search.searcher.SearchRequestBuilderFactory
:@Reference SearchRequestBuilderFactory searchRequestBuilderFactory;
-
Get an instance of
com.liferay.portal.search.searcher.SearchRequestBuilder
:SearchRequestBuilder searchRequestBuilder = searchRequestBuilderFactory.builder();
-
Get a
com.liferay.portal.search.searcher.SearchRequest
instance from the builder:SearchRequest searchRequest = searchRequestBuilder.build();
-
Get a reference to
com.liferay.portal.search.stats.StatsRequestBuilderFactory
:@Reference StatsRequestBuilderFactory statsRequestBuilderFactory;
-
Get a
com.liferay.portal.search.stats.StatsRequestBuilder
instance and buildcom.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();
-
Set
StatsRequest
on theSearchRequest
:searchRequest.statsRequests(statsRequest);
-
Get a reference to
com.liferay.portal.search.searcher.Searcher
:@Reference protected Searcher searcher;
-
Perform a search using
Searcher
andSearchRequest
to getcom.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.
-
Get the map containing the metrics aggregations computed by the search engine:
Map<String, StatsResponse> map = searchResponse.getStatsResponseMap();
-
Get the
StatsResponse
for a given field:StatsResponse statsResponse = map.get(field);
-
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:
-
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); } };
-
Set
Stats
on theSearchContext
: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.StatsRequest | com.liferay.portal.search.api |
com.liferay.portal.search.stats.StatsRequestBuilder | com.liferay.portal.search.api |
com.liferay.portal.search.stats.StatsRequestBuilderFactory | com.liferay.portal.search.api |
com.liferay.portal.search.stats.StatsResponse | com.liferay.portal.search.api |
com.liferay.portal.kernel.search.Stats | portal-kernel |
Deprecated APIs
- SearchSearchRequest#getStats()
- SearchSearchRequest#setStats(Map<String, Stats> stats)