getRanges example


        $aggregator = $this->createEntityAggregator();
        $criteria = new Criteria($data->prefixed('product-'));
        $criteria->addState(Criteria::STATE_ELASTICSEARCH_AWARE);
        $criteria->addAggregation(new RangeAggregation('test-range-aggregation', 'product.stock', $rangesDefinition));

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

        static::assertTrue($aggregations->has('test-range-aggregation'));
        static::assertInstanceOf(RangeResult::class$aggregations->get('test-range-aggregation'));

        $rangesResult = $aggregations->get('test-range-aggregation')->getRanges();

        static::assertCount(\count($rangesDefinition)$rangesResult);
        foreach ($rangesResult as $key => $count) {
            static::assertArrayHasKey($key$rangesExpectedResult);
            static::assertEquals($rangesExpectedResult[$key]$count);
        }
    }

    /** * @depends testIndexing */
    
RangeAggregation $aggregation,
        QueryBuilder $query,
        EntityDefinition $definition,
        Context $context
    ): void {
        $accessor = $this->queryHelper->getFieldAccessor($aggregation->getField()$definition$definition->getEntityName()$context);
        $field = $this->queryHelper->getField($aggregation->getField()$definition$definition->getEntityName());
        if (!$field instanceof PriceField && !$field instanceof FloatField && !$field instanceof IntField) {
            throw new \RuntimeException(sprintf('Provided field "%s" is not supported in RangeAggregation (supports : PriceField, FloatField, IntField)', $aggregation->getField()));
        }
        // build SUM() with range criteria for each range and add it to select         foreach ($aggregation->getRanges() as $range) {
            $id = $range['key'] ?? (($range['from'] ?? '*') . '-' . ($range['to'] ?? '*'));
            $sum = '1';
            if (isset($range['from'])) {
                $sum .= sprintf(' AND %s >= %f', $accessor$range['from']);
            }
            if (isset($range['to'])) {
                $sum .= sprintf(' AND %s < %f', $accessor$range['to']);
            }

            $query->addSelect(sprintf('SUM(%s) as `%s.%s`', $sum$aggregation->getName()$id));
        }
    }


    /** * {@inheritdoc} * * @return array<string, mixed> */
    protected function getArray(): array
    {
        return [
            'field' => $this->getField(),
            'ranges' => $this->getRanges(),
        ];
    }
}
$criteria,
            $exception
        );

        static::assertCount(0, iterator_to_array($exception->getErrors()));
        static::assertCount(1, $criteria->getAggregations());

        $agg = $criteria->getAggregation('range_test');

        /** @var RangeAggregation $agg */
        static::assertInstanceOf(RangeAggregation::class$agg);
        $computedRanges = $agg->getRanges();

        static::assertEquals($expectedRanges[0] + ['key' => '1-2']$computedRanges[0]);
        static::assertEquals($expectedRanges[1] + ['key' => '2-3']$computedRanges[1]);
    }
}

        }

        return $composite;
    }

    protected function parseRangeAggregation(RangeAggregation $aggregation, string $fieldName): Bucketing\RangeAggregation
    {
        return new Bucketing\RangeAggregation(
            $aggregation->getName(),
            $fieldName,
            $aggregation->getRanges()
        );
    }

    /** * @return array<string, mixed> */
    private function getCheapestPriceParameters(Context $context): array
    {
        return [
            'accessors' => $this->getCheapestPriceAccessors($context),
            'decimals' => 10 ** $context->getRounding()->getDecimals(),
            
new RangeAggregation(
                'test-range-aggregation',
                'price.gross',
                $rangesDefinition
            )
        );

        $aggregationCollection = $this->repository->aggregate($criteria$this->context);
        static::assertTrue($aggregationCollection->has('test-range-aggregation'));
        static::assertInstanceOf(RangeResult::class$aggregationCollection->get('test-range-aggregation'));

        $rangesResult = $aggregationCollection->get('test-range-aggregation')->getRanges();

        static::assertCount(\count($rangesDefinition)$rangesResult);
        foreach ($rangesResult as $key => $count) {
            static::assertArrayHasKey($key$rangesExpectedResult);
            static::assertEquals($rangesExpectedResult[$key]$count);
        }
    }
}
Home | Imprint | This part of the site doesn't use cookies.