TermQuery example

Search $search,
        ShopContextInterface $context
    ) {
        $this->handle($criteria$search);
    }

    private function handle(Criteria $criteria, Search $search): void
    {
        $groupBy = $this->buildGroupBy($criteria);

        if ($groupBy) {
            $search->addPostFilter(new TermQuery($groupBy, true));

            return;
        }

        $search->addPostFilter(
            new TermQuery('hasStock', true)
        );
    }

    private function buildGroupBy(Criteria $criteria): ?string
    {
        
foreach ($context->getLanguageIdChain() as $languageId) {
                        $query->add(new ExistsQuery(sprintf('%s.%s', $fieldName$languageId)), BoolQuery::MUST_NOT);
                    }
                } else {
                    $query->add(new ExistsQuery($fieldName), BoolQuery::MUST_NOT);
                }

                return $this->createNestedQuery($query$definition$filter->getField());
            }

            $value = $this->parseValue($definition$filter$filter->getValue());
            $query = new TermQuery($fieldName$value);

            if ($field instanceof TranslatedField) {
                $multiMatchFields = [];

                foreach ($context->getLanguageIdChain() as $languageId) {
                    $multiMatchFields[] = $this->getTranslatedFieldName($fieldName$languageId);
                }

                $query = new MultiMatchQuery($multiMatchFields$value[
                    'type' => 'best_fields',
                ]);
            }
public function handlePostFilter(
        CriteriaPartInterface $criteriaPart,
        Criteria $criteria,
        Search $search,
        ShopContextInterface $context
    ) {
        $search->addPostFilter($this->getQuery());
    }

    private function getQuery(): TermQuery
    {
        return new TermQuery('closeouts', true);
    }
}
public function handlePostFilter(
        CriteriaPartInterface $criteriaPart,
        Criteria $criteria,
        Search $search,
        ShopContextInterface $context
    ) {
        $search->addPostFilter($this->getQuery());
    }

    private function getQuery(): TermQuery
    {
        return new TermQuery('hasAvailableVariant', true);
    }
}

                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:
                if ($value === null) {
                    return new ExistsQuery($field);
                }
                if (\is_array($value)) {
                    throw new RuntimeException('Invalid value for TermQuery provided');
                }
                $filter = new BoolQuery();
                $filter->add(new TermQuery($field$value), BoolQuery::MUST_NOT);

                

    public function handle(
        CriteriaPartInterface $criteriaPart,
        Criteria $criteria,
        Search $search,
        ShopContextInterface $context
    ) {
        $aggregation = new ValueCountAggregation('shipping_free_count');
        $aggregation->setField('shippingFree');

        $filterAgg = new FilterAggregation('shipping_free_filter');
        $filterAgg->setFilter(new TermQuery('shippingFree', true));
        $filterAgg->addAggregation($aggregation);

        $search->addAggregation($filterAgg);
    }

    /** * {@inheritdoc} */
    public function hydrate(
        array $elasticResult,
        ProductNumberSearchResult $result,
        
public function handlePostFilter(
        CriteriaPartInterface $criteriaPart,
        Criteria $criteria,
        Search $search,
        ShopContextInterface $context
    ) {
        $search->addPostFilter($this->getQuery());
    }

    private function getQuery(): TermQuery
    {
        return new TermQuery('isMainVariant', true);
    }
}
if (!\is_array($value)) {
                        $value = [$value];
                    }
                    $query->add(
                        new TermsQuery($condition['property']$value),
                        BoolQuery::MUST
                    );
                    break;

                case '=':
                    $query->add(
                        new TermQuery($condition['property']$condition['value']),
                        BoolQuery::MUST
                    );
                    break;

                case '>=':
                    $query->add(
                        new RangeQuery($condition['property'][RangeQuery::GTE => $condition['value']])
                    );
                    break;

                case '<=':
                    
$data = $elasticResult['aggregations']['properties']['buckets'];
        $ids = array_column($data, 'key');

        if (empty($ids)) {
            return;
        }

        $groupIds = $this->getGroupIds($ids);

        $search = new EsSearch();
        $search->addQuery(new IdsQuery($groupIds), BoolQuery::FILTER);
        $search->addQuery(new TermQuery('filterable', true), BoolQuery::FILTER);
        $search->addSort(new FieldSort('name', 'asc'));
        $search->setFrom(0);
        $search->setSize(self::AGGREGATION_SIZE);

        $index = $this->indexFactory->createShopIndex($context->getShop(), PropertyMapping::TYPE);

        $arguments = [
            'index' => $index->getName(),
            'body' => $search->toArray(),
            'rest_total_hits_as_int' => true,
            'track_total_hits' => true,
        ];

    public function handle(
        CriteriaPartInterface $criteriaPart,
        Criteria $criteria,
        Search $search,
        ShopContextInterface $context
    ) {
        $aggregation = new ValueCountAggregation('has_available_variant_count');
        $aggregation->setField('hasAvailableVariant');

        $filter = new FilterAggregation('has_available_variant_filter');
        $filter->setFilter(new TermQuery('hasAvailableVariant', true));
        $filter->addAggregation($aggregation);

        $search->addAggregation($filter);
    }

    /** * {@inheritdoc} */
    public function hydrate(
        array $elasticResult,
        ProductNumberSearchResult $result,
        
Search $search,
        ShopContextInterface $context
    ) {
        $this->handle($criteriaPart$criteria$search);
    }

    private function handle(VariantCondition $criteriaPart, Criteria $criteria, Search $search): void
    {
        $groupBy = $this->buildGroupBy($criteria);

        if ($groupBy) {
            $search->addPostFilter(new TermQuery($groupBy, true));

            $search->addPostFilter(
                new TermsQuery(
                    'filterConfiguration.options.id',
                    $criteriaPart->getOptionIds()
                )
            );

            return;
        }

        
/** * {@inheritdoc} */
    public function handleFilter(
        CriteriaPartInterface $criteriaPart,
        Criteria $criteria,
        Search $search,
        ShopContextInterface $context
    ) {
        $search->addQuery(
            new TermQuery('shippingFree', true),
            BoolQuery::FILTER
        );
    }

    /** * {@inheritdoc} */
    public function handlePostFilter(
        CriteriaPartInterface $criteriaPart,
        Criteria $criteria,
        Search $search,
        
$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);
        $search->addQuery($query);
    }

    /** * @return int[] */
    private function getProductCategories(int $productId): array
    {
        return $this->connection->createQueryBuilder()
            
Home | Imprint | This part of the site doesn't use cookies.