EntityAggregation example

/** * @depends testIndexing */
    public function testEntityAggregation(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 EntityAggregation('manufacturers', 'product.manufacturerId', ProductManufacturerDefinition::ENTITY_NAME));

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

            static::assertCount(1, $aggregations);

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

            $result = $aggregations->get('manufacturers');
            static::assertInstanceOf(EntityResult::class$result);

            static::assertCount(3, $result->getEntities());

            
use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\EntityAggregation;

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

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

    public function testClone(): void
    {
return null;
                }

                return new RangeAggregation($name(string) $field$aggregation['ranges']);
            case 'entity':
                if (!isset($aggregation['definition'])) {
                    $exceptions->add(new InvalidAggregationQueryException('The aggregation should contain a "definition".'), '/aggregations/' . $index . '/' . $type . '/field');

                    return null;
                }

                return new EntityAggregation($name$field$aggregation['definition']);

            case 'filter':
                if (empty($aggregation['filter'])) {
                    $exceptions->add(new InvalidAggregationQueryException('The aggregation should contain an array of filters in property "filter".'), '/aggregations/' . $index . '/' . $type . '/field');

                    return null;
                }
                if (empty($aggregation['aggregation'])) {
                    $exceptions->add(new InvalidAggregationQueryException('The aggregation should contain an array of filters in property "filter".'), '/aggregations/' . $index . '/' . $type . '/field');

                    return null;
                }


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

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

        $criteria->addAggregation(
            new EntityAggregation('manufacturers', 'product.manufacturerId', 'product_manufacturer')
        );

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

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

        $manufacturers = $result->get('manufacturers');
        static::assertInstanceOf(EntityResult::class$manufacturers);

        static::assertCount(3, $manufacturers->getEntities());
        static::assertInstanceOf(ProductManufacturerCollection::class$manufacturers->getEntities());
        
public function create(Request $request, SalesChannelContext $context): ?Filter
    {
        if (!$request->request->get('manufacturer-filter', true)) {
            return null;
        }

        $ids = $this->getManufacturerIds($request);

        return new Filter(
            'manufacturer',
            !empty($ids),
            [new EntityAggregation('manufacturer', 'product.manufacturerId', 'product_manufacturer')],
            new EqualsAnyFilter('product.manufacturerId', $ids),
            $ids
        );
    }

    /** * @return list<string> */
    private function getManufacturerIds(Request $request): array
    {
        $ids = $request->query->get('manufacturer', '');
        
Home | Imprint | This part of the site doesn't use cookies.