Skip to content

Commit fa6d291

Browse files
authored
Merge pull request #5880 from magento-tsg/2.4-develop-pr46
[TSG] Fixes for 2.4 (pr46) (2.4-develop)
2 parents 6f38590 + 78e0f74 commit fa6d291

File tree

12 files changed

+200
-68
lines changed

12 files changed

+200
-68
lines changed

app/code/Magento/Elasticsearch/Elasticsearch5/Model/Client/Elasticsearch.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Elasticsearch\Elasticsearch5\Model\Client;
78

89
use Magento\Framework\Exception\LocalizedException;
@@ -11,7 +12,7 @@
1112
/**
1213
* Elasticsearch client
1314
*
14-
* @deprecated the Elasticsearch 5 doesn't supported due to EOL
15+
* @deprecated 100.3.5 the Elasticsearch 5 doesn't supported due to EOL
1516
*/
1617
class Elasticsearch implements ClientInterface
1718
{
@@ -48,8 +49,10 @@ public function __construct(
4849
$options = [],
4950
$elasticsearchClient = null
5051
) {
51-
if (empty($options['hostname']) || ((!empty($options['enableAuth']) &&
52-
($options['enableAuth'] == 1)) && (empty($options['username']) || empty($options['password'])))) {
52+
if (empty($options['hostname'])
53+
|| ((!empty($options['enableAuth']) && ($options['enableAuth'] == 1))
54+
&& (empty($options['username']) || empty($options['password'])))
55+
) {
5356
throw new LocalizedException(
5457
__('The search failed because of a search engine misconfiguration.')
5558
);
@@ -302,7 +305,15 @@ public function addFieldsMapping(array $fields, $index, $entityType)
302305
]
303306
),
304307
],
305-
]
308+
],
309+
[
310+
'integer_mapping' => [
311+
'match_mapping_type' => 'long',
312+
'mapping' => [
313+
'type' => 'integer',
314+
],
315+
],
316+
],
306317
],
307318
],
308319
],
@@ -323,7 +334,6 @@ public function addFieldsMapping(array $fields, $index, $entityType)
323334
*/
324335
private function prepareFieldInfo($fieldInfo)
325336
{
326-
327337
if (strcmp($this->getServerVersion(), '5') < 0) {
328338
if ($fieldInfo['type'] == 'keyword') {
329339
$fieldInfo['type'] = 'string';

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

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Elasticsearch\Model\Adapter\BatchDataMapper;
78

89
use Magento\CatalogSearch\Model\Indexer\Fulltext\Action\DataProvider;
@@ -74,7 +75,7 @@ class ProductDataMapper implements BatchDataMapperInterface
7475
private $attributesExcludedFromMerge = [
7576
'status',
7677
'visibility',
77-
'tax_class_id'
78+
'tax_class_id',
7879
];
7980

8081
/**
@@ -85,15 +86,19 @@ class ProductDataMapper implements BatchDataMapperInterface
8586
];
8687

8788
/**
88-
* Construction for DocumentDataMapper
89-
*
89+
* @var string[]
90+
*/
91+
private $filterableAttributeTypes;
92+
93+
/**
9094
* @param Builder $builder
9195
* @param FieldMapperInterface $fieldMapper
9296
* @param DateFieldType $dateFieldType
9397
* @param AdditionalFieldsProviderInterface $additionalFieldsProvider
9498
* @param DataProvider $dataProvider
9599
* @param array $excludedAttributes
96100
* @param array $sortableAttributesValuesToImplode
101+
* @param array $filterableAttributeTypes
97102
*/
98103
public function __construct(
99104
Builder $builder,
@@ -102,7 +107,8 @@ public function __construct(
102107
AdditionalFieldsProviderInterface $additionalFieldsProvider,
103108
DataProvider $dataProvider,
104109
array $excludedAttributes = [],
105-
array $sortableAttributesValuesToImplode = []
110+
array $sortableAttributesValuesToImplode = [],
111+
array $filterableAttributeTypes = []
106112
) {
107113
$this->builder = $builder;
108114
$this->fieldMapper = $fieldMapper;
@@ -115,6 +121,7 @@ public function __construct(
115121
$this->additionalFieldsProvider = $additionalFieldsProvider;
116122
$this->dataProvider = $dataProvider;
117123
$this->attributeOptionsCache = [];
124+
$this->filterableAttributeTypes = $filterableAttributeTypes;
118125
}
119126

120127
/**
@@ -212,7 +219,7 @@ private function convertAttribute(Attribute $attribute, array $attributeValues,
212219
if ($retrievedValue !== null) {
213220
$productAttributes[$attribute->getAttributeCode()] = $retrievedValue;
214221

215-
if ($attribute->getIsSearchable()) {
222+
if ($this->isAttributeLabelsShouldBeMapped($attribute)) {
216223
$attributeLabels = $this->getValuesLabels($attribute, $attributeValues, $storeId);
217224
$retrievedLabel = $this->retrieveFieldValue($attributeLabels);
218225
if ($retrievedLabel) {
@@ -224,6 +231,26 @@ private function convertAttribute(Attribute $attribute, array $attributeValues,
224231
return $productAttributes;
225232
}
226233

234+
/**
235+
* Check if an attribute has one of the next storefront properties enabled for mapping labels:
236+
* - "Use in Search" (is_searchable)
237+
* - "Visible in Advanced Search" (is_visible_in_advanced_search)
238+
* - "Use in Layered Navigation" (is_filterable)
239+
* - "Use in Search Results Layered Navigation" (is_filterable_in_search)
240+
*
241+
* @param Attribute $attribute
242+
* @return bool
243+
*/
244+
private function isAttributeLabelsShouldBeMapped(Attribute $attribute): bool
245+
{
246+
return (
247+
$attribute->getIsSearchable()
248+
|| $attribute->getIsVisibleInAdvancedSearch()
249+
|| $attribute->getIsFilterable()
250+
|| $attribute->getIsFilterableInSearch()
251+
);
252+
}
253+
227254
/**
228255
* Prepare attribute values.
229256
*
@@ -249,6 +276,15 @@ private function prepareAttributeValues(
249276
$attributeValues = $this->prepareMultiselectValues($attributeValues);
250277
}
251278

279+
if (in_array($attribute->getFrontendInput(), $this->filterableAttributeTypes)) {
280+
$attributeValues = array_map(
281+
function (string $valueId) {
282+
return (int)$valueId;
283+
},
284+
$attributeValues
285+
);
286+
}
287+
252288
if ($this->isAttributeDate($attribute)) {
253289
foreach ($attributeValues as $key => $attributeValue) {
254290
$attributeValues[$key] = $this->dateFieldType->formatDate($storeId, $attributeValue);

app/code/Magento/Elasticsearch/Test/Unit/Elasticsearch5/Model/Client/ElasticsearchTest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ public function testAddFieldsMapping()
340340
'match_mapping_type' => 'string',
341341
'mapping' => [
342342
'type' => 'integer',
343-
'index' => true
343+
'index' => true,
344344
],
345345
],
346346
],
@@ -354,6 +354,14 @@ public function testAddFieldsMapping()
354354
],
355355
],
356356
],
357+
[
358+
'integer_mapping' => [
359+
'match_mapping_type' => 'long',
360+
'mapping' => [
361+
'type' => 'integer',
362+
],
363+
],
364+
],
357365
],
358366
],
359367
],
@@ -424,7 +432,15 @@ public function testAddFieldsMappingFailure()
424432
'index' => true,
425433
],
426434
],
427-
]
435+
],
436+
[
437+
'integer_mapping' => [
438+
'match_mapping_type' => 'long',
439+
'mapping' => [
440+
'type' => 'integer',
441+
],
442+
],
443+
],
428444
],
429445
],
430446
],

app/code/Magento/Elasticsearch/Test/Unit/Model/Adapter/BatchDataMapper/ProductDataMapperTest.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use PHPUnit\Framework\TestCase;
2222

2323
/**
24+
* Unit tests for \Magento\Elasticsearch\Model\Adapter\BatchDataMapper\ProductDataMapper class.
25+
*
2426
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2527
*/
2628
class ProductDataMapperTest extends TestCase
@@ -56,12 +58,12 @@ class ProductDataMapperTest extends TestCase
5658
private $additionalFieldsProvider;
5759

5860
/**
59-
* @var MockObject
61+
* @var DataProvider|MockObject
6062
*/
6163
private $dataProvider;
6264

6365
/**
64-
* Set up test environment.
66+
* @inheritdoc
6567
*/
6668
protected function setUp(): void
6769
{
@@ -71,6 +73,11 @@ protected function setUp(): void
7173
$this->attribute = $this->createMock(Attribute::class);
7274
$this->additionalFieldsProvider = $this->getMockForAbstractClass(AdditionalFieldsProviderInterface::class);
7375
$this->dateFieldTypeMock = $this->createMock(Date::class);
76+
$filterableAttributeTypes = [
77+
'boolean' => 'boolean',
78+
'multiselect' => 'multiselect',
79+
'select' => 'select',
80+
];
7481

7582
$objectManager = new ObjectManagerHelper($this);
7683
$this->model = $objectManager->getObject(
@@ -81,6 +88,7 @@ protected function setUp(): void
8188
'dateFieldType' => $this->dateFieldTypeMock,
8289
'dataProvider' => $this->dataProvider,
8390
'additionalFieldsProvider' => $this->additionalFieldsProvider,
91+
'filterableAttributeTypes' => $filterableAttributeTypes,
8492
]
8593
);
8694
}
@@ -159,8 +167,8 @@ public function testGetMap(int $productId, array $attributeData, $attributeValue
159167
$productId => [$attributeId => $attributeValue],
160168
];
161169
$documents = $this->model->map($documentData, $storeId, $context);
162-
$returnAttributeData['store_id'] = $storeId;
163-
$this->assertEquals($returnAttributeData, $documents[$productId]);
170+
$returnAttributeData = ['store_id' => $storeId] + $returnAttributeData;
171+
$this->assertSame($returnAttributeData, $documents[$productId]);
164172
}
165173

166174
/**
@@ -305,8 +313,8 @@ public static function mapProvider(): array
305313
['value' => '2', 'label' => 'Disabled'],
306314
],
307315
],
308-
[10 => '1', 11 => '2'],
309-
['status' => '1'],
316+
[10 => '1', 11 => '2'],
317+
['status' => 1],
310318
],
311319
'select without options' => [
312320
10,
@@ -318,7 +326,7 @@ public static function mapProvider(): array
318326
'options' => [],
319327
],
320328
'44',
321-
['color' => '44'],
329+
['color' => 44],
322330
],
323331
'unsearchable select with options' => [
324332
10,
@@ -333,7 +341,7 @@ public static function mapProvider(): array
333341
],
334342
],
335343
'44',
336-
['color' => '44'],
344+
['color' => 44],
337345
],
338346
'searchable select with options' => [
339347
10,
@@ -348,7 +356,7 @@ public static function mapProvider(): array
348356
],
349357
],
350358
'44',
351-
['color' => '44', 'color_value' => 'red'],
359+
['color' => 44, 'color_value' => 'red'],
352360
],
353361
'composite select with options' => [
354362
10,
@@ -363,7 +371,7 @@ public static function mapProvider(): array
363371
],
364372
],
365373
[10 => '44', 11 => '45'],
366-
['color' => ['44', '45'], 'color_value' => ['red', 'black']],
374+
['color' => [44, 45], 'color_value' => ['red', 'black']],
367375
],
368376
'multiselect without options' => [
369377
10,
@@ -430,10 +438,10 @@ public static function mapProvider(): array
430438
'backend_type' => 'int',
431439
'frontend_input' => 'int',
432440
'is_searchable' => false,
433-
'options' => []
441+
'options' => [],
434442
],
435443
15,
436-
[]
444+
[],
437445
],
438446
];
439447
}

app/code/Magento/Elasticsearch/etc/di.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@
153153
<type name="Magento\Elasticsearch\Model\Adapter\BatchDataMapper\ProductDataMapper">
154154
<arguments>
155155
<argument name="additionalFieldsProvider" xsi:type="object">additionalFieldsProviderForElasticsearch</argument>
156+
<argument name="filterableAttributeTypes" xsi:type="array">
157+
<item name="boolean" xsi:type="string">boolean</item>
158+
<item name="multiselect" xsi:type="string">multiselect</item>
159+
<item name="select" xsi:type="string">select</item>
160+
</argument>
156161
</arguments>
157162
</type>
158163
<preference for="Magento\Elasticsearch\Model\Adapter\BatchDataMapperInterface" type="Magento\Elasticsearch\Model\Adapter\BatchDataMapper\DataMapperResolver" />

app/code/Magento/Elasticsearch6/Model/Client/Elasticsearch.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Elasticsearch6\Model\Client;
78

89
use Magento\AdvancedSearch\Model\Client\ClientInterface;
@@ -48,8 +49,10 @@ public function __construct(
4849
$elasticsearchClient = null,
4950
$fieldsMappingPreprocessors = []
5051
) {
51-
if (empty($options['hostname']) || ((!empty($options['enableAuth']) &&
52-
($options['enableAuth'] == 1)) && (empty($options['username']) || empty($options['password'])))) {
52+
if (empty($options['hostname'])
53+
|| ((!empty($options['enableAuth']) && ($options['enableAuth'] == 1))
54+
&& (empty($options['username']) || empty($options['password'])))
55+
) {
5356
throw new LocalizedException(
5457
__('The search failed because of a search engine misconfiguration.')
5558
);
@@ -303,7 +306,15 @@ public function addFieldsMapping(array $fields, $index, $entityType)
303306
'mapping' => [
304307
'type' => 'text',
305308
'index' => true,
306-
'copy_to' => '_search'
309+
'copy_to' => '_search',
310+
],
311+
],
312+
],
313+
[
314+
'integer_mapping' => [
315+
'match_mapping_type' => 'long',
316+
'mapping' => [
317+
'type' => 'integer',
307318
],
308319
],
309320
],

0 commit comments

Comments
 (0)