setTerm example

$term = mb_substr(trim($term), 0, $this->searchTermMaxLength);

        if (empty($term)) {
            throw RoutingException::missingRequestParameter('search');
        }

        // reset queries and set term to criteria.         $criteria->resetQueries();

        // elasticsearch will interpret this on demand         $criteria->setTerm($term);
    }
}
/** * @depends testIndexing */
    public function testAndSearch(IdsCollection $ids): void
    {
        $this->setSearchConfiguration(true, ['name']);
        $this->setSearchScores([]);

        $criteria = new Criteria();
        $criteria->addState(Criteria::STATE_ELASTICSEARCH_AWARE);
        $criteria->setTerm('Aerodynamic Leather');
        $criteria->addSorting(new FieldSorting('name', FieldSorting::ASCENDING));

        $result = $this->productRepository->searchIds($criteria, Context::createDefaultContext());

        /** @var string[] $resultIds */
        $resultIds = $result->getIds();

        static::assertCount(3, $resultIds, 'But got ' . $ids->getKeys($resultIds));

        static::assertSame(
            [
                
yield 'test filter' => [
            (new Criteria())->addFilter(new EqualsFilter('foo', 'bar')),
            '{"total-count-mode":0,"filter":[{"type":"equals","field":"foo","value":"bar"}]}',
        ];

        yield 'test sorting' => [
            (new Criteria())->addSorting(new FieldSorting('foo', 'bar')),
            '{"total-count-mode":0,"sort":[{"field":"foo","naturalSorting":false,"extensions":[],"order":"bar"}]}',
        ];

        yield 'test term' => [
            (new Criteria())->setTerm('foo'),
            '{"total-count-mode":0,"term":"foo"}',
        ];

        yield 'test query' => [
            (new Criteria())->addQuery(new ScoreQuery(new EqualsFilter('foo', 'bar'), 100)),
            '{"total-count-mode":0,"query":[{"score":100.0,"query":{"type":"equals","field":"foo","value":"bar"},"scoreField":null,"extensions":[]}]}',
        ];

        yield 'test aggregation' => [
            (new Criteria())->addAggregation(new CountAggregation('foo', 'bar')),
            '{"total-count-mode":0,"aggregations":[{"name":"foo","type":"count","field":"bar"}]}',
        ];
$this->createMock(Connection::class),
            [],
            new EventDispatcher(),
            $searchQueryBuilder,
            [
                'en' => 'english',
                'de' => 'german',
            ]
        );

        $criteria = new Criteria();
        $criteria->setTerm('test');
        $query = $definition->buildTermQuery(Context::createDefaultContext()$criteria);

        $queries = $query->toArray();

        static::assertEquals([
            'match' => [
                'name' => [
                    'query' => 'test',
                ],
            ],
        ]$queries);
    }
$parsedQuery = QueryStringParser::fromArray($definition$query['query'] ?? []$searchException);
                $score = $query['score'] ?? 1;
                $scoreField = $query['scoreField'] ?? null;

                $criteria->addQuery(new ScoreQuery($parsedQuery$score$scoreField));
            }
        }

        if (isset($payload['term'])) {
            $term = trim((string) $payload['term']);
            $criteria->setTerm($term);
        }

        if (isset($payload['sort'])) {
            $this->addSorting($payload$criteria$definition$searchException);
        }

        if (isset($payload['aggregations'])) {
            $this->aggregationParser->buildAggregations($definition$payload$criteria$searchException);
        }

        if (isset($payload['associations'])) {
            
->method('addOrderBy')
            ->with('_score', 'DESC');

        $builder = $this->getContainer()->get(CriteriaQueryBuilder::class);
        $builder->build($queryMock$productDefinition$criteria, Context::createDefaultContext());
    }

    public function testSortByScoreIfSearchTermIsSet(): void
    {
        $productDefinition = $this->getContainer()->get(ProductDefinition::class);
        $criteria = new Criteria();
        $criteria->setTerm('searchTerm');
        $queryMock = $this->createTestProxy(QueryBuilder::class[$this->createMock(Connection::class)]);
        $queryMock
            ->expects(static::once())
            ->method('addOrderBy')
            ->with('_score', 'DESC');

        $builder = $this->getContainer()->get(CriteriaQueryBuilder::class);
        $builder->build($queryMock$productDefinition$criteria, Context::createDefaultContext());
    }

    public function testSortByScoreAndAdditionalSorting(): void
    {
$repo = new EntityRepository(
            new ProductDefinition(),
            $reader,
            $this->createMock(VersionManager::class),
            $searcher,
            $aggregator,
            $eventDispatcher,
            $this->createMock(EntityLoadedEventFactory::class),
        );

        $criteria = new Criteria();
        $criteria->setTerm('foo');

        $repo->search($criteria, Context::createDefaultContext());

        static::assertInstanceOf(EntitySearchedEvent::class$event);
    }

    public function testSearchWithFiltersAndResult(): void
    {
        $eventDispatcher = new EventDispatcher();

        $event = null;
        
$builder = new ProductSearchQueryBuilder(
            $connection,
            $helper,
            $this->getDefinition(),
            $tokenFilter,
            new Tokenizer(2),
            $elasticsearchQueryHelper
        );

        $criteria = new Criteria();
        $criteria->setTerm('foo bla');
        $queries = $builder->build($criteria, Context::createDefaultContext());

        static::assertEmpty($queries->getQueries(BoolQuery::SHOULD));

        /** @var BoolQuery[] $tokenQueries */
        $tokenQueries = array_values($queries->getQueries(BoolQuery::MUST));

        static::assertCount(2, $tokenQueries, 'Expected 2 token queries due to token searches');

        $nameQueries = array_map(fn (BuilderInterface $query) => $query->toArray()array_values($tokenQueries[0]->getQueries(BoolQuery::SHOULD)));

        
$definition = new ElasticsearchProductDefinition(
            $this->createMock(ProductDefinition::class),
            $this->createMock(Connection::class),
            [],
            new EventDispatcher(),
            $searchQueryBuilder,
            $this->createMock(EsProductDefinition::class)
        );

        $criteria = new Criteria();
        $criteria->setTerm('test');
        $query = $definition->buildTermQuery(Context::createDefaultContext()$criteria);

        $queries = $query->toArray();

        static::assertEquals([
            'match' => [
                'name' => [
                    'query' => 'test',
                ],
            ],
        ]$queries);
    }
