Skip to content

Commit bfc68a8

Browse files
committed
AC-10652::ES 8 compatibility with 2.4.7 and 2.4.6
1 parent 74b1871 commit bfc68a8

File tree

19 files changed

+1881
-8
lines changed

19 files changed

+1881
-8
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Elasticsearch\Elasticsearch5\Model\Adapter\BatchDataMapper;
9+
10+
use Magento\AdvancedSearch\Model\Adapter\DataMapper\AdditionalFieldsProviderInterface;
11+
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\AttributeProvider;
12+
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldName\ResolverInterface;
13+
use Magento\Elasticsearch\Model\ResourceModel\Index;
14+
15+
/**
16+
* Provide data mapping for categories fields
17+
*/
18+
class CategoryFieldsProvider implements AdditionalFieldsProviderInterface
19+
{
20+
/**
21+
* @var Index
22+
*/
23+
private $resourceIndex;
24+
25+
/**
26+
* @var AttributeProvider
27+
*/
28+
private $attributeAdapterProvider;
29+
30+
/**
31+
* @var ResolverInterface
32+
*/
33+
private $fieldNameResolver;
34+
35+
/**
36+
* @param Index $resourceIndex
37+
* @param AttributeProvider $attributeAdapterProvider
38+
* @param ResolverInterface $fieldNameResolver
39+
*/
40+
public function __construct(
41+
Index $resourceIndex,
42+
AttributeProvider $attributeAdapterProvider,
43+
ResolverInterface $fieldNameResolver
44+
) {
45+
$this->resourceIndex = $resourceIndex;
46+
$this->attributeAdapterProvider = $attributeAdapterProvider;
47+
$this->fieldNameResolver = $fieldNameResolver;
48+
}
49+
50+
/**
51+
* @inheritdoc
52+
*/
53+
public function getFields(array $productIds, $storeId)
54+
{
55+
$categoryData = $this->resourceIndex->getFullCategoryProductIndexData($storeId, $productIds);
56+
57+
$fields = [];
58+
foreach ($productIds as $productId) {
59+
$fields[$productId] = $this->getProductCategoryData($productId, $categoryData);
60+
}
61+
62+
return $fields;
63+
}
64+
65+
/**
66+
* Prepare category index data for product
67+
*
68+
* @param int $productId
69+
* @param array $categoryIndexData
70+
* @return array
71+
*/
72+
private function getProductCategoryData($productId, array $categoryIndexData)
73+
{
74+
$result = [];
75+
76+
if (array_key_exists($productId, $categoryIndexData)) {
77+
$indexData = $categoryIndexData[$productId];
78+
$categoryIds = array_column($indexData, 'id');
79+
80+
if (count($categoryIds)) {
81+
$result = ['category_ids' => $categoryIds];
82+
$positionAttribute = $this->attributeAdapterProvider->getByAttributeCode('position');
83+
$categoryNameAttribute = $this->attributeAdapterProvider->getByAttributeCode('category_name');
84+
foreach ($indexData as $data) {
85+
$categoryPositionKey = $this->fieldNameResolver->getFieldName(
86+
$positionAttribute,
87+
['categoryId' => $data['id']]
88+
);
89+
$categoryNameKey = $this->fieldNameResolver->getFieldName(
90+
$categoryNameAttribute,
91+
['categoryId' => $data['id']]
92+
);
93+
$result[$categoryPositionKey] = $data['position'];
94+
$result[$categoryNameKey] = $data['name'];
95+
}
96+
}
97+
}
98+
99+
return $result;
100+
}
101+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Elasticsearch\Elasticsearch5\Model\Adapter\BatchDataMapper;
7+
8+
use Magento\AdvancedSearch\Model\Client\ClientResolver;
9+
use Magento\AdvancedSearch\Model\Adapter\DataMapper\AdditionalFieldsProviderInterface;
10+
11+
/**
12+
* Proxy for data mapping of categories fields
13+
*/
14+
class CategoryFieldsProviderProxy implements AdditionalFieldsProviderInterface
15+
{
16+
/**
17+
* @var ClientResolver
18+
*/
19+
private $clientResolver;
20+
21+
/**
22+
* @var AdditionalFieldsProviderInterface[]
23+
*/
24+
private $categoryFieldsProviders;
25+
26+
/**
27+
* CategoryFieldsProviderProxy constructor.
28+
* @param ClientResolver $clientResolver
29+
* @param AdditionalFieldsProviderInterface[] $categoryFieldsProviders
30+
*/
31+
public function __construct(
32+
ClientResolver $clientResolver,
33+
array $categoryFieldsProviders
34+
) {
35+
$this->clientResolver = $clientResolver;
36+
$this->categoryFieldsProviders = $categoryFieldsProviders;
37+
}
38+
39+
/**
40+
* @return AdditionalFieldsProviderInterface
41+
*/
42+
private function getCategoryFieldsProvider()
43+
{
44+
return $this->categoryFieldsProviders[$this->clientResolver->getCurrentEngine()];
45+
}
46+
47+
/**
48+
* @inheritdoc
49+
*/
50+
public function getFields(array $productIds, $storeId)
51+
{
52+
return $this->getCategoryFieldsProvider()->getFields($productIds, $storeId);
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Elasticsearch\Elasticsearch5\Model\Adapter\FieldMapper\Product\FieldProvider\FieldIndex;
9+
10+
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldIndex\ConverterInterface;
11+
12+
/**
13+
* Field type converter from internal index type to elastic service.
14+
*/
15+
class Converter implements ConverterInterface
16+
{
17+
/**
18+
* Text flags for Elasticsearch index value
19+
*/
20+
private const ES_NO_INDEX = false;
21+
22+
/**
23+
* Text flags for Elasticsearch no analyze index value
24+
*/
25+
private const ES_NO_ANALYZE = false;
26+
27+
/**
28+
* Mapping between internal data types and elastic service.
29+
*
30+
* @var array
31+
*/
32+
private $mapping = [
33+
ConverterInterface::INTERNAL_NO_INDEX_VALUE => self::ES_NO_INDEX,
34+
ConverterInterface::INTERNAL_NO_ANALYZE_VALUE => self::ES_NO_ANALYZE,
35+
];
36+
37+
/**
38+
* Get service field index type for elasticsearch 5.
39+
*
40+
* @param string $internalType
41+
* @return string|boolean
42+
* @throws \DomainException
43+
*/
44+
public function convert(string $internalType)
45+
{
46+
if (!isset($this->mapping[$internalType])) {
47+
throw new \DomainException(sprintf('Unsupported internal field index type: %s', $internalType));
48+
}
49+
return $this->mapping[$internalType];
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Elasticsearch\Elasticsearch5\Model\Adapter\FieldMapper\Product\FieldProvider\FieldIndex;
9+
10+
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\AttributeAdapter;
11+
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldIndex\ResolverInterface;
12+
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldIndex\ConverterInterface;
13+
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldType\ConverterInterface
14+
as FieldTypeConverterInterface;
15+
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldType\ResolverInterface
16+
as FieldTypeResolver;
17+
18+
/**
19+
* Field index resolver that provides index type for the attribute in mapping.
20+
* For example, we need to set ‘no’/false in the case when attribute must be present in index data,
21+
* but stay as not indexable.
22+
*/
23+
class IndexResolver implements ResolverInterface
24+
{
25+
/**
26+
* @var ConverterInterface
27+
*/
28+
private $converter;
29+
30+
/**
31+
* @var FieldTypeConverterInterface
32+
*/
33+
private $fieldTypeConverter;
34+
35+
/**
36+
* @var FieldTypeResolver
37+
*/
38+
private $fieldTypeResolver;
39+
40+
/**
41+
* @param ConverterInterface $converter
42+
* @param FieldTypeConverterInterface $fieldTypeConverter
43+
* @param FieldTypeResolver $fieldTypeResolver
44+
*/
45+
public function __construct(
46+
ConverterInterface $converter,
47+
FieldTypeConverterInterface $fieldTypeConverter,
48+
FieldTypeResolver $fieldTypeResolver
49+
) {
50+
$this->converter = $converter;
51+
$this->fieldTypeConverter = $fieldTypeConverter;
52+
$this->fieldTypeResolver = $fieldTypeResolver;
53+
}
54+
55+
/**
56+
* @inheritdoc
57+
*/
58+
public function getFieldIndex(AttributeAdapter $attribute)
59+
{
60+
$index = null;
61+
if (!$attribute->isSearchable()
62+
&& !$attribute->isAlwaysIndexable()
63+
&& ($this->isStringServiceFieldType($attribute) || $attribute->isComplexType())
64+
&& !(($attribute->isIntegerType() || $attribute->isBooleanType())
65+
&& !$attribute->isUserDefined())
66+
&& !$attribute->isFloatType()
67+
) {
68+
$index = $this->converter->convert(ConverterInterface::INTERNAL_NO_INDEX_VALUE);
69+
}
70+
71+
return $index;
72+
}
73+
74+
/**
75+
* Check if service field type for field set as 'string'
76+
*
77+
* @param AttributeAdapter $attribute
78+
* @return bool
79+
*/
80+
private function isStringServiceFieldType(AttributeAdapter $attribute): bool
81+
{
82+
$serviceFieldType = $this->fieldTypeResolver->getFieldType($attribute);
83+
$stringTypeKey = $this->fieldTypeConverter->convert(FieldTypeConverterInterface::INTERNAL_DATA_TYPE_STRING);
84+
85+
return $serviceFieldType === $stringTypeKey;
86+
}
87+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Elasticsearch\Elasticsearch5\Model\Adapter\FieldMapper\Product\FieldProvider\FieldType;
9+
10+
use Magento\Elasticsearch\Model\Adapter\FieldMapper\Product\FieldProvider\FieldType\ConverterInterface;
11+
12+
/**
13+
* Field type converter from internal data types to elastic service.
14+
*/
15+
class Converter implements ConverterInterface
16+
{
17+
/**#@+
18+
* Text flags for Elasticsearch field types
19+
*/
20+
private const ES_DATA_TYPE_TEXT = 'text';
21+
private const ES_DATA_TYPE_KEYWORD = 'keyword';
22+
private const ES_DATA_TYPE_DOUBLE = 'double';
23+
private const ES_DATA_TYPE_INT = 'integer';
24+
private const ES_DATA_TYPE_DATE = 'date';
25+
/**#@-*/
26+
27+
/**
28+
* Mapping between internal data types and elastic service.
29+
*
30+
* @var array
31+
*/
32+
private $mapping = [
33+
self::INTERNAL_DATA_TYPE_STRING => self::ES_DATA_TYPE_TEXT,
34+
self::INTERNAL_DATA_TYPE_KEYWORD => self::ES_DATA_TYPE_KEYWORD,
35+
self::INTERNAL_DATA_TYPE_FLOAT => self::ES_DATA_TYPE_DOUBLE,
36+
self::INTERNAL_DATA_TYPE_INT => self::ES_DATA_TYPE_INT,
37+
self::INTERNAL_DATA_TYPE_DATE => self::ES_DATA_TYPE_DATE,
38+
];
39+
40+
/**
41+
* Get service field type for elasticsearch 5.
42+
*
43+
* @param string $internalType
44+
* @return string
45+
* @throws \DomainException
46+
*/
47+
public function convert(string $internalType): string
48+
{
49+
if (!isset($this->mapping[$internalType])) {
50+
throw new \DomainException(sprintf('Unsupported internal field type: %s', $internalType));
51+
}
52+
return $this->mapping[$internalType];
53+
}
54+
}

0 commit comments

Comments
 (0)