Skip to content

Commit 6d92047

Browse files
committed
ACP2E-249: Limit of total fields [XXXXX] in index has been exceeded
1 parent e88d5bd commit 6d92047

File tree

5 files changed

+232
-151
lines changed

5 files changed

+232
-151
lines changed

app/code/Magento/Elasticsearch/Model/Adapter/Elasticsearch.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ class Elasticsearch
3939
*/
4040
private const MAPPING_TOTAL_FIELDS_BUFFER_LIMIT = 1000;
4141

42-
/**#@-*/
42+
/**
43+
* @var ConnectionManager
44+
*/
4345
protected $connectionManager;
4446

4547
/**
@@ -491,7 +493,10 @@ protected function prepareIndex($storeId, $indexName, $mappedIndexerId)
491493
'entityType' => $mappedIndexerId,
492494
// Use store id instead of website id from context for save existing fields mapping.
493495
// In future websiteId will be eliminated due to index stored per store
494-
'websiteId' => $storeId
496+
'websiteId' => $storeId,
497+
// this parameter is introduced to replace 'websiteId' which name does not reflect
498+
// the value assigned to it
499+
'storeId' => $storeId
495500
]
496501
);
497502
$settings['index']['mapping']['total_fields']['limit'] = $this->getMappingTotalFieldsLimit($allAttributeTypes);

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

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

2224
/**
2325
* Provide dynamic fields for product.
@@ -65,6 +67,11 @@ class DynamicField implements FieldProviderInterface
6567
*/
6668
private $fieldNameResolver;
6769

70+
/**
71+
* @var StoreManagerInterface
72+
*/
73+
private $storeManager;
74+
6875
/**
6976
* @param FieldTypeConverterInterface $fieldTypeConverter
7077
* @param IndexTypeConverterInterface $indexTypeConverter
@@ -73,6 +80,7 @@ class DynamicField implements FieldProviderInterface
7380
* @param FieldNameResolver $fieldNameResolver
7481
* @param AttributeProvider $attributeAdapterProvider
7582
* @param Collection $categoryCollection
83+
* @param StoreManagerInterface|null $storeManager
7684
*/
7785
public function __construct(
7886
FieldTypeConverterInterface $fieldTypeConverter,
@@ -81,7 +89,8 @@ public function __construct(
8189
SearchCriteriaBuilder $searchCriteriaBuilder,
8290
FieldNameResolver $fieldNameResolver,
8391
AttributeProvider $attributeAdapterProvider,
84-
Collection $categoryCollection
92+
Collection $categoryCollection,
93+
?StoreManagerInterface $storeManager = null
8594
) {
8695
$this->groupRepository = $groupRepository;
8796
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
@@ -90,6 +99,7 @@ public function __construct(
9099
$this->fieldNameResolver = $fieldNameResolver;
91100
$this->attributeAdapterProvider = $attributeAdapterProvider;
92101
$this->categoryCollection = $categoryCollection;
102+
$this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(StoreManagerInterface::class);
93103
}
94104

95105
/**
@@ -123,7 +133,17 @@ public function getFields(array $context = []): array
123133
$searchCriteria = $this->searchCriteriaBuilder->create();
124134
$groups = $this->groupRepository->getList($searchCriteria)->getItems();
125135
$priceAttribute = $this->attributeAdapterProvider->getByAttributeCode('price');
126-
$ctx = isset($context['websiteId']) ? ['websiteId' => $context['websiteId']] : [];
136+
/**
137+
* For backword compatibility, we use 'websiteId' if the 'storeId' parameter is missing,
138+
* although the 'websiteId' may contain the store ID instead of website ID
139+
* @see \Magento\Elasticsearch\Model\Adapter\Elasticsearch:494
140+
*/
141+
$ctx = [];
142+
if (isset($context['storeId'])) {
143+
$ctx['websiteId'] = $this->storeManager->getStore($context['storeId'])->getWebsiteId();
144+
} elseif (isset($context['websiteId'])) {
145+
$ctx['websiteId'] = $context['websiteId'];
146+
}
127147
foreach ($groups as $group) {
128148
$ctx['customerGroupId'] = $group->getId();
129149
$groupPriceKey = $this->fieldNameResolver->getFieldName(

app/code/Magento/Elasticsearch/Test/Unit/Model/Adapter/ElasticsearchTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
* Test for Elasticsearch client
3535
*
3636
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
37+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
38+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
39+
* @SuppressWarnings(PHPMD.NPathComplexity)
3740
*/
3841
class ElasticsearchTest extends TestCase
3942
{
@@ -159,6 +162,13 @@ protected function setUp(): void
159162
->willReturn($this->client);
160163
$this->fieldMapper->expects($this->any())
161164
->method('getAllAttributesTypes')
165+
->with(
166+
[
167+
'entityType' => 'product',
168+
'websiteId' => 1,
169+
'storeId' => 1,
170+
]
171+
)
162172
->willReturn(
163173
[
164174
'name' => [

0 commit comments

Comments
 (0)