// check that provided criteria has other key         $route->load(new Request()$context$criteria);
    }

    public static function criteriaProvider(): \Generator
    {
        yield 'Paginated criteria' => [(new Criteria())->setOffset(1)->setLimit(20)];
        yield 'Filtered criteria' => [(new Criteria())->addFilter(new EqualsFilter('active', true))];
        yield 'Post filtered criteria' => [(new Criteria())->addPostFilter(new EqualsFilter('active', true))];
        yield 'Aggregation criteria' => [(new Criteria())->addAggregation(new StatsAggregation('name', 'name'))];
        yield 'Query criteria' => [(new Criteria())->addQuery(new ScoreQuery(new EqualsFilter('active', true), 200))];
        yield 'Term criteria' => [(new Criteria())->setTerm('test')];
        yield 'Sorted criteria' => [(new Criteria())->addSorting(new FieldSorting('active'))];
    }

    /** * @dataProvider invalidationProvider */
    public function testInvalidation(\Closure $before, \Closure $after, int $calls): void
    {
        $this->getContainer()->get('cache.object')
            ->invalidateTags([CachedSalutationRoute::ALL_TAG]);

        
// check that provided criteria has other key         $route->load($ids->get('product')new Request()$context$criteria);
    }

    public static function criteriaProvider(): \Generator
    {
        yield 'Paginated criteria' => [(new Criteria())->setOffset(1)->setLimit(20)];
        yield 'Filtered criteria' => [(new Criteria())->addFilter(new EqualsFilter('active', true))];
        yield 'Post filtered criteria' => [(new Criteria())->addPostFilter(new EqualsFilter('active', true))];
        yield 'Aggregation criteria' => [(new Criteria())->addAggregation(new StatsAggregation('name', 'name'))];
        yield 'Query criteria' => [(new Criteria())->addQuery(new ScoreQuery(new EqualsFilter('active', true), 200))];
        yield 'Term criteria' => [(new Criteria())->setTerm('test')];
        yield 'Sorted criteria' => [(new Criteria())->addSorting(new FieldSorting('active'))];
    }

    /** * @dataProvider invalidationProvider */
    public function testInvalidation(IdsCollection $ids, \Closure $before, \Closure $after, int $calls): void
    {
        $this->getContainer()->get('cache.object')
            ->invalidateTags([CachedProductReviewRoute::ALL_TAG]);

        
// check that provided criteria has other key         $route->load($ids->get('c.1')new Request()$context$criteria);
    }

    public static function criteriaProvider(): \Generator
    {
        yield 'Paginated criteria' => [(new Criteria())->setOffset(1)->setLimit(20)];
        yield 'Filtered criteria' => [(new Criteria())->addFilter(new EqualsFilter('active', true))];
        yield 'Post filtered criteria' => [(new Criteria())->addPostFilter(new EqualsFilter('active', true))];
        yield 'Aggregation criteria' => [(new Criteria())->addAggregation(new StatsAggregation('price', 'price'))];
        yield 'Query criteria' => [(new Criteria())->addQuery(new ScoreQuery(new EqualsFilter('active', true), 200))];
        yield 'Term criteria' => [(new Criteria())->setTerm('test')];
        yield 'Sorted criteria' => [(new Criteria())->addSorting(new FieldSorting('active'))];
    }

    /** * @dataProvider stateProvider */
    public function testStates(array $current, array $config): void
    {
        $ids = new IdsCollection();

        $criteria = new Criteria();

        
// check that provided criteria has other key         $route->load(new Request()$criteria$context);
    }

    public static function criteriaProvider(): \Generator
    {
        yield 'Paginated criteria' => [(new Criteria())->setOffset(1)->setLimit(20)];
        yield 'Filtered criteria' => [(new Criteria())->addFilter(new EqualsFilter('active', true))];
        yield 'Post filtered criteria' => [(new Criteria())->addPostFilter(new EqualsFilter('active', true))];
        yield 'Aggregation criteria' => [(new Criteria())->addAggregation(new StatsAggregation('name', 'name'))];
        yield 'Query criteria' => [(new Criteria())->addQuery(new ScoreQuery(new EqualsFilter('active', true), 200))];
        yield 'Term criteria' => [(new Criteria())->setTerm('test')];
        yield 'Sorted criteria' => [(new Criteria())->addSorting(new FieldSorting('active'))];
    }

    /** * @dataProvider invalidationProvider */
    public function testInvalidation(\Closure $before, \Closure $after, int $calls): void
    {
        $this->getContainer()->get('cache.object')
            ->invalidateTags([CachedCountryRoute::buildName(TestDefaults::SALES_CHANNEL)]);

        


    /** * @depends testIndexing */
    public function testEntityAggregationWithTermQuery(IdsCollection $data): void
    {
        try {
            $aggregator = $this->createEntityAggregator();

            // check simple search without any restrictions             $criteria = (new Criteria($data->prefixed('p')))->setTerm('Grouped');
            $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);

            
$clone = clone $criteria;
        $clone->resetAggregations();
        $clone->resetSorting();
        $clone->resetPostFilters();
        $clone->resetGroupFields();

        // Early resolve terms to extract score queries         if ($clone->getTerm()) {
            $pattern = $this->interpreter->interpret((string) $criteria->getTerm());
            $queries = $this->scoreBuilder->buildScoreQueries($pattern$definition$definition->getEntityName()$context);
            $clone->addQuery(...$queries);
            $clone->setTerm(null);
        }

        $scoreCriteria = clone $clone;
        $clone->resetQueries();

        $query = new QueryBuilder($this->connection);

        // If an aggregation is to be created on a to many association that is already stored as a filter.         // The association is therefore referenced twice in the query and would have to be created as a sub-join in each case.         // But since only the filters are considered, the association is referenced only once.         // In this case we add the aggregation field as path to the criteria builder and the join group builder will consider this path for the sub-join logic
Home | Imprint | This part of the site doesn't use cookies.