Skip to content

Commit 6858667

Browse files
committed
Merge remote-tracking branch 'origin/BUG#AC-700' into GL_Mainline_PR_03012022
2 parents d4868c8 + fc1f595 commit 6858667

File tree

4 files changed

+96
-3
lines changed

4 files changed

+96
-3
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ public function getFields(array $context = []): array
127127
*
128128
* @param AbstractAttribute $attribute
129129
* @return array
130+
*
131+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
132+
* @SuppressWarnings(PHPMD.NPathComplexity)
130133
*/
131134
public function getField(AbstractAttribute $attribute): array
132135
{
@@ -150,7 +153,7 @@ public function getField(AbstractAttribute $attribute): array
150153
$fieldMapping[$fieldName]['index'] = $index;
151154
}
152155

153-
if ($attributeAdapter->isSortable()) {
156+
if ($attributeAdapter->isSortable() && !$attributeAdapter->isComplexType()) {
154157
$sortFieldName = $this->fieldNameResolver->getFieldName(
155158
$attributeAdapter,
156159
['type' => FieldMapperInterface::TYPE_SORT]
@@ -188,6 +191,20 @@ public function getField(AbstractAttribute $attribute): array
188191
$fieldMapping[$childFieldName] = [
189192
'type' => $this->fieldTypeConverter->convert(FieldTypeConverterInterface::INTERNAL_DATA_TYPE_STRING)
190193
];
194+
if ($attributeAdapter->isSortable()) {
195+
$sortFieldName = $this->fieldNameResolver->getFieldName(
196+
$attributeAdapter,
197+
['type' => FieldMapperInterface::TYPE_SORT]
198+
);
199+
$fieldMapping[$childFieldName]['fields'][$sortFieldName] = [
200+
'type' => $this->fieldTypeConverter->convert(
201+
FieldTypeConverterInterface::INTERNAL_DATA_TYPE_KEYWORD
202+
),
203+
'index' => $this->indexTypeConverter->convert(
204+
IndexTypeConverterInterface::INTERNAL_NO_ANALYZE_VALUE
205+
)
206+
];
207+
}
191208
}
192209

193210
return $fieldMapping;

app/code/Magento/Elasticsearch/SearchAdapter/Query/Builder/Sort.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ public function __construct(
7474
*
7575
* @param RequestInterface $request
7676
* @return array
77+
*
78+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
79+
* @SuppressWarnings(PHPMD.NPathComplexity)
7780
*/
7881
public function getSort(RequestInterface $request)
7982
{
@@ -94,7 +97,18 @@ public function getSort(RequestInterface $request)
9497
if (isset($this->map[$fieldName])) {
9598
$fieldName = $this->map[$fieldName];
9699
}
97-
if ($attribute->isSortable() && !($attribute->isFloatType() || $attribute->isIntegerType())) {
100+
if ($attribute->isSortable() &&
101+
!$attribute->isComplexType() &&
102+
!($attribute->isFloatType() || $attribute->isIntegerType())
103+
) {
104+
$suffix = $this->fieldNameResolver->getFieldName(
105+
$attribute,
106+
['type' => FieldMapperInterface::TYPE_SORT]
107+
);
108+
$fieldName .= '.' . $suffix;
109+
}
110+
if ($attribute->isComplexType() && $attribute->isSortable()) {
111+
$fieldName .= '_value';
98112
$suffix = $this->fieldNameResolver->getFieldName(
99113
$attribute,
100114
['type' => FieldMapperInterface::TYPE_SORT]

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,34 @@ public function attributeProvider(): array
278278
],
279279
],
280280
],
281+
[
282+
'attr_code',
283+
'select',
284+
false,
285+
false,
286+
'select',
287+
true,
288+
false,
289+
'attr_code_value',
290+
'',
291+
'sort_attr_code',
292+
[
293+
'attr_code_value' => [
294+
'type' => 'select',
295+
'index' => false,
296+
'fields' => [
297+
'sort_attr_code' => [
298+
'type' => 'string',
299+
'index' => 'not_analyzed',
300+
],
301+
],
302+
],
303+
'store_id' => [
304+
'type' => 'string',
305+
'index' => 'no',
306+
],
307+
],
308+
],
281309
[
282310
'attr_code',
283311
'text',

app/code/Magento/Elasticsearch/Test/Unit/SearchAdapter/Query/Builder/SortTest.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ protected function setUp(): void
5959

6060
/**
6161
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
62+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
6263
* @dataProvider getSortProvider
6364
* @param array $sortItems
6465
* @param $isSortable
@@ -72,6 +73,7 @@ public function testGetSort(
7273
$isSortable,
7374
$isFloatType,
7475
$isIntegerType,
76+
$isComplexType,
7577
$fieldName,
7678
array $expected
7779
) {
@@ -85,7 +87,7 @@ public function testGetSort(
8587
->willReturn($sortItems);
8688
$attributeMock = $this->getMockBuilder(AttributeAdapter::class)
8789
->disableOriginalConstructor()
88-
->setMethods(['isSortable', 'isFloatType', 'isIntegerType'])
90+
->setMethods(['isSortable', 'isFloatType', 'isIntegerType', 'isComplexType'])
8991
->getMock();
9092
$attributeMock->expects($this->any())
9193
->method('isSortable')
@@ -96,6 +98,9 @@ public function testGetSort(
9698
$attributeMock->expects($this->any())
9799
->method('isIntegerType')
98100
->willReturn($isIntegerType);
101+
$attributeMock->expects($this->any())
102+
->method('isComplexType')
103+
->willReturn($isComplexType);
99104
$this->attributeAdapterProvider->expects($this->any())
100105
->method('getByAttributeCode')
101106
->with($this->anything())
@@ -136,6 +141,7 @@ public function getSortProvider()
136141
false,
137142
false,
138143
false,
144+
false,
139145
null,
140146
[]
141147
],
@@ -153,6 +159,7 @@ public function getSortProvider()
153159
false,
154160
false,
155161
false,
162+
false,
156163
'price',
157164
[
158165
[
@@ -176,6 +183,7 @@ public function getSortProvider()
176183
true,
177184
true,
178185
true,
186+
false,
179187
'price',
180188
[
181189
[
@@ -199,6 +207,7 @@ public function getSortProvider()
199207
true,
200208
false,
201209
false,
210+
false,
202211
'name',
203212
[
204213
[
@@ -222,6 +231,7 @@ public function getSortProvider()
222231
false,
223232
false,
224233
false,
234+
false,
225235
'not_eav_attribute',
226236
[
227237
[
@@ -230,6 +240,30 @@ public function getSortProvider()
230240
]
231241
]
232242
]
243+
],
244+
[
245+
[
246+
[
247+
'field' => 'entity_id',
248+
'direction' => 'DESC'
249+
],
250+
[
251+
'field' => 'color',
252+
'direction' => 'DESC'
253+
],
254+
],
255+
true,
256+
false,
257+
false,
258+
true,
259+
'color',
260+
[
261+
[
262+
'color_value.sort_color' => [
263+
'order' => 'desc'
264+
]
265+
]
266+
]
233267
]
234268
];
235269
}

0 commit comments

Comments
 (0)