Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SetInitialSearchWeightForAttributes.php
Go to the documentation of this file.
1 <?php
8 
12 use Magento\Framework\Indexer\IndexerInterfaceFactory;
14 
20 {
24  private $indexerFactory;
25 
29  private $attributeRepository;
30 
34  private $state;
35 
42  public function __construct(
43  IndexerInterfaceFactory $indexerFactory,
44  ProductAttributeRepositoryInterface $attributeRepository,
45  State $state
46  ) {
47  $this->indexerFactory = $indexerFactory;
48  $this->attributeRepository = $attributeRepository;
49  $this->state = $state;
50  }
51 
55  public function apply()
56  {
57  $this->setWeight('sku', 6);
58  $this->setWeight('name', 5);
59  $indexer = $this->indexerFactory->create()->load('catalogsearch_fulltext');
60  $this->state->emulateAreaCode(
61  \Magento\Framework\App\Area::AREA_CRONTAB,
62  function () use ($indexer) {
63  $indexer->reindexAll();
64  }
65  );
66  }
67 
71  public static function getDependencies()
72  {
73  return [];
74  }
75 
79  public static function getVersion()
80  {
81  return '2.0.0';
82  }
83 
87  public function getAliases()
88  {
89  return [];
90  }
91 
99  private function setWeight($attributeCode, $weight)
100  {
101  $attribute = $this->attributeRepository->get($attributeCode);
102  $attribute->setSearchWeight($weight);
103  $this->attributeRepository->save($attribute);
104  }
105 }
$attributeCode
Definition: extend.phtml:12
__construct(IndexerInterfaceFactory $indexerFactory, ProductAttributeRepositoryInterface $attributeRepository, State $state)