SumAggregation example

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)),
        };
    }

    private function parseEqualsFilter(EqualsFilter $filter, EntityDefinition $definition, Context $context): BuilderInterface
    {
        
use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\SumAggregation;

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

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

    public function testClone(): void
    {
        
'name' => 'sub 2',
                    'stock' => 10,
                ],
            ],
        ];

        $context = Context::createDefaultContext();

        $this->repository->create($data$context);

        $criteria = new Criteria();
        $criteria->addAggregation(new SumAggregation('stock_sum', 'root.sub.stock'));
        $result = $this->repository->search($criteria$context);

        static::assertTrue($result->getAggregations()->has('stock_sum'));
        $sum = $result->getAggregations()->get('stock_sum');

        /** @var SumResult $sum */
        static::assertInstanceOf(SumResult::class$sum);

        static::assertEquals(11, $sum->getSum());
    }

    

        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;
                }

                return new RangeAggregation($name(string) $field$aggregation['ranges']);
            case 'entity':
                
$versionContext = $context->createWithVersionId($versionId);

        $this->productRepository->update([
            [
                'id' => $id,
                'price' => [['currencyId' => Defaults::CURRENCY, 'gross' => 1000, 'net' => 1000, 'linked' => false]],
            ],
        ]$versionContext);

        $criteria = new Criteria();
        $criteria->addFilter(new EqualsFilter('product.ean', 'EAN'));
        $criteria->addAggregation(new SumAggregation('sum_price', 'product.price.gross'));

        $aggregations = $this->productRepository->aggregate($criteria$context);
        static::assertTrue($aggregations->has('sum_price'));
        /** @var SumResult $sum */
        $sum = $aggregations->get('sum_price');
        static::assertEquals(100, $sum->getSum());

        $aggregations = $this->productRepository->aggregate($criteria$versionContext);
        static::assertTrue($aggregations->has('sum_price'));
        /** @var SumResult $sum */
        $sum = $aggregations->get('sum_price');
        
/** * @depends testIndexing */
    public function testSumAggregation(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 SumAggregation('sum-stock', 'product.stock'));

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

            static::assertCount(1, $aggregations);

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

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

            static::assertEquals(1362, $result->getSum());
        }


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

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

        $criteria->addAggregation(
            new SumAggregation('sum-price', 'product.price')
        );

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

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

        $sum = $result->get('sum-price');
        static::assertInstanceOf(SumResult::class$sum);

        static::assertSame(750.0, $sum->getSum());
    }

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