Skip to content

Commit 3df1b61

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 262445f commit 3df1b61

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

app/code/Magento/CatalogGraphQl/DataProvider/Product/SearchCriteriaBuilder.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ public function build(array $args, bool $includeAggregation): SearchCriteriaInte
104104
$this->addDefaultSortOrder($searchCriteria, $isSearch);
105105
}
106106

107+
$this->addEntityIdSort($searchCriteria, $isSearch);
107108
$this->addVisibilityFilter($searchCriteria, $isSearch, !empty($args['filter']));
108109

109110
$searchCriteria->setCurrentPage($args['currentPage']);
@@ -132,6 +133,25 @@ private function addVisibilityFilter(SearchCriteriaInterface $searchCriteria, bo
132133
$this->addFilter($searchCriteria, 'visibility', $visibilityIds, 'in');
133134
}
134135

136+
/**
137+
* Add sort by Entity ID
138+
*
139+
* @param SearchCriteriaInterface $searchCriteria
140+
* @param bool $isSearch
141+
*/
142+
private function addEntityIdSort(SearchCriteriaInterface $searchCriteria, bool $isSearch): void
143+
{
144+
if ($isSearch) {
145+
return;
146+
}
147+
$sortOrderArray = $searchCriteria->getSortOrders();
148+
$sortOrderArray[] = $this->sortOrderBuilder
149+
->setField('_id')
150+
->setDirection(SortOrder::SORT_DESC)
151+
->create();
152+
$searchCriteria->setSortOrders($sortOrderArray);
153+
}
154+
135155
/**
136156
* Prepare price aggregation algorithm
137157
*

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider;
99

10+
use Magento\Catalog\Api\Data\EavAttributeInterface;
1011
use Magento\Catalog\Api\Data\ProductSearchResultsInterfaceFactory;
1112
use Magento\Catalog\Model\ResourceModel\Product\Collection;
1213
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
@@ -18,6 +19,7 @@
1819
use Magento\Framework\Api\Search\SearchResultInterface;
1920
use Magento\Framework\Api\SearchCriteriaInterface;
2021
use Magento\Framework\Api\SearchResultsInterface;
22+
use Magento\Framework\Api\SortOrder;
2123
use Magento\GraphQl\Model\Query\ContextInterface;
2224

2325
/**
@@ -153,7 +155,12 @@ private function getSortOrderArray(SearchCriteriaInterface $searchCriteria)
153155
$sortOrders = $searchCriteria->getSortOrders();
154156
if (is_array($sortOrders)) {
155157
foreach ($sortOrders as $sortOrder) {
156-
$ordersArray[$sortOrder->getField()] = $sortOrder->getDirection();
158+
if ($sortOrder->getField() !== '_id') {
159+
if ($sortOrder->getField() == EavAttributeInterface::POSITION) {
160+
$sortOrder->setDirection(SortOrder::SORT_DESC);
161+
}
162+
$ordersArray[$sortOrder->getField()] = $sortOrder->getDirection();
163+
}
157164
}
158165
}
159166

dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductSearchTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ public function testFilterByMultipleProductUrlKeys()
898898
$product1 = $productRepository->get('simple');
899899
$product2 = $productRepository->get('12345');
900900
$product3 = $productRepository->get('simple-4');
901-
$filteredProducts = [$product1, $product2, $product3];
901+
$filteredProducts = [$product3, $product2, $product1];
902902
$urlKey =[];
903903
foreach ($filteredProducts as $product) {
904904
$urlKey[] = $product->getUrlKey();

0 commit comments

Comments
 (0)