RangeQuery example

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

    private function getQuery(ShopContextInterface $context): RangeQuery
    {
        return new RangeQuery(
            $this->getPseudoPriceField($context),
            ['gt' => 0]
        );
    }

    private function getPseudoPriceField(ShopContextInterface $context): string
    {
        $key = $context->getCurrentCustomerGroup()->getKey();
        $currency = $context->getCurrency()->getId();

        return 'calculatedPrices.' . $key . '_' . $currency . '.calculatedPseudoPrice';
    }
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);

                return $filter;

            case ProductAttributeCondition::OPERATOR_LT:
                return new RangeQuery($field['lt' => $value]);

            case ProductAttributeCondition::OPERATOR_LTE:
                return new RangeQuery($field['lte' => $value]);

            case ProductAttributeCondition::OPERATOR_BETWEEN:
                if (!\is_array($value)) {
                    throw new RuntimeException('Invalid value for RangeQuery provided');
                }

                return new RangeQuery($field['gte' => $value['min'], 'lte' => $value['max']]);

            
'accessors' => $this->getCheapestPriceAccessors($context, true)]
                ),
            ]);
        }

        $accessor = $this->buildAccessor($definition$filter->getField()$context);

        if ($this->keyValueStorage->get(ElasticsearchIndexer::ENABLE_MULTILINGUAL_INDEX_KEY, false)) {
            $field = $this->getField($definition$filter->getField());

            $value = $this->parseValue($definition$filter$filter->getParameters());
            $query = new RangeQuery($accessor$value);

            if ($field instanceof TranslatedField) {
                $query = new DisMaxQuery();
                foreach ($context->getLanguageIdChain() as $languageId) {
                    $fieldName = $this->getTranslatedFieldName($accessor$languageId);
                    $query->addQuery(new RangeQuery($fieldName$value));
                }
            }

            return $this->createNestedQuery(
                $query,
                
public function handlePostFilter(
        CriteriaPartInterface $criteriaPart,
        Criteria $criteria,
        Search $search,
        ShopContextInterface $context
    ) {
        $search->addPostFilter($this->getQuery($criteriaPart));
    }

    private function getQuery(LastProductIdCondition $criteriaPart): RangeQuery
    {
        return new RangeQuery('id', [
            'gt' => $criteriaPart->getLastId(),
        ]);
    }
}
private function getQuery(PriceCondition $criteriaPart, Criteria $criteria, ShopContextInterface $context): RangeQuery
    {
        $range = [];
        if ($criteriaPart->getMinPrice()) {
            $range['gte'] = $criteriaPart->getMinPrice();
        }
        if ($criteriaPart->getMaxPrice()) {
            $range['lte'] = $criteriaPart->getMaxPrice();
        }

        return new RangeQuery($this->mapper->getPriceField($criteria$context)$range);
    }
}
$search->addPostFilter($this->getQuery());
    }

    private function getQuery(): RangeQuery
    {
        $dayLimit = (int) $this->config->get('markAsNew');
        $timestamp = strtotime('-' . $dayLimit . ' days');
        if ($timestamp === false) {
            throw new RuntimeException(sprintf('Could not convert "-%s days" into a timestamp', $dayLimit));
        }

        return new RangeQuery('formattedCreatedAt', [
            'gte' => date('Y-m-d', $timestamp),
            'lte' => date('Y-m-d'),
        ]);
    }
}
private function getQuery(LengthCondition $criteriaPart): RangeQuery
    {
        $range = [];
        if ($criteriaPart->getMinLength()) {
            $range['gte'] = $criteriaPart->getMinLength();
        }
        if ($criteriaPart->getMaxLength()) {
            $range['lte'] = $criteriaPart->getMaxLength();
        }

        return new RangeQuery('length', $range);
    }
}
private function getQuery(WeightCondition $criteriaPart): RangeQuery
    {
        $range = [];
        if ($criteriaPart->getMinWeight()) {
            $range['gte'] = $criteriaPart->getMinWeight();
        }
        if ($criteriaPart->getMaxWeight()) {
            $range['lte'] = $criteriaPart->getMaxWeight();
        }

        return new RangeQuery('weight', $range);
    }
}
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 '<=':
                    $query->add(
                        new RangeQuery($condition['property'][RangeQuery::LTE => $condition['value']])
                    );
                    break;

                case '<':
                    $query->add(
                        
public function handlePostFilter(
        CriteriaPartInterface $criteriaPart,
        Criteria $criteria,
        Search $search,
        ShopContextInterface $context
    ) {
        $search->addPostFilter($this->getQuery($criteriaPart));
    }

    private function getQuery(VoteAverageCondition $criteriaPart): RangeQuery
    {
        return new RangeQuery('voteAverage.average', [
            'gte' => $criteriaPart->getAverage() * 2,
        ]);
    }
}
private function getQuery(HeightCondition $criteriaPart): RangeQuery
    {
        $range = [];
        if ($criteriaPart->getMinHeight()) {
            $range['gte'] = $criteriaPart->getMinHeight();
        }
        if ($criteriaPart->getMaxHeight()) {
            $range['lte'] = $criteriaPart->getMaxHeight();
        }

        return new RangeQuery('height', $range);
    }
}
private function getQuery(WidthCondition $criteriaPart): RangeQuery
    {
        $range = [];
        if ($criteriaPart->getMinWidth()) {
            $range['gte'] = $criteriaPart->getMinWidth();
        }
        if ($criteriaPart->getMaxWidth()) {
            $range['lte'] = $criteriaPart->getMaxWidth();
        }

        return new RangeQuery('width', $range);
    }
}
public function handlePostFilter(
        CriteriaPartInterface $criteriaPart,
        Criteria $criteria,
        Search $search,
        ShopContextInterface $context
    ) {
        $search->addPostFilter($this->getQuery($criteriaPart));
    }

    private function getQuery(SalesCondition $criteriaPart): RangeQuery
    {
        return new RangeQuery('sales', ['gt' => $criteriaPart->getMinSales()]);
    }
}
$this->getQuery($criteriaPart)
        );
    }

    private function getQuery(CreateDateCondition $criteriaPart): RangeQuery
    {
        $date = new DateTime();
        $intervalSpec = 'P' . $criteriaPart->getDays() . 'D';
        $interval = new DateInterval($intervalSpec);
        $date->sub($interval);

        return new RangeQuery('formattedCreatedAt', [
            'gte' => $date->format('Y-m-d'),
        ]);
    }
}
private function getQuery(ReleaseDateCondition $criteriaPart): RangeQuery
    {
        $date = new DateTime();
        $intervalSpec = 'P' . $criteriaPart->getDays() . 'D';
        $interval = new DateInterval($intervalSpec);
        $dateNow = new DateTime();

        switch ($criteriaPart->getDirection()) {
            case ReleaseDateCondition::DIRECTION_FUTURE:
                $date->add($interval);

                return new RangeQuery('formattedReleaseDate', [
                    'lte' => $date->format('Y-m-d'),
                    'gt' => $dateNow->format('Y-m-d'),
                ]);

            case ReleaseDateCondition::DIRECTION_PAST:
                $date->sub($interval);

                return new RangeQuery('formattedReleaseDate', [
                    'gte' => $date->format('Y-m-d'),
                    'lte' => $dateNow->format('Y-m-d'),
                ]);

            
Home | Imprint | This part of the site doesn't use cookies.