Skip to content

Commit a6c4770

Browse files
committed
Implement file size aggregation services for assets
1 parent 3c80eb9 commit a6c4770

File tree

10 files changed

+123
-6
lines changed

10 files changed

+123
-6
lines changed

config/services/search/search-services.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ services:
2222
Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\Element\ElementSearchHelperInterface:
2323
class: Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\Element\SearchHelper
2424

25+
Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\Asset\Aggregation\FileSizeAggregationServiceInterface:
26+
class: Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\Asset\Aggregation\FileSizeAggregationService
27+
2528
Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\SearchProviderInterface:
2629
class: Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\SearchProvider
2730

doc/04_Searching_For_Data_In_Index/05_Search_Modifiers/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ $search->addModifier(new ParentIdFilter(1))
5656
### Aggregations
5757

5858

59-
| Modifier | Modifier Category | Description |
60-
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------|-------------------------------------------------------------------------------------------------------|
61-
| [ChildrenCountAggregation](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/Aggregation/Tree/ChildrenCountAggregation.php) | Tree related aggregation | Get children counts for given element IDs. |
62-
| [AssetMetaDataAggregation](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/Aggregation/Asset/AssetMetaDataAggregation.php) | Assets | Used for the filters in the asset grid to aggregate the filter options for supported meta data types. |
59+
| Modifier | Modifier Category | Description |
60+
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
61+
| [ChildrenCountAggregation](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/Aggregation/Tree/ChildrenCountAggregation.php) | Tree related aggregation | Get children counts for given element IDs. |
62+
| [AssetMetaDataAggregation](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/Aggregation/Asset/AssetMetaDataAggregation.php) | Assets | Used for the filters in the asset grid to aggregate the filter options for supported meta data types. |
63+
| [FileSizeSumAggregation](https://github.com/pimcore/generic-data-index-bundle/blob/1.x/src/Model/Search/Modifier/Aggregation/Asset/FileSizeSumAggregation.php) | Assets | Aggregates the sum of file sizes for all assets for a given search. The `FileSizeAggregationServiceInterface` internally uses this aggregation and provides an easy way to use this functionality. |
6364

6465
## Add your own search modifier
6566

src/Model/Search/Asset/SearchResult/AssetSearchResultItem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ public function isHasChildren(): bool
279279
$this->lazyLoad();
280280
}
281281

282-
return $this->hasChildren;
282+
return $this->hasChildren ?? false;
283283
}
284284

285285
public function setHasChildren(bool $hasChildren): AssetSearchResultItem
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* Pimcore
6+
*
7+
* This source file is available under two different licenses:
8+
* - GNU General Public License version 3 (GPLv3)
9+
* - Pimcore Commercial License (PCL)
10+
* Full copyright and license information is available in
11+
* LICENSE.md which is distributed with this source code.
12+
*
13+
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
14+
* @license http://www.pimcore.org/license GPLv3 and PCL
15+
*/
16+
17+
namespace Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Aggregation\Asset;
18+
19+
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\SearchModifierInterface;
20+
21+
final readonly class FileSizeSumAggregation implements SearchModifierInterface
22+
{
23+
public function __construct(
24+
private string $aggregationName,
25+
) {
26+
}
27+
28+
public function getAggregationName(): string
29+
{
30+
return $this->aggregationName;
31+
}
32+
}

src/Model/SearchIndexAdapter/SearchResultAggregation.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function __construct(
2424
private array $buckets,
2525
private int $otherDocCount,
2626
private int $docCountErrorUpperBound,
27+
private array $aggregationResult = [],
2728
) {
2829
}
2930

@@ -46,4 +47,10 @@ public function getDocCountErrorUpperBound(): int
4647
{
4748
return $this->docCountErrorUpperBound;
4849
}
50+
51+
public function getAggregationResult(): array
52+
{
53+
return $this->aggregationResult;
54+
}
55+
4956
}

