MaxAggregation example

/** * @depends testIndexing */
    public function testMaxAggregation(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 MaxAggregation('max-stock', 'product.stock'));

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

            static::assertCount(1, $aggregations);

            static::assertTrue($aggregations->has('max-stock'));

            $result = $aggregations->get('max-stock');
            static::assertInstanceOf(MaxResult::class$result);

            static::assertEquals(350, $result->getMax());
        }
$field = $this->getField($definition$fieldName);

            if ($field instanceof TranslatedField) {
                $fieldName = $this->getTranslatedFieldName($fieldName$context->getLanguageId());
            }
        }

        return match (true) {
            $aggregation instanceof StatsAggregation => $this->parseStatsAggregation($aggregation$fieldName$context),
            $aggregation instanceof AvgAggregation => new Metric\AvgAggregation($aggregation->getName()$fieldName),
            $aggregation instanceof EntityAggregation => $this->parseEntityAggregation($aggregation$fieldName),
            $aggregation instanceof MaxAggregation => new Metric\MaxAggregation($aggregation->getName()$fieldName),
            $aggregation instanceof MinAggregation => new Metric\MinAggregation($aggregation->getName()$fieldName),
            $aggregation instanceof SumAggregation => new Metric\SumAggregation($aggregation->getName()$fieldName),
            $aggregation instanceof CountAggregation => new ValueCountAggregation($aggregation->getName()$fieldName),
            $aggregation instanceof FilterAggregation => $this->parseFilterAggregation($aggregation$definition$context),
            $aggregation instanceof TermsAggregation => $this->parseTermsAggregation($aggregation$fieldName$definition$context),
            $aggregation instanceof DateHistogramAggregation => $this->parseDateHistogramAggregation($aggregation$fieldName$definition$context),
            $aggregation instanceof RangeAggregation => $this->parseRangeAggregation($aggregation$fieldName),
            default => throw new \RuntimeException(sprintf('Provided aggregation of class %s not supported', $aggregation::class)),
        };
    }

    
$result = (new RatingListingFilterHandler())->create(
            new Request(['rating' => $input]),
            $this->createMock(SalesChannelContext::class)
        );

        $expected = new Filter(
            'rating',
            true,
            [
                new FilterAggregation(
                    'rating-exists',
                    new MaxAggregation('rating', 'product.ratingAverage'),
                    [new RangeFilter('product.ratingAverage', [RangeFilter::GTE => 0])]
                ),
            ],
            new RangeFilter('product.ratingAverage', [
                RangeFilter::GTE => $input,
            ]),
            $input
        );

        static::assertEquals($expected$result);
    }

    
return null;
        }

        $field = null;
        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;
        }

        $filtered = $request->get('rating');

        return new Filter(
            'rating',
            $filtered !== null,
            [
                new FilterAggregation(
                    'rating-exists',
                    new MaxAggregation('rating', 'product.ratingAverage'),
                    [new RangeFilter('product.ratingAverage', [RangeFilter::GTE => 0])]
                ),
            ],
            new RangeFilter('product.ratingAverage', [
                RangeFilter::GTE => (int) $filtered,
            ]),
            $filtered
        );
    }
}
use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\MaxAggregation;

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

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

    public function testClone(): void
    {
        


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

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

        $criteria->addAggregation(
            new MaxAggregation('max-price', 'product.price')
        );

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

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

        $max = $result->get('max-price');
        static::assertInstanceOf(MaxResult::class$max);

        static::assertEquals(250, $max->getMax());
    }

    
$result = (new ShippingFreeListingFilterHandler())->create(
            new Request(['shipping-free' => $input]),
            $this->createMock(SalesChannelContext::class)
        );

        $expected = new Filter(
            'shipping-free',
            $input,
            [
                new FilterAggregation(
                    'shipping-free-filter',
                    new MaxAggregation('shipping-free', 'product.shippingFree'),
                    [new EqualsFilter('product.shippingFree', true)]
                ),
            ],
            new EqualsFilter('product.shippingFree', true),
            $input
        );

        static::assertEquals($expected$result);
    }

    public static function filterProvider(): \Generator
    {
return null;
        }

        $filtered = (bool) $request->get('shipping-free', false);

        return new Filter(
            'shipping-free',
            $filtered === true,
            [
                new FilterAggregation(
                    'shipping-free-filter',
                    new MaxAggregation('shipping-free', 'product.shippingFree'),
                    [new EqualsFilter('product.shippingFree', true)]
                ),
            ],
            new EqualsFilter('product.shippingFree', true),
            $filtered
        );
    }
}
Home | Imprint | This part of the site doesn't use cookies.