Skip to content

Commit 5daadae

Browse files
authored
Merge pull request #5965 from magento-performance/MC-35903
MC-35903
2 parents 00f8a8c + 14f3784 commit 5daadae

File tree

16 files changed

+164
-88
lines changed

16 files changed

+164
-88
lines changed

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

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,10 @@ public function build(array $args, bool $includeAggregation): SearchCriteriaInte
101101
}
102102

103103
if (!$searchCriteria->getSortOrders()) {
104-
$this->addDefaultSortOrder($searchCriteria, $isSearch);
104+
$this->addDefaultSortOrder($searchCriteria, $args, $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
*
@@ -179,18 +199,32 @@ private function addFilter(
179199
* Sort by relevance DESC by default
180200
*
181201
* @param SearchCriteriaInterface $searchCriteria
202+
* @param array $args
182203
* @param bool $isSearch
183204
*/
184-
private function addDefaultSortOrder(SearchCriteriaInterface $searchCriteria, $isSearch = false): void
205+
private function addDefaultSortOrder(SearchCriteriaInterface $searchCriteria, array $args, $isSearch = false): void
185206
{
186-
$sortField = $isSearch ? 'relevance' : EavAttributeInterface::POSITION;
187-
$sortDirection = $isSearch ? SortOrder::SORT_DESC : SortOrder::SORT_ASC;
188-
$defaultSortOrder = $this->sortOrderBuilder
189-
->setField($sortField)
190-
->setDirection($sortDirection)
191-
->create();
207+
$defaultSortOrder = [];
208+
if ($isSearch) {
209+
$defaultSortOrder[] = $this->sortOrderBuilder
210+
->setField('relevance')
211+
->setDirection(SortOrder::SORT_DESC)
212+
->create();
213+
} else {
214+
$categoryIdFilter = isset($args['filter']['category_id']) ? $args['filter']['category_id'] : false;
215+
if ($categoryIdFilter) {
216+
if (!is_array($categoryIdFilter[array_key_first($categoryIdFilter)])
217+
|| count($categoryIdFilter[array_key_first($categoryIdFilter)]) <= 1
218+
) {
219+
$defaultSortOrder[] = $this->sortOrderBuilder
220+
->setField(EavAttributeInterface::POSITION)
221+
->setDirection(SortOrder::SORT_ASC)
222+
->create();
223+
}
224+
}
225+
}
192226

193-
$searchCriteria->setSortOrders([$defaultSortOrder]);
227+
$searchCriteria->setSortOrders($defaultSortOrder);
194228
}
195229

196230
/**

app/code/Magento/CatalogGraphQl/Model/Category/CategoryFilter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\Exception\InputException;
1414
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1515
use Magento\Framework\GraphQl\Query\Resolver\Argument\SearchCriteria\ArgumentApplier\Filter;
16+
use Magento\Framework\GraphQl\Query\Resolver\Argument\SearchCriteria\ArgumentApplier\Sort;
1617
use Magento\Search\Model\Query;
1718
use Magento\Store\Api\Data\StoreInterface;
1819
use Magento\Store\Model\ScopeInterface;
@@ -71,6 +72,7 @@ public function getResult(array $criteria, StoreInterface $store)
7172
$categoryIds = [];
7273
$criteria[Filter::ARGUMENT_NAME] = $this->formatMatchFilters($criteria['filters'], $store);
7374
$criteria[Filter::ARGUMENT_NAME][CategoryInterface::KEY_IS_ACTIVE] = ['eq' => 1];
75+
$criteria[Sort::ARGUMENT_NAME][CategoryInterface::KEY_POSITION] = ['ASC'];
7476
$searchCriteria = $this->searchCriteriaBuilder->build('categoryList', $criteria);
7577
$pageSize = $criteria['pageSize'] ?? 20;
7678
$currentPage = $criteria['currentPage'] ?? 1;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ private function getSortOrderArray(SearchCriteriaInterface $searchCriteria)
153153
$sortOrders = $searchCriteria->getSortOrders();
154154
if (is_array($sortOrders)) {
155155
foreach ($sortOrders as $sortOrder) {
156+
// I am replacing _id with entity_id because in ElasticSearch _id is required for sorting by ID.
157+
// Where as entity_id is required when using ID as the sort in $collection->load();.
158+
// @see \Magento\CatalogGraphQl\Model\Resolver\Products\Query\Search::getResult
159+
if ($sortOrder->getField() === '_id') {
160+
$sortOrder->setField('entity_id');
161+
}
156162
$ordersArray[$sortOrder->getField()] = $sortOrder->getDirection();
157163
}
158164
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\CatalogGraphQl\Model\Resolver\Products\SearchResult;
1313
use Magento\CatalogGraphQl\Model\Resolver\Products\SearchResultFactory;
1414
use Magento\Framework\Api\Search\SearchCriteriaInterface;
15+
use Magento\Framework\Exception\InputException;
1516
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1617
use Magento\GraphQl\Model\Query\ContextInterface;
1718
use Magento\Search\Api\SearchInterface;
@@ -83,6 +84,7 @@ public function __construct(
8384
* @param ResolveInfo $info
8485
* @param ContextInterface $context
8586
* @return SearchResult
87+
* @throws InputException
8688
*/
8789
public function getResult(
8890
array $args,
@@ -103,7 +105,12 @@ public function getResult(
103105
//Address limitations of sort and pagination on search API apply original pagination from GQL query
104106
$searchCriteria->setPageSize($realPageSize);
105107
$searchCriteria->setCurrentPage($realCurrentPage);
106-
$searchResults = $this->productsProvider->getList($searchCriteria, $itemsResults, $queryFields, $context);
108+
$searchResults = $this->productsProvider->getList(
109+
$searchCriteria,
110+
$itemsResults,
111+
$queryFields,
112+
$context
113+
);
107114

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

app/code/Magento/MessageQueue/Console/StartConsumerCommand.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
7979

8080
$singleThread = $input->getOption(self::OPTION_SINGLE_THREAD);
8181

82-
if ($singleThread && $this->lockManager->isLocked(md5($consumerName))) { //phpcs:ignore
82+
if ($singleThread && !$this->lockManager->lock(md5($consumerName),0)) { //phpcs:ignore
8383
$output->writeln('<error>Consumer with the same name is running</error>');
8484
return \Magento\Framework\Console\Cli::RETURN_FAILURE;
8585
}
8686

87-
if ($singleThread) {
88-
$this->lockManager->lock(md5($consumerName)); //phpcs:ignore
89-
}
90-
9187
$this->appState->setAreaCode($areaCode ?? 'global');
9288

9389
$consumer = $this->consumerFactory->get($consumerName, $batchSize);
9490
$consumer->process($numberOfMessages);
95-
9691
if ($singleThread) {
9792
$this->lockManager->unlock(md5($consumerName)); //phpcs:ignore
9893
}
@@ -163,7 +158,7 @@ protected function configure()
163158
To specify the preferred area:
164159
165160
<comment>%command.full_name% someConsumer --area-code='adminhtml'</comment>
166-
161+
167162
To do not run multiple copies of one consumer simultaneously:
168163
169164
<comment>%command.full_name% someConsumer --single-thread'</comment>

app/code/Magento/MessageQueue/Test/Unit/Console/StartConsumerCommandTest.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ public function testExecute(
103103
$pidFilePath,
104104
$singleThread,
105105
$lockExpects,
106-
$isLockedExpects,
107106
$isLocked,
108107
$unlockExpects,
109108
$runProcessExpects,
@@ -144,14 +143,11 @@ public function testExecute(
144143
->method('get')->with($consumerName, $batchSize)->willReturn($consumer);
145144
$consumer->expects($this->exactly($runProcessExpects))->method('process')->with($numberOfMessages);
146145

147-
$this->lockManagerMock->expects($this->exactly($isLockedExpects))
148-
->method('isLocked')
149-
->with(md5($consumerName)) //phpcs:ignore
150-
->willReturn($isLocked);
151-
152146
$this->lockManagerMock->expects($this->exactly($lockExpects))
153147
->method('lock')
154-
->with(md5($consumerName)); //phpcs:ignore
148+
->with(md5($consumerName))//phpcs:ignore
149+
->willReturn($isLocked);
150+
155151
$this->lockManagerMock->expects($this->exactly($unlockExpects))
156152
->method('unlock')
157153
->with(md5($consumerName)); //phpcs:ignore
@@ -172,8 +168,7 @@ public function executeDataProvider()
172168
'pidFilePath' => null,
173169
'singleThread' => false,
174170
'lockExpects' => 0,
175-
'isLockedExpects' => 0,
176-
'isLocked' => false,
171+
'isLocked' => true,
177172
'unlockExpects' => 0,
178173
'runProcessExpects' => 1,
179174
'expectedReturn' => Cli::RETURN_SUCCESS,
@@ -182,18 +177,16 @@ public function executeDataProvider()
182177
'pidFilePath' => '/var/consumer.pid',
183178
'singleThread' => true,
184179
'lockExpects' => 1,
185-
'isLockedExpects' => 1,
186-
'isLocked' => false,
180+
'isLocked' => true,
187181
'unlockExpects' => 1,
188182
'runProcessExpects' => 1,
189183
'expectedReturn' => Cli::RETURN_SUCCESS,
190184
],
191185
[
192186
'pidFilePath' => '/var/consumer.pid',
193187
'singleThread' => true,
194-
'lockExpects' => 0,
195-
'isLockedExpects' => 1,
196-
'isLocked' => true,
188+
'lockExpects' => 1,
189+
'isLocked' => false,
197190
'unlockExpects' => 0,
198191
'runProcessExpects' => 0,
199192
'expectedReturn' => Cli::RETURN_FAILURE,

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public function testQueryChildCategoriesWithProducts()
189189
$expectedBaseCategoryProducts = [
190190
['sku' => 'simple', 'name' => 'Simple Product'],
191191
['sku' => 'simple-4', 'name' => 'Simple Product Three'],
192-
['sku' => '12345', 'name' => 'Simple Product Two']
192+
['sku' => '12345', 'name' => 'Simple Product Two'],
193193
];
194194
$this->assertCategoryProducts($baseCategory, $expectedBaseCategoryProducts);
195195
//Check base category children
@@ -648,15 +648,6 @@ public function filterMultipleCategoriesDataProvider(): array
648648
'in',
649649
'["category-1-2", "movable"]',
650650
[
651-
[
652-
'id' => '7',
653-
'name' => 'Movable',
654-
'url_key' => 'movable',
655-
'url_path' => 'movable',
656-
'children_count' => '0',
657-
'path' => '1/2/7',
658-
'position' => '3'
659-
],
660651
[
661652
'id' => '13',
662653
'name' => 'Category 1.2',
@@ -665,6 +656,15 @@ public function filterMultipleCategoriesDataProvider(): array
665656
'children_count' => '0',
666657
'path' => '1/2/3/13',
667658
'position' => '2'
659+
],
660+
[
661+
'id' => '7',
662+
'name' => 'Movable',
663+
'url_key' => 'movable',
664+
'url_path' => 'movable',
665+
'children_count' => '0',
666+
'path' => '1/2/7',
667+
'position' => '3'
668668
]
669669
]
670670
],

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public function testPaging()
155155
$lastPageQuery = sprintf($baseQuery, $page1Result['categories']['page_info']['total_pages']);
156156
$lastPageResult = $this->graphQlQuery($lastPageQuery);
157157
$this->assertCount(1, $lastPageResult['categories']['items']);
158-
$this->assertEquals('Category 1.2', $lastPageResult['categories']['items'][0]['name']);
158+
$this->assertEquals('Category 12', $lastPageResult['categories']['items'][0]['name']);
159159
}
160160

161161
/**

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public function testQueryChildCategoriesWithProducts()
215215
$this->assertEquals('Its a description of Test Category 1.2', $secondChildCategory['description']);
216216
$firstChildCategoryExpectedProducts = [
217217
['sku' => 'simple-4', 'name' => 'Simple Product Three'],
218-
['sku' => 'simple', 'name' => 'Simple Product'],
218+
['sku' => 'simple', 'name' => 'Simple Product']
219219
];
220220
$this->assertCategoryProducts($secondChildCategory, $firstChildCategoryExpectedProducts);
221221
$firstChildCategoryChildren = [];
@@ -629,15 +629,6 @@ public function filterMultipleCategoriesDataProvider(): array
629629
'in',
630630
'["category-1-2", "movable"]',
631631
[
632-
[
633-
'id' => '7',
634-
'name' => 'Movable',
635-
'url_key' => 'movable',
636-
'url_path' => 'movable',
637-
'children_count' => '0',
638-
'path' => '1/2/7',
639-
'position' => '3'
640-
],
641632
[
642633
'id' => '13',
643634
'name' => 'Category 1.2',
@@ -646,6 +637,15 @@ public function filterMultipleCategoriesDataProvider(): array
646637
'children_count' => '0',
647638
'path' => '1/2/3/13',
648639
'position' => '2'
640+
],
641+
[
642+
'id' => '7',
643+
'name' => 'Movable',
644+
'url_key' => 'movable',
645+
'url_path' => 'movable',
646+
'children_count' => '0',
647+
'path' => '1/2/7',
648+
'position' => '3'
649649
]
650650
]
651651
],

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ public function testFilterProductsByDropDownCustomAttribute()
313313
$product1 = $productRepository->get('simple');
314314
$product2 = $productRepository->get('12345');
315315
$product3 = $productRepository->get('simple-4');
316-
$filteredProducts = [$product1, $product2, $product3 ];
316+
$filteredProducts = [$product3, $product2, $product1];
317317
$countOfFilteredProducts = count($filteredProducts);
318318
$this->reIndexAndCleanCache();
319319
$response = $this->graphQlQuery($query);
@@ -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();

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public function testQueryAllFieldsSimpleProduct()
232232
special_from_date
233233
special_price
234234
special_to_date
235-
swatch_image
235+
swatch_image
236236
tier_price
237237
tier_prices
238238
{
@@ -578,8 +578,8 @@ public function testProductLinks()
578578
*/
579579
public function testProductPrices()
580580
{
581-
$firstProductSku = 'simple-249';
582-
$secondProductSku = 'simple-156';
581+
$firstProductSku = 'simple-156';
582+
$secondProductSku = 'simple-249';
583583
$query = <<<QUERY
584584
{
585585
products(filter: {price: {from: "150.0", to: "250.0"}})
@@ -1050,7 +1050,7 @@ public function testProductInNonAnchoredSubCategories()
10501050
{
10511051
$query = <<<QUERY
10521052
{
1053-
products(filter:
1053+
products(filter:
10541054
{
10551055
sku: {in:["12345"]}
10561056
}

0 commit comments

Comments
 (0)