1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogSearch\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Indexer\IndexerInterfaceFactory;
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
class InstallData implements InstallDataInterface
{
/**
* @var IndexerInterfaceFactory
*/
private $indexerFactory;
/**
* @param IndexerInterfaceFactory $indexerFactory
* @param ProductAttributeRepositoryInterface $attributeRepository
*/
public function __construct(
IndexerInterfaceFactory $indexerFactory,
ProductAttributeRepositoryInterface $attributeRepository
) {
$this->indexerFactory = $indexerFactory;
$this->attributeRepository = $attributeRepository;
}
/**
* Installs data for a module
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$this->setWeight('sku', 6);
$this->setWeight('name', 5);
$this->getIndexer('catalogsearch_fulltext')->reindexAll();
}
/**
* @param string $indexerId
* @return \Magento\Framework\Indexer\IndexerInterface
*/
private function getIndexer($indexerId)
{
return $this->indexerFactory->create()->load($indexerId);
}
/**
* @param string $attributeCode
* @param int $weight
* @return void
*/
private function setWeight($attributeCode, $weight)
{
$attribute = $this->attributeRepository->get($attributeCode);
$attribute->setSearchWeight($weight);
$this->attributeRepository->save($attribute);
}
}