MinAggregation example



    private function buildCriteriaForNextScheduledTask(): Criteria
    {
        $criteria = new Criteria();
        $criteria->addFilter(
            new EqualsAnyFilter('status', [
                ScheduledTaskDefinition::STATUS_SCHEDULED,
                ScheduledTaskDefinition::STATUS_SKIPPED,
            ])
        )
        ->addAggregation(new MinAggregation('nextExecutionTime', 'nextExecutionTime'));

        return $criteria;
    }

    private function buildCriteriaForMinRunInterval(): Criteria
    {
        $criteria = new Criteria();
        $criteria->addFilter(
            new NotFilter(NotFilter::CONNECTION_OR, [
                new EqualsFilter('status', ScheduledTaskDefinition::STATUS_INACTIVE),
                new EqualsFilter('status', ScheduledTaskDefinition::STATUS_SKIPPED),
            ])
$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;
                }
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\MinAggregation;

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

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

    public function testClone(): void
    {
        


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

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

        $criteria->addAggregation(
            new MinAggregation('min-price', 'product.price')
        );

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

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

        $min = $result->get('min-price');
        static::assertInstanceOf(MinResult::class$min);

        static::assertEquals(50, $min->getMin());
    }

    
/** * @depends testIndexing */
    public function testMinAggregation(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 MinAggregation('min-stock', 'product.stock'));

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

            static::assertCount(1, $aggregations);

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

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

            static::assertEquals(1, $result->getMin());
        }
Home | Imprint | This part of the site doesn't use cookies.