Create custom Aggregation¶
To create a custom Aggregation, create an aggregation class. In the following example, an aggregation groups the Location query results by the Location priority:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
The PriorityRangeAggregation
class extends AbstractRangeAggregation
.
The name of the class indicates that it aggregates the results by using the Range aggregation.
An aggregation must implement the Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation
interface or inherit one of following abstract classes:
Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation\AbstractRangeAggregation
Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation\AbstractStatsAggregation
Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation\AbstractTermAggregation
An aggregation can also implement one of the following interfaces:
Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation\FieldAggregation
, based on content FieldIbexa\Contracts\Core\Repository\Values\Content\Query\Aggregation\LocationAggregation
, based on content LocationIbexa\Contracts\Core\Repository\Values\Content\Query\Aggregation\RawAggregation
, based on details of the index structure
Aggregation definition
An aggregation definition must contain at least the name of an aggregation and optional aggregation parameters, such as, for example, the path (string) that is used to limit aggregation results to a specific subtree, Content Type identifier, or Field definition identifier, which will be mapped to the search index field name.
Aggregation definition must be independent of the search engine used.
A custom aggregation requires that the following elements are provided:
- An aggregation visitor that returns an array of results
- A result extractor that transforms raw aggregation results from the search engine
into
AggregationResult
objects
In simpler cases, you can apply one of the built-in visitors that correspond
to the aggregation type.
The example below uses RangeAggregationVisitor
:
1 2 3 4 5 6 7 8 9 |
|
1 2 3 4 5 6 7 8 9 |
|
The visitor is created by SearchFieldAggregationVisitorFactory
.
You provide it with two arguments:
- The aggregation class in
aggregationClass
- The field name in search index in
searchIndexFieldName
Tag the service with ibexa.search.solr.query.location.aggregation.visitor
.
Tag the service with ibexa.elasticsearch.query.location.aggregation_visitor
.
For the result extractor, you can use the built-in RangeAggregationResultExtractor
and provide it with the aggregation class in the aggregationClass
parameter.
Tag the service with ibexa.search.solr.query.location.aggregation.result.extractor
.
1 2 3 4 5 6 7 8 |
|
Tag the service with ibexa.search.elasticsearch.query.location.aggregation.result.extractor
.
1 2 3 4 5 6 7 |
|
You can use a different type of aggregation, followed by respective visitor and extractor classes:
Ibexa\Solr\Query\Common\AggregationVisitor\StatsAggregationVisitor
Ibexa\Solr\Query\Common\AggregationVisitor\TermAggregationVisitor
Ibexa\Solr\ResultExtractor\AggregationResultExtractor\StatsAggregationResultExtractor
Ibexa\Solr\ResultExtractor\AggregationResultExtractor\TermAggregationResultExtractor
Ibexa\ElasticSearchEngine\Query\AggregationVisitor\RangeAggregationVisitor
Ibexa\ElasticSearchEngine\Query\AggregationVisitor\StatsAggregationVisitor
-
Ibexa\ElasticSearchEngine\Query\AggregationVisitor\TermAggregationVisitor
-
Ibexa\ElasticSearchEngine\Query\ResultExtractor\AggregationResultExtractor\RangeAggregationResultExtractor
Ibexa\ElasticSearchEngine\Query\ResultExtractor\AggregationResultExtractor\StatsAggregationResultExtractor
Ibexa\ElasticSearchEngine\Query\ResultExtractor\AggregationResultExtractor\TermAggregationResultExtractor
In a more complex use case, you must create your own visitor and extractor.
Create aggregation visitor¶
The aggregation visitor must implement Ibexa\Contracts\Solr\Query\AggregationVisitor
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
The aggregation visitor must implement Ibexa\Contracts\ElasticSearchEngine\Query\AggregationVisitor
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
The canVisit()
method checks whether the provided aggregation is of the supported type
(in this case, your custom PriorityRangeAggregation
).
The visit()
method returns an array of results.
Create result extractor¶
You must also create a result extractor, which implements Ibexa\Solr\ResultExtractor\AggregationResultExtractor
that transforms raw aggregation results from Solr into AggregationResult
objects:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
The canVisit()
method checks whether the provided aggregation is of the supported type
(in this case, your custom PriorityRangeAggregation
).
The extract()
method converts the raw data provided by the search engine to a RangeAggregationResult
object.
You must also create a result extractor, which implements Ibexa\Contracts\ElasticSearchEngine\Query\AggregationResultExtractor
that transforms raw aggregation results from Elasticsearch into AggregationResult
objects:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
The supports()
method checks whether the provided aggregation is of the supported type
(in this case, your custom PriorityRangeAggregation
).
The extract()
method converts the raw data provided by the search engine to a RangeAggregationResult
object.
Finally, register both the aggregation visitor and the result extractor as services.
Tag the aggregation visitor with ibexa.search.solr.query.location.aggregation.visitor
and the result extractor with ibexa.search.solr.query.location.aggregation.result.extractor
:
1 2 3 4 5 6 7 8 9 10 |
|
For content-based aggregations, use the ibexa.search.solr.query.content.aggregation.visitor
and ibexa.search.solr.query.content.aggregation.result.extractor
tags respectively.
Tag the aggregation visitor with ibexa.elasticsearch.query.location.aggregation_visitor
and the result extractor with ibexa.elasticsearch.query.location.aggregation_result_extractor
:
1 2 3 4 5 6 7 8 9 10 |
|
For content-based aggregations, use the ibexa.search.elasticsearch.query.content.aggregation.visitor
and ibexa.search.elasticsearch.query.content.aggregation.result.extractor
tags respectively.