BoolQuery example

'en' => 'english',
                'de' => 'german',
            ]
        );

        static::assertSame($productDefinition$definition->getEntityDefinition());
    }

    public function testBuildTermQueryUsingSearchQueryBuilder(): void
    {
        $searchQueryBuilder = $this->createMock(AbstractProductSearchQueryBuilder::class);
        $boolQuery = new BoolQuery();
        $boolQuery->add(new MatchQuery('name', 'test'));
        $searchQueryBuilder
            ->method('build')
            ->willReturn($boolQuery);

        $definition = new EsProductDefinition(
            $this->createMock(ProductDefinition::class),
            $this->createMock(Connection::class),
            [],
            new EventDispatcher(),
            $searchQueryBuilder,
            [
/** * @param string $term * * @return BoolQuery */
    public function buildQuery(array $fields$term)
    {
        $tokens = $this->tokenize($term);

        $combines = $this->combine($tokens);

        $bool = new BoolQuery();
        foreach ($tokens as $token) {
            foreach ($fields as $field => $priority) {
                $bool->add(new MatchQuery($field$token['boost' => $priority]), BoolQuery::SHOULD);
                $bool->add(new WildcardQuery($field, '*' . strtolower($token) . '*'), BoolQuery::SHOULD);
            }
        }

        // use combination for more precision         foreach ($combines as $token) {
            foreach ($fields as $field => $priority) {
                $bool->add(new MatchQuery($field$token['boost' => $priority * 2]), BoolQuery::SHOULD);
            }
/** * @deprecated tag:v6.6.0 - Will become abstract, implementation should implement their own `buildTermQuery` */
    public function buildTermQuery(Context $context, Criteria $criteria): BoolQuery
    {
        Feature::triggerDeprecationOrThrow(
            'ES_MULTILINGUAL_INDEX',
            'Will become abstract, implementation should implement their own `buildTermQuery`'
        );

        $bool = new BoolQuery();

        $term = (string) $criteria->getTerm();

        $queries = [
            new MatchQuery('fullTextBoosted', $term['boost' => 10]), // boosted word matches             new MatchQuery('fullText', $term['boost' => 5]), // whole word matches             new MatchQuery('fullText', $term['fuzziness' => 'auto', 'boost' => 3]), // word matches not exactly =>             new MatchPhrasePrefixQuery('fullText', $term['boost' => 1, 'slop' => 5]), // one of the words begins with: "Spachtel" => "Spachtelmasse"             new WildcardQuery('fullText', '*' . mb_strtolower($term) . '*'), // part of a word matches: "masse" => "Spachtelmasse"             new MatchQuery('fullText.ngram', $term),
        ];

        

    public function addFilter(BuilderInterface $query)
    {
        $this->addQuery($query, BoolQuery::FILTER);
    }

    /** * {@inheritdoc} */
    public function getFilters(BuilderInterface $query): BuilderInterface
    {
        $boolQuery = new BoolQuery();

        foreach ($this->getQueries()->getQueries(BoolQuery::FILTER) as $filterQuerys) {
            $boolQuery->add($filterQuerys, BoolQuery::FILTER);
        }

        return $boolQuery;
    }
}
protected function addSortings(SearchCriteria $criteria, Search $search)
    {
        foreach ($criteria->sortings as $sorting) {
            $search->addSort(
                new FieldSort($sorting['property']strtolower($sorting['direction']))
            );
        }
    }

    private function addFilters(Search $search, SearchCriteria $criteria)
    {
        $query = new BoolQuery();
        foreach ($criteria->conditions as $condition) {
            if ($condition['property'] === 'search') {
                $search->addQuery(
                    $this->searchQueryBuilder->buildQuery($this->getSearchFields()$condition['value'])
                );
                continue;
            }

            $expression = $condition['expression'] ?? '=';

            switch (strtolower($expression)) {
                
 else {
                $value = (bool) $value;
            }
        }

        switch ($criteriaPart->getOperator()) {
            case ProductAttributeCondition::OPERATOR_EQ:
                if ($type === 'string') {
                    $field .= '.raw';
                }
                if ($value === null) {
                    $filter = new BoolQuery();
                    $filter->add(new ExistsQuery($field), BoolQuery::MUST_NOT);

                    return $filter;
                }
                if (\is_array($value)) {
                    throw new RuntimeException('Invalid value for TermQuery provided');
                }

                return new TermQuery($field$value);

            case ProductAttributeCondition::OPERATOR_NEQ:
                
throw new \RuntimeException(sprintf('Filter aggregation %s contains no nested aggregation.', $aggregation->getName()));
        }

        $nested = $this->parseAggregation($aggregation->getAggregation()$definition$context);
        if ($nested === null) {
            throw new \RuntimeException(sprintf('Nested filter aggregation %s can not be parsed.', $aggregation->getName()));
        }

        // when aggregation inside the filter aggregation points to a nested object (e.g. product.properties.id) we have to add all filters         // which points to the same association to the same "nesting" level like the nested aggregation for this association         $path = $nested instanceof NestedAggregation ? $nested->getPath() : null;
        $bool = new BoolQuery();

        $filters = [];
        foreach ($aggregation->getFilter() as $filter) {
            $query = $this->parseFilter($filter$definition$definition->getEntityName()$context);

            if (!$query instanceof NestedQuery) {
                $filters[] = new Bucketing\FilterAggregation($aggregation->getName()$query);

                continue;
            }

            
new EventDispatcher(),
            $this->createMock(AbstractProductSearchQueryBuilder::class),
            $this->createMock(EsProductDefinition::class)
        );

        static::assertSame($productDefinition$definition->getEntityDefinition());
    }

    public function testBuildTermQueryUsingSearchQueryBuilder(): void
    {
        $searchQueryBuilder = $this->createMock(AbstractProductSearchQueryBuilder::class);
        $boolQuery = new BoolQuery();
        $boolQuery->add(new MatchQuery('name', 'test'));
        $searchQueryBuilder
            ->method('build')
            ->willReturn($boolQuery);

        $definition = new ElasticsearchProductDefinition(
            $this->createMock(ProductDefinition::class),
            $this->createMock(Connection::class),
            [],
            new EventDispatcher(),
            $searchQueryBuilder,
            
private readonly Connection $connection,
        private readonly EntityDefinitionQueryHelper $helper,
        private readonly EntityDefinition $productDefinition,
        private readonly AbstractTokenFilter $tokenFilter,
        private readonly Tokenizer $tokenizer,
        private readonly ElasticsearchHelper $elasticsearchHelper
    ) {
    }

    public function build(Criteria $criteria, Context $context): BoolQuery
    {
        $bool = new BoolQuery();

        $searchConfig = $this->fetchConfig($context);

        $isAndSearch = $searchConfig[0]['and_logic'] === '1';

        $tokens = $this->tokenizer->tokenize((string) $criteria->getTerm());
        $tokens = $this->tokenFilter->filter($tokens$context);

        foreach ($tokens as $token) {
            $tokenBool = new BoolQuery();

            
ShopContextInterface $context
    ) {
        $this->handleFilter($criteriaPart$criteria$search$context);
    }

    private function addQuery(SimilarProductCondition $criteriaPart, Search $search): void
    {
        $productId = $criteriaPart->getProductId();
        $productName = $criteriaPart->getProductName();
        $categories = $this->getProductCategories($productId);

        $query = new BoolQuery();

        $nameQuery = new MultiMatchQuery(['name', 'keywords']$productName['boost' => 5]);
        $categoriesQuery = new TermsQuery('categoryIds', $categories['boost' => 0.2]);

        $query->add($nameQuery, BoolQuery::SHOULD);
        $query->add($categoriesQuery, BoolQuery::MUST);

        $not = new BoolQuery();
        $not->add(new TermQuery('id', $productId), BoolQuery::MUST_NOT);

        $search->addQuery($not, BoolQuery::FILTER);
        
public function __construct(array $maxExpansions)
    {
        $this->maxExpansions = $maxExpansions;
    }

    /** * {@inheritdoc} */
    public function buildQuery(ShopContextInterface $context$term)
    {
        $boolQuery = new BoolQuery();
        $boolQuery->addParameter('minimum_should_match', 1);
        $boolQuery->add($this->getBestFieldQuery($term), BoolQuery::SHOULD);
        foreach ($this->maxExpansions as $field => $maxExpansion) {
            $boolQuery->add($this->getPhrasePrefixQuery($term$field$maxExpansion), BoolQuery::SHOULD);
        }

        return $boolQuery;
    }

    private function getBestFieldQuery(string $term): MultiMatchQuery
    {
        
$search->addQuery($query);
    }

    public function addQueries(EntityDefinition $definition, Criteria $criteria, Search $search, Context $context): void
    {
        $queries = $criteria->getQueries();
        if (empty($queries)) {
            return;
        }

        $bool = new BoolQuery();

        foreach ($queries as $query) {
            $parsed = $this->parser->parseFilter($query->getQuery()$definition$definition->getEntityName()$context);

            if ($parsed instanceof MatchQuery) {
                $score = (string) $query->getScore();

                $parsed->addParameter('boost', $score);
                $parsed->addParameter('fuzziness', '2');
            }

            
foreach ($conditions as $condition) {
            $handler = $handlerRegistry->getHandler($condition);

            if ($handler instanceof PartialConditionHandlerInterface) {
                $handler->handleFilter($condition$criteria$search$context);
            } else {
                $handler->handle($condition$criteria$search$context);
            }
        }

        $query = new BoolQuery();

        if ($search->getPostFilters()) {
            $query->add($search->getPostFilters());
        }

        if ($search->getQueries()->getQueries(BoolQuery::FILTER)) {
            foreach ($search->getQueries()->getQueries(BoolQuery::FILTER) as $filter) {
                $query->add($filter, BoolQuery::FILTER);
            }
        }
        if ($search->getQueries()) {
            
public function handlePostFilter(
        CriteriaPartInterface $criteriaPart,
        Criteria $criteria,
        Search $search,
        ShopContextInterface $context
    ) {
        $search->addPostFilter($this->getQuery($criteriaPart));
    }

    private function getQuery(CustomerGroupCondition $criteriaPart): BoolQuery
    {
        $filter = new BoolQuery();
        $filter->add(
            new TermsQuery('blockedCustomerGroupIds', $criteriaPart->getCustomerGroupIds()),
            BoolQuery::MUST_NOT
        );

        return $filter;
    }
}
Home | Imprint | This part of the site doesn't use cookies.