-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathFindWithAggregationCommand.php
More file actions
57 lines (44 loc) · 2.05 KB
/
Copy pathFindWithAggregationCommand.php
File metadata and controls
57 lines (44 loc) · 2.05 KB
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
52
53
54
55
56
57
<?php declare(strict_types=1);
namespace App\Command;
use Ibexa\Contracts\Core\Repository\SearchService;
use Ibexa\Contracts\Core\Repository\Values\Content\LocationQuery;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation\ContentTypeTermAggregation;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation\Field\SelectionTermAggregation;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(
name: 'doc:find_with_aggregation',
description: 'Counts content per content type and the value of Selection Field.'
)]
class FindWithAggregationCommand extends Command
{
private SearchService $searchService;
public function __construct(SearchService $searchService)
{
$this->searchService = $searchService;
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$query = new LocationQuery();
$query->query = new Criterion\ParentLocationId(2);
$contentTypeTermAggregation = new ContentTypeTermAggregation('content_type');
$contentTypeTermAggregation->setLimit(5);
$contentTypeTermAggregation->setMinCount(10);
$query->aggregations[] = $contentTypeTermAggregation;
$query->aggregations[] = new SelectionTermAggregation('selection', 'blog_post', 'topic');
$results = $this->searchService->findContentInfo($query);
$contentByType = $results->aggregations->get('content_type');
$contentBySelection = $results->aggregations->get('selection');
foreach ($contentByType as $contentType => $count) {
$output->writeln($contentType->getName() . ': ' . $count);
}
foreach ($contentBySelection as $selection => $count) {
$output->writeln($selection . ': ' . $count);
}
return self::SUCCESS;
}
}