getTerm example

/** * @dataProvider cases * * @param list<string> $expected */
    public function testMatching(string $term, array $expected): void
    {
        $context = Context::createDefaultContext();

        $matches = $this->interpreter->interpret($term$context);

        $keywords = array_map(fn (SearchTerm $term) => $term->getTerm()$matches->getTerms());

        sort($expected);
        sort($keywords);
        static::assertEquals($expected$keywords);
    }

    /** * @dataProvider casesWithTokenFilter * * @param list<string> $expected */
    
$mockElasticsearchHelper = $this->createMock(ElasticsearchHelper::class);
        $mockElasticsearchHelper->expects(static::once())->method('allowSearch')->with($productDefinition$context$criteria)->willReturn(true);

        $searchBuilder = new ProductSearchBuilder(
            $mockProductSearchBuilder,
            $mockElasticsearchHelper,
            $productDefinition
        );

        $searchBuilder->build($request$criteria$mockSalesChannelContext);

        static::assertNotEquals('array', $criteria->getTerm());
        static::assertEquals($expected$criteria->getTerm());
    }

    /** * @return iterable<string, array<int, string|array<string>>> */
    public static function providerQueries(): iterable
    {
        yield 'search is array' => [
            [
                'Word 1',
                

        $query = $this->helper->getBaseQuery($query$definition$context);

        if ($definition->isInheritanceAware() && $context->considerInheritance()) {
            $parent = $definition->getFields()->get('parent');

            if ($parent) {
                $this->helper->resolveField($parent$definition$definition->getEntityName()$query$context);
            }
        }

        if ($criteria->getTerm()) {
            $pattern = $this->interpreter->interpret((string) $criteria->getTerm());
            $queries = $this->scoreBuilder->buildScoreQueries($pattern$definition$definition->getEntityName()$context);
            $criteria->addQuery(...$queries);
        }

        $filters = $this->groupFilters($definition$criteria$paths);

        $this->criteriaPartResolver->resolve($filters$definition$query$context);

        $this->criteriaPartResolver->resolve($criteria->getQueries()$definition$query$context);

        
return;
        }

        $condition = $criteria->getCondition('search');
        if (!$condition instanceof SearchTermCondition) {
            return;
        }

        $now = new DateTime();
        $this->connection->insert('s_statistics_search', [
            'datum' => $now->format('Y-m-d H:i:s'),
            'searchterm' => $condition->getTerm(),
            'results' => $result->getTotalCount(),
            'shop_id' => $shop->getId(),
        ]);
    }
}
'total-count-mode' => $criteria->getTotalCountMode(),
        ];

        if ($criteria->getLimit()) {
            $array['limit'] = $criteria->getLimit();
        }

        if ($criteria->getOffset()) {
            $array['page'] = ($criteria->getOffset() / $criteria->getLimit()) + 1;
        }

        if ($criteria->getTerm()) {
            $array['term'] = $criteria->getTerm();
        }

        if ($criteria->getIncludes()) {
            $array['includes'] = $criteria->getIncludes();
        }

        if (\count($criteria->getIds())) {
            $array['ids'] = $criteria->getIds();
        }

        
public function handlePostFilter(
        CriteriaPartInterface $criteriaPart,
        Criteria $criteria,
        Search $search,
        ShopContextInterface $context
    ) {
        $this->handleFilter($criteriaPart$criteria$search$context);
    }

    private function getQuery(SearchTermCondition $criteriaPart, ShopContextInterface $context): BoolQuery
    {
        return $this->queryBuilder->buildQuery($context$criteriaPart->getTerm());
    }
}
$term = trim($term);
        if (empty($term)) {
            throw RoutingException::missingRequestParameter('search');
        }

        $pattern = $this->interpreter->interpret($term$context->getContext());

        foreach ($pattern->getTerms() as $searchTerm) {
            $criteria->addQuery(
                new ScoreQuery(
                    new EqualsFilter('product.searchKeywords.keyword', $searchTerm->getTerm()),
                    $searchTerm->getScore(),
                    'product.searchKeywords.ranking'
                )
            );
        }
        $criteria->addQuery(
            new ScoreQuery(
                new ContainsFilter('product.searchKeywords.keyword', $pattern->getOriginal()->getTerm()),
                $pattern->getOriginal()->getScore(),
                'product.searchKeywords.ranking'
            )
        );

