Skip to content

Commit 62ba8aa

Browse files
author
dnyomo
committed
Added a new default sort by ID when searching Elasticsearch in SearchCriteriaBuilder.php
Remove the _id from the order that is being passed to the product collection in ProductSearch.php Updated the position to be DESC passed to the product collection in ProductSearch.php Updated the ProductSearchTest.php to have the items in the attended order
1 parent 3df1b61 commit 62ba8aa

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Products/DataProvider/ProductSearch.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,15 @@ public function __construct(
8686
*
8787
* @param SearchCriteriaInterface $searchCriteria
8888
* @param SearchResultInterface $searchResult
89+
* @param array $args
8990
* @param array $attributes
9091
* @param ContextInterface|null $context
9192
* @return SearchResultsInterface
9293
*/
9394
public function getList(
9495
SearchCriteriaInterface $searchCriteria,
9596
SearchResultInterface $searchResult,
97+
array $args,
9698
array $attributes = [],
9799
ContextInterface $context = null
98100
): SearchResultsInterface {
@@ -105,7 +107,7 @@ public function getList(
105107
$this->getSearchResultsApplier(
106108
$searchResult,
107109
$collection,
108-
$this->getSortOrderArray($searchCriteriaForCollection)
110+
$this->getSortOrderArray($searchCriteriaForCollection, $args)
109111
)->apply();
110112

111113
$this->collectionPreProcessor->process($collection, $searchCriteriaForCollection, $attributes, $context);
@@ -147,17 +149,22 @@ private function getSearchResultsApplier(
147149
* E.g. ['field1' => 'DESC', 'field2' => 'ASC", ...]
148150
*
149151
* @param SearchCriteriaInterface $searchCriteria
152+
* @param array $args
150153
* @return array
151154
*/
152-
private function getSortOrderArray(SearchCriteriaInterface $searchCriteria)
155+
private function getSortOrderArray(SearchCriteriaInterface $searchCriteria, $args)
153156
{
154157
$ordersArray = [];
155158
$sortOrders = $searchCriteria->getSortOrders();
156159
if (is_array($sortOrders)) {
157160
foreach ($sortOrders as $sortOrder) {
158161
if ($sortOrder->getField() !== '_id') {
159162
if ($sortOrder->getField() == EavAttributeInterface::POSITION) {
160-
$sortOrder->setDirection(SortOrder::SORT_DESC);
163+
if (isset($args['sort'][EavAttributeInterface::POSITION])) {
164+
$sortOrder->setDirection($args['sort'][EavAttributeInterface::POSITION]);
165+
} else {
166+
$sortOrder->setDirection(SortOrder::SORT_DESC);
167+
}
161168
}
162169
$ordersArray[$sortOrder->getField()] = $sortOrder->getDirection();
163170
}

app/code/Magento/CatalogGraphQl/Model/Resolver/Products/Query/Search.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function getResult(
103103
//Address limitations of sort and pagination on search API apply original pagination from GQL query
104104
$searchCriteria->setPageSize($realPageSize);
105105
$searchCriteria->setCurrentPage($realCurrentPage);
106-
$searchResults = $this->productsProvider->getList($searchCriteria, $itemsResults, $queryFields, $context);
106+
$searchResults = $this->productsProvider->getList($searchCriteria, $itemsResults, $args, $queryFields, $context);
107107

108108
$totalPages = $realPageSize ? ((int)ceil($searchResults->getTotalCount() / $realPageSize)) : 0;
109109

0 commit comments

Comments
 (0)