Skip to content

Commit 89fb35b

Browse files
committed
MAGETWO-36045: Default weight SPIKE
MAGETWO-38817: Document default field weights - Update after code review
1 parent e59b738 commit 89fb35b

File tree

3 files changed

+27
-21
lines changed

3 files changed

+27
-21
lines changed

app/code/Magento/CatalogSearch/Model/Adapter/Mysql/Filter/Preprocessor.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,10 @@ public function __construct(
6464

6565
/**
6666
* {@inheritdoc}
67-
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
6867
*/
6968
public function process(FilterInterface $filter, $isNegation, $query, QueryContainer $queryContainer)
7069
{
71-
return $resultQuery = $this->processQueryWithField($filter, $isNegation, $query, $queryContainer);
70+
return $this->processQueryWithField($filter, $isNegation, $query, $queryContainer);
7271
}
7372

7473
/**

app/code/Magento/CatalogSearch/Setup/InstallData.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,46 @@
77
namespace Magento\CatalogSearch\Setup;
88

99

10-
use Magento\Framework\DB\Adapter\AdapterInterface;
10+
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
11+
use Magento\Framework\Setup\InstallDataInterface;
1112
use Magento\Framework\Setup\ModuleContextInterface;
1213
use Magento\Framework\Setup\ModuleDataSetupInterface;
1314

14-
class InstallData implements \Magento\Framework\Setup\InstallDataInterface
15+
class InstallData implements InstallDataInterface
1516
{
17+
/**
18+
* @var ProductAttributeRepositoryInterface
19+
*/
20+
private $attributeRepository;
21+
22+
/**
23+
* @param ProductAttributeRepositoryInterface $attributeRepository
24+
*/
25+
public function __construct(ProductAttributeRepositoryInterface $attributeRepository)
26+
{
27+
$this->attributeRepository = $attributeRepository;
28+
}
29+
1630
/**
1731
* @param ModuleDataSetupInterface $setup
1832
* @param ModuleContextInterface $context
33+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
1934
*/
2035
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
2136
{
22-
$connection = $setup->getConnection();
23-
$this->setWeight('sku', 6, $connection);
24-
$this->setWeight('name', 5, $connection);
37+
$this->setWeight('sku', 6);
38+
$this->setWeight('name', 5);
2539
}
2640

27-
2841
/**
2942
* @param string $attributeCode
3043
* @param int $weight
31-
* @param AdapterInterface $connection
32-
* @internal param $oldWeight
44+
* @internal param AdapterInterface $connection
3345
*/
34-
private function setWeight($attributeCode, $weight, AdapterInterface $connection)
46+
private function setWeight($attributeCode, $weight)
3547
{
36-
$updateQuery = 'UPDATE catalog_eav_attribute SET search_weight = ?'
37-
. ' WHERE attribute_id ='
38-
. ' (SELECT eav_attribute.attribute_id FROM eav_attribute'
39-
. ' LEFT JOIN eav_entity_type ON eav_attribute.entity_type_id = eav_entity_type.entity_type_id'
40-
. ' WHERE eav_entity_type.entity_type_code = ?'
41-
. ' AND eav_attribute.attribute_code = ?)';
42-
$bindings = [$weight, 'catalog_product', $attributeCode];
43-
44-
$connection->query($updateQuery, $bindings);
48+
$attribute = $this->attributeRepository->get($attributeCode);
49+
$attribute->setSearchWeight($weight);
50+
$this->attributeRepository->save($attribute);
4551
}
4652
}

lib/internal/Magento/Framework/Search/Test/Unit/Adapter/Mysql/ScoreBuilderTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ public function testBuild()
4545

4646
$result = $builder->build();
4747

48+
$weightExpression = 'POW(2, ' . ScoreBuilder::WEIGHT_FIELD . ')';
4849
$expected = '((someCondition1 * %1$s + (someCondition2 * %1$s + someCondition3 * %1$s + ' .
4950
'(someCondition4 * %1$s + someCondition5 * %1$s) * 10.1 + (someCondition6 * %1$s + ' .
5051
'someCondition7 * %1$s) * 10.2) * 10.3) * 10.4 + (0)) AS ' . $builder->getScoreAlias();
51-
$expected = sprintf($expected, ScoreBuilder::WEIGHT_FIELD);
52+
$expected = sprintf($expected, $weightExpression);
5253
$this->assertEquals($expected, $result);
5354
}
5455
}

0 commit comments

Comments
 (0)