#[Package('core')] class RepositorySearchDetector
{
    public static function isSearchRequired(EntityDefinition $definition, Criteria $criteria): bool
    {
        // total counts can only be fetched by entity searcher         if ($criteria->getTotalCountMode() !== Criteria::TOTAL_COUNT_MODE_NONE) {
            return true;
        }

        if ($criteria->getTerm()) {
            return true;
        }

        // paginated lists are handled by entity searcher         if ($criteria->getOffset() !== null || $criteria->getLimit() !== null) {
            return true;
        }

        // group by is only supported by entity searcher         if (\count($criteria->getGroupFields())) {
            return true;
        }

    private function buildQueryFromKeywords($keywords$tables)
    {
        $keywordSelection = [];
        foreach ($keywords as $match) {
            $keywordSelection[] = 'SELECT ' . $match->getRelevance() . ' as relevance, ' . $this->connection->quote($match->getTerm()) . ' as term, ' . $match->getId() . ' as keywordID';
        }
        $keywordSelection = implode("\n UNION ALL ", $keywordSelection);

        $tablesSql = [];
        foreach ($tables as $table) {
            $query = $this->connection->createQueryBuilder();
            $alias = 'st' . $table['tableID'];

            $query->select(['MAX(sf.relevance * sm.relevance) as relevance', 'sm.keywordID', 'term']);
            $query->from('(' . $keywordSelection . ')', 'sm');
            $query->innerJoin('sm', 's_search_index', 'si', 'sm.keywordID = si.keywordID');
            
new MultiFilter(MultiFilter::CONNECTION_AND, $postFilters),
            $definition,
            $definition->getEntityName(),
            $context
        );

        $search->addPostFilter($query, BoolQuery::FILTER);
    }

    public function addTerm(Criteria $criteria, Search $search, Context $context, EntityDefinition $definition): void
    {
        if (!$criteria->getTerm()) {
            return;
        }

        $esDefinition = $this->registry->get($definition->getEntityName());

        if (!$esDefinition) {
            throw new UnsupportedElasticsearchDefinitionException($definition->getEntityName());
        }

        $query = $esDefinition->buildTermQuery($context$criteria);

        

    public function generateCondition(
        ConditionInterface $condition,
        QueryBuilder $query,
        ShopContextInterface $context
    ) {
        $this->addCondition($condition$query);
    }

    private function addCondition(SearchTermCondition $condition, QueryBuilder $query): void
    {
        $searchQuery = $this->searchTermQueryBuilder->buildQuery($condition->getTerm());

        // no matching products found by the search query builder.         // add condition that the result contains no product.         if ($searchQuery === null) {
            $query->andWhere('0 = 1');

            return;
        }

        $queryString = $searchQuery->getSQL();

        
$queries = [];
        foreach ($fields as $field) {
            /** @var SearchRanking|null $flag */
            $flag = $field->getFlag(SearchRanking::class);

            $ranking = $multiplier;
            if ($flag) {
                $ranking = $flag->getRanking() * $multiplier;
            }

            if ($field instanceof DateTimeField) {
                if (!$this->validateDateFormat(Defaults::STORAGE_DATE_FORMAT, $term->getOriginal()->getTerm())) {
                    continue;
                }
            }

            $select = $root . '.' . $field->getPropertyName();

            if ($field instanceof ManyToManyAssociationField) {
                $queries = array_merge(
                    $queries,
                    $this->buildScoreQueries($term$field->getToManyReferenceDefinition()$select$context$ranking)
                );

                

        if (empty($this->getIds())) {
            return false;
        }

        // manual sorting provided         if (!empty($this->getSorting())) {
            return false;
        }

        // result will be sorted by interpreted search term and the calculated ranking         if (!empty($this->getTerm())) {
            return false;
        }

        // result will be sorted by calculated ranking         if (!empty($this->getQueries())) {
            return false;
        }

        return true;
    }

    
$context->getTaxState(),
            $context->getItemRounding(),
            $ruleIds,
        ], \JSON_THROW_ON_ERROR));
    }

    public function getCriteriaHash(Criteria $criteria): string
    {
        return md5((string) json_encode([
            $criteria->getIds(),
            $criteria->getFilters(),
            $criteria->getTerm(),
            $criteria->getPostFilters(),
            $criteria->getQueries(),
            $criteria->getSorting(),
            $criteria->getLimit(),
            $criteria->getOffset() ?? 0,
            $criteria->getTotalCountMode(),
            $criteria->getGroupFields(),
            $criteria->getAggregations(),
            $criteria->getAssociations(),
        ], \JSON_THROW_ON_ERROR));
    }
}

    public function getTokenTerms(): array
    {
        return $this->tokenTerms;
    }

    /** * @return list<string> */
    public function getAllTerms(): array
    {
        $terms = [$this->original->getTerm()];
        foreach ($this->terms as $term) {
            $terms[] = $term->getTerm();
        }

        return $terms;
    }

    private function getBooleanClauseMapping(bool $booleanClauseAnd): string
    {
        return $booleanClauseAnd ? self::BOOLEAN_CLAUSE_AND : self::BOOLEAN_CLAUSE_OR;
    }
}
Home | Imprint | This part of the site doesn't use cookies.