1

How can i Get a product list order by relevance like the front office?

I want sort my search by name, price and relevance like the frontend.

The first two work perfectly but when $searchOrder is by relevance does not work properly. Always is the same result and different of frontoffice

<?php
$searchOrder  = "relevance";
$searchDirection = ($searchDir == "desc") ? SortOrder::SORT_DESC : SortOrder::SORT_ASC;

$this->_searchCriteriaBuilder->addSortOrder($searchOrder, $searchDirection);
$searchCriteria = $this->_searchCriteriaBuilder->setCurrentPage($page)->setPageSize($pageSize)->create();
$products = $this->_productRepository->getList($searchCriteria);
$products = $products->getItems();
?>
2
  • Could you be more specific on what you're asking? Commented Aug 21, 2017 at 20:31
  • Ok i have edited my question
    – Miguel
    Commented Aug 22, 2017 at 9:08

1 Answer 1

0
public function __construct(
    ...
    \Magento\Framework\Api\SortOrderBuilder $sortOrderBuilder
    ...
) {
    ...
    $this->sortOrderBuilder = $sortOrderBuilder;
    ...
}

$searchOrder  = "relevance";
$searchDirection = ($searchDir == "desc") ? SortOrder::SORT_DESC : SortOrder::SORT_ASC;
$sortOrder = $this->sortOrderBuilder->setField($searchOrder)->setDirection($searchDirection)->create();
$this->searchCriteriaBuilder->addSortOrder($sortOrder);

use above way and check it.

1
  • does not work, continue ignoring when sort by relevance
    – Miguel
    Commented Aug 22, 2017 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.