CountAggregation example

/** * @depends testIndexing */
    public function testCountAggregation(IdsCollection $data): void
    {
        try {
            $aggregator = $this->createEntityAggregator();

            // check simple search without any restrictions             $criteria = new Criteria($data->prefixed('p'));
            $criteria->addState(Criteria::STATE_ELASTICSEARCH_AWARE);
            $criteria->addAggregation(new CountAggregation('manufacturer-count', 'product.manufacturerId'));

            $aggregations = $aggregator->aggregate($this->productDefinition, $criteria$this->context);

            static::assertCount(1, $aggregations);

            static::assertTrue($aggregations->has('manufacturer-count'));

            $result = $aggregations->get('manufacturer-count');
            static::assertInstanceOf(CountResult::class$result);

            static::assertEquals(6, $result->getCount());
        }
$updated = [
            'id' => $productId,
            'categories' => [
                ['id' => $categoryId3, 'name' => 'matching value'],
            ],
        ];
        $this->productRepository->update([$updated]$versionContext);

        $criteria = new Criteria();
        $criteria->addFilter(new EqualsFilter('product.id', $productId));
        $criteria->addAggregation(new CountAggregation('category_count', 'product.categories.id'));

        $result = $this->productRepository->aggregate($criteria$context);
        /** @var CountResult $aggregation */
        $aggregation = $result->get('category_count');

        static::assertInstanceOf(AggregationResult::class$aggregation);
        static::assertSame(2, $aggregation->getCount());

        $result = $this->productRepository->aggregate($criteria$versionContext);
        /** @var CountResult $aggregation */
        $aggregation = $result->get('category_count');

        


    public function testCountAggregation(): void
    {
        $context = Context::createDefaultContext();

        $criteria = new Criteria(
            $this->ids->getList(['p-1', 'p-2', 'p-3', 'p-4', 'p-5'])
        );

        $criteria->addAggregation(
            new CountAggregation('count-manufacturer', 'product.manufacturerId')
        );

        $result = $this->aggregator->aggregate($this->definition, $criteria$context);

        static::assertTrue($result->has('count-manufacturer'));

        $count = $result->get('count-manufacturer');
        static::assertInstanceOf(CountResult::class$count);

        static::assertEquals(3, $count->getCount());
    }

    
use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\CountAggregation;

/** * @internal * * @covers \Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\CountAggregation */
class CountAggregationTest extends TestCase
{
    public function testEncode(): void
    {
        $aggregation = new CountAggregation('foo', 'bar');

        static::assertEquals([
            'name' => 'foo',
            'extensions' => [],
            'field' => 'bar',
            '_class' => CountAggregation::class,
        ]$aggregation->jsonSerialize());
    }

    public function testClone(): void
    {
        
// Fetch additional level of categories for counting visible children that are NOT included in the original query         $criteria = new Criteria();
        $criteria->addFilter(
            new ContainsFilter('path', '|' . $rootId . '|'),
            new EqualsFilter('level', $rootLevel + $depth + 1),
            new EqualsFilter('active', true),
            new EqualsFilter('visible', true)
        );

        $criteria->addAggregation(
            new TermsAggregation('category-ids', 'parentId', null, null, new CountAggregation('visible-children-count', 'id'))
        );

        $termsResult = $this->categoryRepository
            ->aggregate($criteria$context)
            ->get('category-ids');

        if (!($termsResult instanceof TermsResult)) {
            return;
        }

        foreach ($termsResult->getBuckets() as $bucket) {
            
$criteria->addFilter(
            new MultiFilter(MultiFilter::CONNECTION_AND, [
                new MultiFilter(MultiFilter::CONNECTION_OR, $reviewFilters),
                new MultiFilter(MultiFilter::CONNECTION_OR, [
                    new EqualsFilter('product.id', $productId),
                    new EqualsFilter('product.parentId', $productId),
                ]),
            ])
        );

        $criteria->addAggregation(new CountAggregation('review-count', 'id'));

        return $criteria;
    }
}
yield 'test term' => [
            (new Criteria())->setTerm('foo'),
            '{"total-count-mode":0,"term":"foo"}',
        ];

        yield 'test query' => [
            (new Criteria())->addQuery(new ScoreQuery(new EqualsFilter('foo', 'bar'), 100)),
            '{"total-count-mode":0,"query":[{"score":100.0,"query":{"type":"equals","field":"foo","value":"bar"},"scoreField":null,"extensions":[]}]}',
        ];

        yield 'test aggregation' => [
            (new Criteria())->addAggregation(new CountAggregation('foo', 'bar')),
            '{"total-count-mode":0,"aggregations":[{"name":"foo","type":"count","field":"bar"}]}',
        ];

        yield 'test grouping' => [
            (new Criteria())->addGroupField(new FieldGrouping('foo')),
            '{"total-count-mode":0,"grouping":["foo"]}',
        ];
    }

    public function testConstructorDoesNotAllowEmptyIdArray(): void
    {
        
'Missing permissions for nested grouping' => [
                ['product:read'],
                (new Criteria())
                    ->addGroupField(new FieldGrouping('categories.media.private')),
                false,
            ],

            // aggregation validation             'Has permissions for aggregation' => [
                ['product:read', 'category:read'],
                (new Criteria())
                    ->addAggregation(new CountAggregation('count-agg', 'categories.active')),
                true,
            ],
            'Missing permissions for aggregation' => [
                ['product:read'],
                (new Criteria())
                    ->addAggregation(new CountAggregation('count-agg', 'categories.active')),
                false,
            ],
            'Has permissions for nested aggregation' => [
                ['product:read', 'category:read', 'media:read'],
                (new Criteria())
                    
case 'avg':
                return new AvgAggregation($name$field);
            case 'max':
                return new MaxAggregation($name$field);
            case 'min':
                return new MinAggregation($name$field);
            case 'stats':
                return new StatsAggregation($name$field);
            case 'sum':
                return new SumAggregation($name$field);
            case 'count':
                return new CountAggregation($name$field);
            case 'range':
                if (!isset($aggregation['ranges'])) {
                    $exceptions->add(new InvalidAggregationQueryException('The aggregation should contain "ranges".'), '/aggregations/' . $index . '/' . $type . '/field');

                    return null;
                }

                return new RangeAggregation($name(string) $field$aggregation['ranges']);
            case 'entity':
                if (!isset($aggregation['definition'])) {
                    $exceptions->add(new InvalidAggregationQueryException('The aggregation should contain a "definition".'), '/aggregations/' . $index . '/' . $type . '/field');

                    
Home | Imprint | This part of the site doesn't use cookies.