Skip to content

Commit 832a9e4

Browse files
committed
Merge remote-tracking branch 'tango/MC-29530' into DEC-PR-01
2 parents 14251e7 + 64561a6 commit 832a9e4

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ class ProductDataMapper implements BatchDataMapperInterface
7777
'tax_class_id'
7878
];
7979

80+
/**
81+
* @var string[]
82+
*/
83+
private $sortableAttributesValuesToImplode = [
84+
'name'
85+
];
86+
8087
/**
8188
* Construction for DocumentDataMapper
8289
*
@@ -86,19 +93,25 @@ class ProductDataMapper implements BatchDataMapperInterface
8693
* @param AdditionalFieldsProviderInterface $additionalFieldsProvider
8794
* @param DataProvider $dataProvider
8895
* @param array $excludedAttributes
96+
* @param array $sortableAttributesValuesToImplode
8997
*/
9098
public function __construct(
9199
Builder $builder,
92100
FieldMapperInterface $fieldMapper,
93101
DateFieldType $dateFieldType,
94102
AdditionalFieldsProviderInterface $additionalFieldsProvider,
95103
DataProvider $dataProvider,
96-
array $excludedAttributes = []
104+
array $excludedAttributes = [],
105+
array $sortableAttributesValuesToImplode = []
97106
) {
98107
$this->builder = $builder;
99108
$this->fieldMapper = $fieldMapper;
100109
$this->dateFieldType = $dateFieldType;
101110
$this->excludedAttributes = array_merge($this->defaultExcludedAttributes, $excludedAttributes);
111+
$this->sortableAttributesValuesToImplode = array_merge(
112+
$this->sortableAttributesValuesToImplode,
113+
$sortableAttributesValuesToImplode
114+
);
102115
$this->additionalFieldsProvider = $additionalFieldsProvider;
103116
$this->dataProvider = $dataProvider;
104117
$this->attributeOptionsCache = [];
@@ -241,6 +254,13 @@ private function prepareAttributeValues(
241254
}
242255
}
243256

257+
if ($attribute->getUsedForSortBy()
258+
&& in_array($attribute->getAttributeCode(), $this->sortableAttributesValuesToImplode)
259+
&& count($attributeValues) > 1
260+
) {
261+
$attributeValues = [$productId => implode(' ', $attributeValues)];
262+
}
263+
244264
return $attributeValues;
245265
}
246266

dev/tests/integration/testsuite/Magento/Elasticsearch/Model/Indexer/ReindexAllTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,42 @@ public function testSearchAll()
7979
self::assertGreaterThanOrEqual(2, $result);
8080
}
8181

82+
/**
83+
* Test sorting of all products after full reindex
84+
*
85+
* @magentoDbIsolation enabled
86+
* @magentoConfigFixture default/catalog/search/engine elasticsearch6
87+
* @magentoConfigFixture current_store catalog/search/elasticsearch_index_prefix indexerhandlertest_configurable
88+
* @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_products.php
89+
*/
90+
public function testSort()
91+
{
92+
/** @var $productFifth \Magento\Catalog\Model\Product */
93+
$productSimple = Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
94+
$productSimple->setTypeId('simple')
95+
->setAttributeSetId(4)
96+
->setWebsiteIds([1])
97+
->setName('ABC')
98+
->setSku('abc-first-in-sort')
99+
->setPrice(20)
100+
->setMetaTitle('meta title')
101+
->setMetaKeyword('meta keyword')
102+
->setMetaDescription('meta description')
103+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
104+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
105+
->setStockData(['use_config_manage_stock' => 0])
106+
->save();
107+
$productConfigurableOption = $this->productRepository->get('simple_10');
108+
$productConfigurableOption->setName('1ABC');
109+
$this->productRepository->save($productConfigurableOption);
110+
$this->reindexAll();
111+
$productSimple = $this->productRepository->get('abc-first-in-sort');
112+
$result = $this->sortByName();
113+
$firstInSearchResults = (int) $result[0]['_id'];
114+
$productSimpleId = (int) $productSimple->getId();
115+
$this->assertEquals($productSimpleId, $firstInSearchResults);
116+
}
117+
82118
/**
83119
* Test search of specific product after full reindex
84120
*
@@ -125,6 +161,38 @@ private function searchByName($text)
125161
return isset($queryResult['hits']['hits']) ? $queryResult['hits']['hits'] : [];
126162
}
127163

164+
/**
165+
* @return array
166+
*/
167+
private function sortByName()
168+
{
169+
$storeId = $this->storeManager->getDefaultStoreView()->getId();
170+
$searchQuery = [
171+
'index' => $this->searchIndexNameResolver->getIndexName($storeId, 'catalogsearch_fulltext'),
172+
'type' => $this->clientConfig->getEntityType(),
173+
'body' => [
174+
'sort' => [
175+
'name.sort_name' => [
176+
'order' => 'asc'
177+
],
178+
],
179+
'query' => [
180+
'bool' => [
181+
'must' => [
182+
[
183+
'terms' => [
184+
'visibility' => [2, 4],
185+
],
186+
],
187+
],
188+
],
189+
],
190+
],
191+
];
192+
$queryResult = $this->client->query($searchQuery);
193+
return isset($queryResult['hits']['hits']) ? $queryResult['hits']['hits'] : [];
194+
}
195+
128196
/**
129197
* Make fulltext catalog search reindex
130198
*

0 commit comments

Comments
 (0)