StatsAggregation example

$criteria->setLimit(200);

        // check that provided criteria has other key         $route->load(new Request()$criteria$context);
    }

    public static function criteriaProvider(): \Generator
    {
        yield 'Paginated criteria' => [(new Criteria())->setOffset(1)->setLimit(20)];
        yield 'Filtered criteria' => [(new Criteria())->addFilter(new EqualsFilter('active', true))];
        yield 'Post filtered criteria' => [(new Criteria())->addPostFilter(new EqualsFilter('active', true))];
        yield 'Aggregation criteria' => [(new Criteria())->addAggregation(new StatsAggregation('name', 'name'))];
        yield 'Query criteria' => [(new Criteria())->addQuery(new ScoreQuery(new EqualsFilter('active', true), 200))];
        yield 'Term criteria' => [(new Criteria())->setTerm('test')];
        yield 'Sorted criteria' => [(new Criteria())->addSorting(new FieldSorting('active'))];
    }

    /** * @dataProvider invalidationProvider */
    public function testInvalidation(\Closure $before, \Closure $after, int $calls): void
    {
        $this->getContainer()->get('cache.object')
            
$criteria->setLimit(200);

        // check that provided criteria has other key         $route->load($ids->get('product')new Request()$context$criteria);
    }

    public static function criteriaProvider(): \Generator
    {
        yield 'Paginated criteria' => [(new Criteria())->setOffset(1)->setLimit(20)];
        yield 'Filtered criteria' => [(new Criteria())->addFilter(new EqualsFilter('active', true))];
        yield 'Post filtered criteria' => [(new Criteria())->addPostFilter(new EqualsFilter('active', true))];
        yield 'Aggregation criteria' => [(new Criteria())->addAggregation(new StatsAggregation('name', 'name'))];
        yield 'Query criteria' => [(new Criteria())->addQuery(new ScoreQuery(new EqualsFilter('active', true), 200))];
        yield 'Term criteria' => [(new Criteria())->setTerm('test')];
        yield 'Sorted criteria' => [(new Criteria())->addSorting(new FieldSorting('active'))];
    }

    /** * @dataProvider invalidationProvider */
    public function testInvalidation(IdsCollection $ids, \Closure $before, \Closure $after, int $calls): void
    {
        $this->getContainer()->get('cache.object')
            
if ($type !== 'filter') {
            $field = self::buildFieldName($definition$aggregation['field']);
        }
        switch ($type) {
            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;
                }

                
Request::METHOD_POST,
        ];

        yield 'Test GET filter will be generated with min and max price' => [
            new Request(['min-price' => 10.0, 'max-price' => 20.0]),
            self::create(true, new RangeFilter('product.cheapestPrice', [RangeFilter::GTE => 10.0, RangeFilter::LTE => 20.0])['min' => 10.0, 'max' => 20.0]),
        ];
    }

    private static function create(bool $filtered, DALFilter $filter, mixed $values): Filter
    {
        $aggregations = [new StatsAggregation('price', 'product.cheapestPrice', true, true, false, false)];

        return new Filter('price', $filtered$aggregations$filter$values);
    }
}
$criteria->setLimit(200);

        // check that provided criteria has other key         $route->load($ids->get('c.1')new Request()$context$criteria);
    }

    public static function criteriaProvider(): \Generator
    {
        yield 'Paginated criteria' => [(new Criteria())->setOffset(1)->setLimit(20)];
        yield 'Filtered criteria' => [(new Criteria())->addFilter(new EqualsFilter('active', true))];
        yield 'Post filtered criteria' => [(new Criteria())->addPostFilter(new EqualsFilter('active', true))];
        yield 'Aggregation criteria' => [(new Criteria())->addAggregation(new StatsAggregation('price', 'price'))];
        yield 'Query criteria' => [(new Criteria())->addQuery(new ScoreQuery(new EqualsFilter('active', true), 200))];
        yield 'Term criteria' => [(new Criteria())->setTerm('test')];
        yield 'Sorted criteria' => [(new Criteria())->addSorting(new FieldSorting('active'))];
    }

    /** * @dataProvider stateProvider */
    public function testStates(array $current, array $config): void
    {
        $ids = new IdsCollection();

        
if ($aggregation->getLimit()) {
            $composite->addParameter('size', (string) $aggregation->getLimit());
        }

        return $composite;
    }

    protected function parseStatsAggregation(StatsAggregation $aggregation, string $fieldName, Context $context): Metric\StatsAggregation
    {
        if ($this->isCheapestPriceField($aggregation->getField())) {
            return new Metric\StatsAggregation($aggregation->getName(), null, [
                'id' => 'cheapest_price',
                'params' => $this->getCheapestPriceParameters($context),
            ]);
        }

        if ($this->isCheapestPriceField($aggregation->getField(), true)) {
            return new Metric\StatsAggregation($aggregation->getName(), null, [
                'id' => 'cheapest_price_percentage',
                'params' => ['accessors' => $this->getCheapestPriceAccessors($context, true)],
            ]);
        }

        
$range = [];
        if ($min !== null && $min >= 0) {
            $range[RangeFilter::GTE] = $min;
        }
        if ($max !== null && $max >= 0) {
            $range[RangeFilter::LTE] = $max;
        }

        return new Filter(
            'price',
            !empty($range),
            [new StatsAggregation('price', 'product.cheapestPrice', true, true, false, false)],
            new RangeFilter('product.cheapestPrice', $range),
            [
                'min' => (float) $request->get('min-price'),
                'max' => (float) $request->get('max-price'),
            ]
        );
    }
}
throw $e;
        }
    }

    /** * @depends testIndexing */
    public function testAggregation(IdsCollection $ids): void
    {
        try {
            $criteria = new Criteria(array_values($ids->all()));
            $criteria->addAggregation(new StatsAggregation('price', 'product.cheapestPrice'));

            $context = $this->getContainer()->get(SalesChannelContextFactory::class)
                ->create(Uuid::randomHex(), TestDefaults::SALES_CHANNEL);

            $cases = $this->providerAggregation();
            foreach ($cases as $message => $case) {
                $context->setRuleIds($ids->getList($case['rules']));

                $result = $this->getContainer()->get('sales_channel.product.repository')
                    ->aggregate($criteria$context);

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

            // check simple search without any restrictions             $criteria = new Criteria($data->prefixed('product-'));
            $criteria->addState(Criteria::STATE_ELASTICSEARCH_AWARE);
            $criteria->addAggregation(new StatsAggregation('price-stats', 'product.cheapestPrice'));

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

            static::assertCount(1, $aggregations);

            static::assertTrue($aggregations->has('price-stats'));

            $result = $aggregations->get('price-stats');
            static::assertInstanceOf(StatsResult::class$result);

            static::assertEquals(50, $result->getMin());
            
$criteria->setLimit(200);

        // check that provided criteria has other key         $route->load(new Request()$context$criteria);
    }

    public static function criteriaProvider(): \Generator
    {
        yield 'Paginated criteria' => [(new Criteria())->setOffset(1)->setLimit(20)];
        yield 'Filtered criteria' => [(new Criteria())->addFilter(new EqualsFilter('active', true))];
        yield 'Post filtered criteria' => [(new Criteria())->addPostFilter(new EqualsFilter('active', true))];
        yield 'Aggregation criteria' => [(new Criteria())->addAggregation(new StatsAggregation('name', 'name'))];
        yield 'Query criteria' => [(new Criteria())->addQuery(new ScoreQuery(new EqualsFilter('active', true), 200))];
        yield 'Term criteria' => [(new Criteria())->setTerm('test')];
        yield 'Sorted criteria' => [(new Criteria())->addSorting(new FieldSorting('active'))];
    }

    /** * @dataProvider invalidationProvider */
    public function testInvalidation(\Closure $before, \Closure $after, int $calls): void
    {
        $this->getContainer()->get('cache.object')
            
use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\StatsAggregation;

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

        static::assertEquals([
            'name' => 'foo',
            'extensions' => [],
            'field' => 'bar',
            'max' => true,
            'min' => true,
            'avg' => true,
            'sum' => true,
            '_class' => StatsAggregation::class,
        ]$aggregation->jsonSerialize());
    }


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

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

        $criteria->addAggregation(
            new StatsAggregation('stats-price', 'product.price')
        );

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

        static::assertTrue($result->has('stats-price'));

        $stats = $result->get('stats-price');
        static::assertInstanceOf(StatsResult::class$stats);

        static::assertEquals(50, $stats->getMin());
        static::assertEquals(250, $stats->getMax());
        
Home | Imprint | This part of the site doesn't use cookies.