Skip to content

Commit 72d4183

Browse files
committed
MAGETWO-98369: Improve degradation of CatalogSearch indexation with elasticsearch
1 parent b855438 commit 72d4183

File tree

2 files changed

+35
-24
lines changed

2 files changed

+35
-24
lines changed

app/code/Magento/Elasticsearch/Model/Adapter/FieldMapper/Product/FieldProvider/DynamicField.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\ResolverInterface
1919
as FieldNameResolver;
2020
use Magento\Framework\Api\SearchCriteriaBuilder;
21+
use Magento\Catalog\Model\ResourceModel\Category\Collection;
22+
use Magento\Framework\App\ObjectManager;
2123

2224
/**
2325
* Provide dynamic fields for product.
@@ -27,10 +29,18 @@ class DynamicField implements FieldProviderInterface
2729
/**
2830
* Category list.
2931
*
32+
* @deprecated
3033
* @var CategoryListInterface
3134
*/
3235
private $categoryList;
3336

37+
/**
38+
* Category collection.
39+
*
40+
* @var Collection
41+
*/
42+
private $categoryCollection;
43+
3444
/**
3545
* Customer group repository.
3646
*
@@ -73,6 +83,7 @@ class DynamicField implements FieldProviderInterface
7383
* @param CategoryListInterface $categoryList
7484
* @param FieldNameResolver $fieldNameResolver
7585
* @param AttributeProvider $attributeAdapterProvider
86+
* @param Collection|null $categoryCollection
7687
*/
7788
public function __construct(
7889
FieldTypeConverterInterface $fieldTypeConverter,
@@ -81,7 +92,8 @@ public function __construct(
8192
SearchCriteriaBuilder $searchCriteriaBuilder,
8293
CategoryListInterface $categoryList,
8394
FieldNameResolver $fieldNameResolver,
84-
AttributeProvider $attributeAdapterProvider
95+
AttributeProvider $attributeAdapterProvider,
96+
Collection $categoryCollection = null
8597
) {
8698
$this->groupRepository = $groupRepository;
8799
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
@@ -90,6 +102,8 @@ public function __construct(
90102
$this->categoryList = $categoryList;
91103
$this->fieldNameResolver = $fieldNameResolver;
92104
$this->attributeAdapterProvider = $attributeAdapterProvider;
105+
$this->categoryCollection = $categoryCollection ?:
106+
ObjectManager::getInstance()->get(Collection::class);
93107
}
94108

95109
/**
@@ -98,18 +112,17 @@ public function __construct(
98112
public function getFields(array $context = []): array
99113
{
100114
$allAttributes = [];
101-
$searchCriteria = $this->searchCriteriaBuilder->create();
102-
$categories = $this->categoryList->getList($searchCriteria)->getItems();
115+
$categoryIds = $this->categoryCollection->getAllIds();
103116
$positionAttribute = $this->attributeAdapterProvider->getByAttributeCode('position');
104117
$categoryNameAttribute = $this->attributeAdapterProvider->getByAttributeCode('category_name');
105-
foreach ($categories as $category) {
118+
foreach ($categoryIds as $categoryId) {
106119
$categoryPositionKey = $this->fieldNameResolver->getFieldName(
107120
$positionAttribute,
108-
['categoryId' => $category->getId()]
121+
['categoryId' => $categoryId]
109122
);
110123
$categoryNameKey = $this->fieldNameResolver->getFieldName(
111124
$categoryNameAttribute,
112-
['categoryId' => $category->getId()]
125+
['categoryId' => $categoryId]
113126
);
114127
$allAttributes[$categoryPositionKey] = [
115128
'type' => $this->fieldTypeConverter->convert(FieldTypeConverterInterface::INTERNAL_DATA_TYPE_STRING),
@@ -121,6 +134,7 @@ public function getFields(array $context = []): array
121134
];
122135
}
123136

137+
$searchCriteria = $this->searchCriteriaBuilder->create();
124138
$groups = $this->groupRepository->getList($searchCriteria)->getItems();
125139
$priceAttribute = $this->attributeAdapterProvider->getByAttributeCode('price');
126140
foreach ($groups as $group) {

app/code/Magento/Elasticsearch/Test/Unit/Model/Adapter/FieldMapper/Product/FieldProvider/DynamicFieldTest.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Magento\Customer\Api\Data\GroupInterface;
2525
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\ResolverInterface
2626
as FieldNameResolver;
27+
use Magento\Catalog\Model\ResourceModel\Category\Collection;
2728

2829
/**
2930
* @SuppressWarnings(PHPMD)
@@ -65,6 +66,11 @@ class DynamicFieldTest extends \PHPUnit\Framework\TestCase
6566
*/
6667
private $categoryList;
6768

69+
/**
70+
* @var Collection
71+
*/
72+
private $categoryCollection;
73+
6874
/**
6975
* @var FieldNameResolver
7076
*/
@@ -100,6 +106,10 @@ protected function setUp()
100106
$this->categoryList = $this->getMockBuilder(CategoryListInterface::class)
101107
->disableOriginalConstructor()
102108
->getMock();
109+
$this->categoryCollection = $this->getMockBuilder(Collection::class)
110+
->disableOriginalConstructor()
111+
->setMethods(['getAllIds'])
112+
->getMock();
103113

104114
$objectManager = new ObjectManagerHelper($this);
105115

@@ -113,6 +123,7 @@ protected function setUp()
113123
'attributeAdapterProvider' => $this->attributeAdapterProvider,
114124
'categoryList' => $this->categoryList,
115125
'fieldNameResolver' => $this->fieldNameResolver,
126+
'categoryCollection' => $this->categoryCollection,
116127
]
117128
);
118129
}
@@ -124,7 +135,6 @@ protected function setUp()
124135
* @param $groupId
125136
* @param array $expected
126137
* @return void
127-
* @throws \Magento\Framework\Exception\LocalizedException
128138
*/
129139
public function testGetAllAttributesTypes(
130140
$complexType,
@@ -138,10 +148,6 @@ public function testGetAllAttributesTypes(
138148
$this->searchCriteriaBuilder->expects($this->any())
139149
->method('create')
140150
->willReturn($searchCriteria);
141-
$categorySearchResults = $this->getMockBuilder(CategorySearchResultsInterface::class)
142-
->disableOriginalConstructor()
143-
->setMethods(['getItems'])
144-
->getMockForAbstractClass();
145151
$groupSearchResults = $this->getMockBuilder(GroupSearchResultsInterface::class)
146152
->disableOriginalConstructor()
147153
->setMethods(['getItems'])
@@ -156,19 +162,10 @@ public function testGetAllAttributesTypes(
156162
$groupSearchResults->expects($this->any())
157163
->method('getItems')
158164
->willReturn([$group]);
159-
$category = $this->getMockBuilder(CategoryInterface::class)
160-
->disableOriginalConstructor()
161-
->setMethods(['getId'])
162-
->getMockForAbstractClass();
163-
$category->expects($this->any())
164-
->method('getId')
165-
->willReturn($categoryId);
166-
$categorySearchResults->expects($this->any())
167-
->method('getItems')
168-
->willReturn([$category]);
169-
$this->categoryList->expects($this->any())
170-
->method('getList')
171-
->willReturn($categorySearchResults);
165+
166+
$this->categoryCollection->expects($this->any())
167+
->method('getAllIds')
168+
->willReturn([$categoryId]);
172169

173170
$categoryAttributeMock = $this->getMockBuilder(AttributeAdapter::class)
174171
->disableOriginalConstructor()

0 commit comments

Comments
 (0)