Skip to content

Commit 9a4b9fe

Browse files
committed
ACP2E-3892: [Mainline] Unpopulated pages are cached due to search engine errors
1 parent 2a09bb5 commit 9a4b9fe

File tree

5 files changed

+99
-101
lines changed

5 files changed

+99
-101
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Category/Products.php

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ class Products implements ResolverInterface
2222
/**
2323
* @var ProductQueryInterface
2424
*/
25-
private ProductQueryInterface $searchQuery;
25+
private $searchQuery;
2626

2727
/**
2828
* @var SearchCriteriaBuilder
2929
*/
30-
private SearchCriteriaBuilder $searchApiCriteriaBuilder;
30+
private $searchApiCriteriaBuilder;
3131

3232
/**
3333
* @param ProductQueryInterface $searchQuery
@@ -63,47 +63,34 @@ public function resolve(
6363
'eq' => $value['id']
6464
]
6565
];
66-
try {
67-
$searchResult = $this->searchQuery->getResult($args, $info, $context);
66+
$searchResult = $this->searchQuery->getResult($args, $info, $context);
6867

69-
//possible division by 0
70-
if ($searchResult->getPageSize()) {
71-
$maxPages = ceil($searchResult->getTotalCount() / $searchResult->getPageSize());
72-
} else {
73-
$maxPages = 0;
74-
}
75-
76-
$currentPage = $searchResult->getCurrentPage();
77-
if ($searchResult->getCurrentPage() > $maxPages && $searchResult->getTotalCount() > 0) {
78-
$currentPage = new GraphQlInputException(
79-
__(
80-
'currentPage value %1 specified is greater than the number of pages available.',
81-
[$maxPages]
82-
)
83-
);
84-
}
68+
//possible division by 0
69+
if ($searchResult->getPageSize()) {
70+
$maxPages = ceil($searchResult->getTotalCount() / $searchResult->getPageSize());
71+
} else {
72+
$maxPages = 0;
73+
}
8574

86-
$data = [
87-
'total_count' => $searchResult->getTotalCount(),
88-
'items' => $searchResult->getProductsSearchResult(),
89-
'page_info' => [
90-
'page_size' => $searchResult->getPageSize(),
91-
'current_page' => $currentPage,
92-
'total_pages' => $maxPages
93-
]
94-
];
95-
} catch (\Throwable) {
96-
$data = [
97-
'total_count' => 0,
98-
'items' => [],
99-
'page_info' => [
100-
'page_size' => 0,
101-
'current_page' => 0,
102-
'total_pages' => 0
103-
]
104-
];
75+
$currentPage = $searchResult->getCurrentPage();
76+
if ($searchResult->getCurrentPage() > $maxPages && $searchResult->getTotalCount() > 0) {
77+
$currentPage = new GraphQlInputException(
78+
__(
79+
'currentPage value %1 specified is greater than the number of pages available.',
80+
[$maxPages]
81+
)
82+
);
10583
}
10684

85+
$data = [
86+
'total_count' => $searchResult->getTotalCount(),
87+
'items' => $searchResult->getProductsSearchResult(),
88+
'page_info' => [
89+
'page_size' => $searchResult->getPageSize(),
90+
'current_page' => $currentPage,
91+
'total_pages' => $maxPages
92+
]
93+
];
10794
return $data;
10895
}
10996
}

app/code/Magento/CatalogGraphQl/Model/Resolver/Layer/DataProvider/Filters.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
1111
use Magento\CatalogGraphQl\Model\Resolver\Layer\FiltersProvider;
1212
use Magento\Catalog\Model\Layer\Filter\Item;
13+
use Magento\Framework\Exception\LocalizedException;
1314

1415
/**
1516
* Layered navigation filters data provider.
@@ -52,7 +53,7 @@ public function getData(string $layerType, ?array $attributesToFilter = null) :
5253
$filtersData = [];
5354
/** @var AbstractFilter $filter */
5455
foreach ($this->filtersProvider->getFilters($layerType) as $filter) {
55-
if ($this->isNeedToAddFilter($filter, $attributesToFilter)) {
56+
if ($this->isNeedToAddFilter($filter, $attributesToFilter)) {//here
5657
$filterGroup = [
5758
'name' => (string)$filter->getName(),
5859
'filter_items_count' => $filter->getItemsCount(),
@@ -76,11 +77,11 @@ public function getData(string $layerType, ?array $attributesToFilter = null) :
7677
* Check for adding filter to the list
7778
*
7879
* @param AbstractFilter $filter
79-
* @param array $attributesToFilter
80+
* @param array|null $attributesToFilter
8081
* @return bool
81-
* @throws \Magento\Framework\Exception\LocalizedException
82+
* @throws LocalizedException
8283
*/
83-
private function isNeedToAddFilter(AbstractFilter $filter, array $attributesToFilter): bool
84+
private function isNeedToAddFilter(AbstractFilter $filter, ?array $attributesToFilter): bool
8485
{
8586
if ($attributesToFilter === null) {
8687
$result = (bool)$filter->getItemsCount();

app/code/Magento/CatalogGraphQl/Model/Resolver/LayerFilters.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function resolve(
5757
private function prepareAttributesResults(array $value): ?array
5858
{
5959
$attributes = [];
60-
if (!empty($value['search_result'])) {
60+
if (!empty($value['search_result']) && $value['search_result']->getSearchAggregation()) {
6161
$buckets = $value['search_result']->getSearchAggregation()->getBuckets();
6262
foreach ($buckets as $bucket) {
6363
if (!empty($bucket->getValues())) {

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

Lines changed: 63 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

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

10+
use Magento\AdvancedSearch\Model\Client\ClientException;
1011
use Magento\CatalogGraphQl\DataProvider\Product\SearchCriteriaBuilder;
1112
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ProductSearch;
1213
use Magento\CatalogGraphQl\Model\Resolver\Products\Query\Search\QueryPopularity;
@@ -29,42 +30,42 @@ class Search implements ProductQueryInterface
2930
/**
3031
* @var SearchInterface
3132
*/
32-
private $search;
33+
private SearchInterface $search;
3334

3435
/**
3536
* @var SearchResultFactory
3637
*/
37-
private $searchResultFactory;
38+
private SearchResultFactory $searchResultFactory;
3839

3940
/**
4041
* @var FieldSelection
4142
*/
42-
private $fieldSelection;
43+
private FieldSelection $fieldSelection;
4344

4445
/**
4546
* @var ArgumentsProcessorInterface
4647
*/
47-
private $argsSelection;
48+
private ArgumentsProcessorInterface $argsSelection;
4849

4950
/**
5051
* @var ProductSearch
5152
*/
52-
private $productsProvider;
53+
private ProductSearch $productsProvider;
5354

5455
/**
5556
* @var SearchCriteriaBuilder
5657
*/
57-
private $searchCriteriaBuilder;
58+
private SearchCriteriaBuilder $searchCriteriaBuilder;
5859

5960
/**
6061
* @var Suggestions
6162
*/
62-
private $suggestions;
63+
private Suggestions $suggestions;
6364

6465
/**
6566
* @var QueryPopularity
6667
*/
67-
private $queryPopularity;
68+
private QueryPopularity $queryPopularity;
6869

6970
/**
7071
* @param SearchInterface $search
@@ -111,47 +112,62 @@ public function getResult(
111112
ContextInterface $context
112113
): SearchResult {
113114
$searchCriteria = $this->buildSearchCriteria($args, $info);
114-
$itemsResults = $this->search->search($searchCriteria);
115-
$searchResults = $this->productsProvider->getList(
116-
$searchCriteria,
117-
$itemsResults,
118-
$this->fieldSelection->getProductsFieldSelection($info),
119-
$context
120-
);
121-
122-
$totalPages = $searchCriteria->getPageSize()
123-
? ((int)ceil($searchResults->getTotalCount() / $searchCriteria->getPageSize()))
124-
: 0;
125-
126-
// add query statistics data
127-
if (!empty($args['search'])) {
128-
$this->queryPopularity->execute($context, $args['search'], (int) $searchResults->getTotalCount());
115+
try {
116+
$itemsResults = $this->search->search($searchCriteria);
117+
$searchResults = $this->productsProvider->getList(
118+
$searchCriteria,
119+
$itemsResults,
120+
$this->fieldSelection->getProductsFieldSelection($info),
121+
$context
122+
);
123+
124+
$totalPages = $searchCriteria->getPageSize()
125+
? ((int)ceil($searchResults->getTotalCount() / $searchCriteria->getPageSize()))
126+
: 0;
127+
128+
// add query statistics data
129+
if (!empty($args['search'])) {
130+
$this->queryPopularity->execute($context, $args['search'], (int) $searchResults->getTotalCount());
131+
}
132+
133+
$productArray = [];
134+
/** @var \Magento\Catalog\Model\Product $product */
135+
foreach ($searchResults->getItems() as $product) {
136+
$productArray[$product->getId()] = $product->getData();
137+
$productArray[$product->getId()]['model'] = $product;
138+
}
139+
140+
$suggestions = [];
141+
$totalCount = (int) $searchResults->getTotalCount();
142+
if ($totalCount === 0 && !empty($args['search'])) {
143+
$suggestions = $this->suggestions->execute($context, $args['search']);
144+
}
145+
146+
return $this->searchResultFactory->create(
147+
[
148+
'totalCount' => $totalCount,
149+
'productsSearchResult' => $productArray,
150+
'searchAggregation' => $itemsResults->getAggregations(),
151+
'pageSize' => $args['pageSize'],
152+
'currentPage' => $args['currentPage'],
153+
'totalPages' => $totalPages,
154+
'suggestions' => $suggestions,
155+
]
156+
);
157+
} catch (ClientException) {
158+
return $this->searchResultFactory->create(
159+
[
160+
'totalCount' => 0,
161+
'productsSearchResult' => [],
162+
'searchAggregation' => null,
163+
'pageSize' => $args['pageSize'],
164+
'currentPage' => $args['currentPage'],
165+
'totalPages' => 0,
166+
'suggestions' => [],
167+
]
168+
);
129169
}
130170

131-
$productArray = [];
132-
/** @var \Magento\Catalog\Model\Product $product */
133-
foreach ($searchResults->getItems() as $product) {
134-
$productArray[$product->getId()] = $product->getData();
135-
$productArray[$product->getId()]['model'] = $product;
136-
}
137-
138-
$suggestions = [];
139-
$totalCount = (int) $searchResults->getTotalCount();
140-
if ($totalCount === 0 && !empty($args['search'])) {
141-
$suggestions = $this->suggestions->execute($context, $args['search']);
142-
}
143-
144-
return $this->searchResultFactory->create(
145-
[
146-
'totalCount' => $totalCount,
147-
'productsSearchResult' => $productArray,
148-
'searchAggregation' => $itemsResults->getAggregations(),
149-
'pageSize' => $args['pageSize'],
150-
'currentPage' => $args['currentPage'],
151-
'totalPages' => $totalPages,
152-
'suggestions' => $suggestions,
153-
]
154-
);
155171
}
156172

157173
/**

lib/internal/Magento/Framework/Search/Search.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
*/
66
namespace Magento\Framework\Search;
77

8-
use Magento\AdvancedSearch\Model\Client\ClientException;
98
use Magento\Framework\Api\Search\SearchInterface;
109
use Magento\Framework\Api\Search\SearchCriteriaInterface;
1110
use Magento\Framework\App\ScopeResolverInterface;
12-
use Magento\Framework\Exception\LocalizedException;
1311
use Magento\Framework\Search\Request\Builder;
1412

1513
/**
@@ -83,14 +81,10 @@ public function search(SearchCriteriaInterface $searchCriteria)
8381
if (method_exists($this->requestBuilder, 'setSort')) {
8482
$this->requestBuilder->setSort($searchCriteria->getSortOrders());
8583
}
86-
try {
87-
$request = $this->requestBuilder->create();
88-
$searchResponse = $this->searchEngine->search($request);
89-
$response = $this->searchResponseBuilder->build($searchResponse)
90-
->setSearchCriteria($searchCriteria);
91-
} catch (ClientException $e) {
92-
throw new LocalizedException(__('Could not perform search'), $e, $e->getCode());
93-
}
84+
$request = $this->requestBuilder->create();
85+
$searchResponse = $this->searchEngine->search($request);
86+
$response = $this->searchResponseBuilder->build($searchResponse)
87+
->setSearchCriteria($searchCriteria);
9488

9589
return $response;
9690
}

0 commit comments

Comments
 (0)