FieldSort example

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

    private function getSorting(ProductNumberSorting $criteriaPart): FieldSort
    {
        return new FieldSort('number', strtolower($criteriaPart->getDirection()));
    }
}
public function handle(
        CriteriaPartInterface $criteriaPart,
        Criteria $criteria,
        Search $search,
        ShopContextInterface $context
    ) {
        $search->addSort($this->getSorting($criteriaPart));
    }

    private function getSorting(ReleaseDateSorting $criteriaPart): FieldSort
    {
        return new FieldSort('formattedCreatedAt', strtolower($criteriaPart->getDirection()),
            ['unmapped_type' => 'date']);
    }
}
public function handle(
        CriteriaPartInterface $criteriaPart,
        Criteria $criteria,
        Search $search,
        ShopContextInterface $context
    ) {
        $search->addSort($this->getSorting($criteriaPart));
    }

    private function getSorting(ProductStockSorting $criteriaPart): FieldSort
    {
        return new FieldSort('stock', strtolower($criteriaPart->getDirection())['unmapped_type' => 'keyword']);
    }
}
Criteria $criteria,
        Search $search,
        ShopContextInterface $context
    ) {
        $search->addSort($this->getSorting($criteriaPart));
    }

    private function getSorting(ProductAttributeSorting $criteriaPart): FieldSort
    {
        $field = 'attributes.core.' . $criteriaPart->getField();

        return new FieldSort($fieldstrtolower($criteriaPart->getDirection()));
    }
}
public function handle(
        CriteriaPartInterface $criteriaPart,
        Criteria $criteria,
        Search $search,
        ShopContextInterface $context
    ) {
        $search->addSort($this->getSorting($criteriaPart));
    }

    private function getSorting(ProductNameSorting $criteriaPart): FieldSort
    {
        return new FieldSort('name.raw', strtolower($criteriaPart->getDirection())['unmapped_type' => 'long']);
    }
}


        $search->addSort($this->getSorting($criteriaPart$categoryCondition));
    }

    private function getSorting(ManualSorting $criteriaPart, CategoryCondition $categoryCondition): FieldSort
    {
        $categoryId = $categoryCondition->getCategoryIds()[0];

        // Elasticsearch DSL does not support the new format         // @see: https://www.elastic.co/guide/en/elasticsearch/reference/6.8/search-request-sort.html#_nested_sorting_examples         return new FieldSort('manualSorting.position', strtolower($criteriaPart->getDirection())[
            'unmapped_type' => 'integer',
            'nested' => [
                'path' => 'manualSorting',
                'filter' => (new TermsQuery('manualSorting.category_id', [$categoryId]))->toArray(),
            ],
        ]);
    }
}
public function handle(
        CriteriaPartInterface $criteriaPart,
        Criteria $criteria,
        Search $search,
        ShopContextInterface $context
    ) {
        $search->addSort($this->getSorting($criteriaPart));
    }

    private function getSorting(SearchRankingSorting $criteriaPart): FieldSort
    {
        return new FieldSort('_score', strtolower($criteriaPart->getDirection()));
    }
}


        $parts[] = 'c_' . $currencyId;
        $parts[] = $taxState;

        return implode('.', $parts);
    }

    public function parseSorting(FieldSorting $sorting, EntityDefinition $definition, Context $context): FieldSort
    {
        if ($this->isCheapestPriceField($sorting->getField())) {
            return new FieldSort('_script', $sorting->getDirection(), null, [
                'type' => 'number',
                'script' => [
                    'id' => 'cheapest_price',
                    'params' => $this->getCheapestPriceParameters($context),
                ],
            ]);
        }

        if ($this->isCheapestPriceField($sorting->getField(), true)) {
            return new FieldSort('_script', $sorting->getDirection(), null, [
                'type' => 'number',
                

    public function handle(
        CriteriaPartInterface $criteriaPart,
        Criteria $criteria,
        Search $search,
        ShopContextInterface $context
    ) {
        $randomQuery = new FunctionScoreQuery(new MatchAllQuery());
        $randomQuery->addRandomFunction(md5(uniqid('swag', true)));

        $search->addSort(new FieldSort('_score'));
        $search->addQuery($randomQuery);
    }
}
Criteria $criteria,
        Search $search,
        ShopContextInterface $context
    ) {
        $search->addSort($this->getSorting($criteriaPart$criteria$context));
    }

    private function getSorting(PriceSorting $criteriaPart, Criteria $criteria, ShopContextInterface $context): FieldSort
    {
        $field = $this->mapper->getPriceField($criteria$context);

        return new FieldSort($fieldstrtolower($criteriaPart->getDirection())['unmapped_type' => 'double']);
    }
}
$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,
        ];

        
$this->addConditions($criteria$context$search);
        $this->addCriteriaParts($criteria$context$search$criteria->getSortings());
        $this->addCriteriaParts($criteria$context$search$criteria->getFacets());

        if ($criteria->getOffset() !== null) {
            $search->setFrom($criteria->getOffset());
        }
        if ($criteria->getLimit() !== null) {
            $search->setSize($criteria->getLimit());
        }

        $search->addSort(new FieldSort('id', 'asc'));

        return $search;
    }

    /** * @param CriteriaPartInterface[] $criteriaParts */
    private function addCriteriaParts(
        Criteria $criteria,
        ShopContextInterface $context,
        EsSearch $search,
        

    protected function getSearchFields()
    {
        return ['swag_all' => 1];
    }

    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'])
                );
public function handle(
        CriteriaPartInterface $criteriaPart,
        Criteria $criteria,
        Search $search,
        ShopContextInterface $context
    ) {
        $search->addSort($this->getSorting($criteriaPart));
    }

    private function getSorting(PopularitySorting $criteriaPart): FieldSort
    {
        return new FieldSort('sales', strtolower($criteriaPart->getDirection())['unmapped_type' => 'long']);
    }
}
Home | Imprint | This part of the site doesn't use cookies.