src/Model/SearchIndexAdapter/SearchResultAggregationBucket.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
readonly class SearchResultAggregationBucket
2020
{
2121
public function __construct(
22-
private string|int $key,
22+
private string|int|float $key,
2323
private int $docCount,
2424
) {
2525
}

src/SearchIndexAdapter/OpenSearch/Search/Modifier/Aggregation/AssetAggregations.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818

1919
use Pimcore\Bundle\GenericDataIndexBundle\Attribute\OpenSearch\AsSearchModifierHandler;
2020
use Pimcore\Bundle\GenericDataIndexBundle\Exception\InvalidModifierException;
21+
use Pimcore\Bundle\GenericDataIndexBundle\Model\OpenSearch\Aggregation\Aggregation;
2122
use Pimcore\Bundle\GenericDataIndexBundle\Model\OpenSearch\Modifier\SearchModifierContextInterface;
23+
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\AssetSearch;
2224
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Aggregation\Asset\AssetMetaDataAggregation;
25+
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Aggregation\Asset\FileSizeSumAggregation;
2326
use Pimcore\Bundle\GenericDataIndexBundle\SearchIndexAdapter\Asset\FieldDefinitionServiceInterface;
2427
use Pimcore\Twig\Extension\Templating\Placeholder\Exception;
2528

@@ -64,4 +67,26 @@ public function handleAssetMetaDataAggregation(
6467
throw new InvalidModifierException($e->getMessage(), 0, $e);
6568
}
6669
}
70+
71+
#[AsSearchModifierHandler]
72+
public function handleFileSizeAggregation(
73+
FileSizeSumAggregation $aggregation,
74+
SearchModifierContextInterface $context
75+
): void
76+
{
77+
if (!$context->getOriginalSearch() instanceof AssetSearch) {
78+
throw new InvalidModifierException('FileSizeAggregation can only be used with AssetSearch');
79+
}
80+
81+
$context->getSearch()->addAggregation(
82+
new Aggregation(
83+
name: $aggregation->getAggregationName(),
84+
params: [
85+
'sum' => [
86+
'field' => 'system_fields.fileSize',
87+
],
88+
]
89+
)
90+
);
91+
}
6792
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\Asset\Aggregation;
5+
6+
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\AssetSearch;
7+
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Modifier\Aggregation\Asset\FileSizeSumAggregation;
8+
use Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\Asset\AssetSearchServiceInterface;
9+
10+
/**
11+
* @internal
12+
*/
13+
final readonly class FileSizeAggregationService implements FileSizeAggregationServiceInterface
14+
{
15+
public function __construct(
16+
private AssetSearchServiceInterface $assetSearchService,
17+
)
18+
{
19+
}
20+
21+
public function getFileSizeSum(AssetSearch $assetSearch): int
22+
{
23+
$aggregation = new FileSizeSumAggregation('fileSizeSum');
24+
$assetSearch
25+
->addModifier($aggregation)
26+
->setAggregationsOnly(true)
27+
;
28+
29+
$result = $this->assetSearchService->search($assetSearch);
30+
31+
$sum = $result->getAggregation($aggregation->getAggregationName())?->getAggregationResult()['value'] ?? 0;
32+
return (int) $sum;
33+
}
34+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Pimcore\Bundle\GenericDataIndexBundle\Service\Search\SearchService\Asset\Aggregation;
5+
6+
use Pimcore\Bundle\GenericDataIndexBundle\Model\Search\Asset\AssetSearch;
7+
8+
interface FileSizeAggregationServiceInterface
9+
{
10+
/**
11+
* Returns the sum of the file sizes of all assets that match the given search criteria in bytes.
12+
*/
13+
public function getFileSizeSum(AssetSearch $assetSearch): int;
14+
}

src/Service/Serializer/Denormalizer/SearchIndexAdapter/SearchResultDenormalizer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ private function hydrateAggregations(array $aggregations): array
7878
buckets: $this->hydrateAggregationBuckets($aggregation['buckets'] ?? []),
7979
otherDocCount: $aggregation['sum_other_doc_count'] ?? 0,
8080
docCountErrorUpperBound: $aggregation['doc_count_error_upper_bound'] ?? 0,
81+
aggregationResult: $aggregation,
8182
);
8283
}
8384

0 commit comments

Comments
 (0)