How to create a custom indexer
In order to do it, we will create a custom module. First of all, the registration.php file:
use \Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register
(ComponentRegistrar::MODULE,
'YVendor_NewMod',
__DIR__
);
And the etc/module.xml file:
So, now we define our index. We need to create a etc/indexer.xml file for it:
Custom Index Name
Custom Index Description
In this file, we define a new indexer with attributes:
id is a new unique index identifier. It is necessary particularly for launching reindex via terminal.
view_id is an identifier in the mview.xml file.
class possesses certain processing methods (executeFull, executeList, executeRow)
Simple indexer should contain the following elements:
title of an index that will be displayed in index grid
description of an index that will be displayed in index grid
Then, we create a etc/mview.xml file. It is necessary to detect changes in table objects and turn on indexers.
In this file, we define the identifier of our mview and class where the execute() method will launch if there are any data changes. In the subscriptions node, we define the tables that contain the table name and column which value will be transferred to the execute() method if the value is changed. So when the value of entity_id will be transferred to the YVendor\NewMod\Model\Indexer::execute() method if the values are changed in the yvendor_newmod_soffer table.
In the indexer.xml and mview.xml files, we have defined the YVendor\NewMod\Model\Indexer class where the methods for changing data processing should be. Now, we need to create Model/Indexer.php
namespace YVendor\NewMod\Model;
use Magento\Framework\Indexer\ActionInterface as IndexerInterface;
use Magento\Framework\Mview\ActionInterface as MviewInterface;
class Indexer implements IndexerInterface, MviewInterface
{
/**
* It's used by mview. It will execute when process indexer in "Update on schedule" Mode.
*/
public function execute($ids)
{
}
/**
* Add code here for execute full indexation
*/
public function executeFull()
{
$writer = new \Laminas\Log\Writer\Stream(BP . '/var/log/test.log');
$logger = new \Laminas\Log\Logger();
$logger->addWriter($writer);
$logger->info('Custom Indexer Log message');
/*
* install laminas if not available
* using: composer require laminas/laminas-log
*/
}
/**
* Add code here for execute partial indexation by ID list
*/
public function executeList(array $ids)
{
}
/**
* Add code here for execute partial indexation by ID
*/
public function executeRow($id)
{
// $this->_logger->info('Execute Row Method Call When partial indextion by specific id');
}
}
Now suppose we change data as given below:



