In simple words, Index is a data structure that helps to improve the performance of data searches and queries on a table.
A) Create an indexer class
B) Create etc/indexer.xml file
C) Create etc/mview.xml file
The custom indexer class must implement \Magento\Framework\Indexer\ActionInterface and the indexer should be able to perform three types of operations:
1 ) executeRow($id)
– Processing a single entry from a dictionary.
2 ) executeList($ids)
– Processing a set of dictionary entries
3 ) executeFull()
– Processing all entities from a specific dictionary.
\Magento\Framework\Mview\ActionInterfaceinterface contains the methods that are applied for the Update By Schedule mode of an indexation
<?php namespace Dolphin\CustomIndexer\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) { $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log'); $logger = new \Zend\Log\Logger(); $logger->addWriter($writer); $logger->info('Execute Method Call When Update on Schedule'); } /** * Add code here for execute full indexation */ public function executeFull() { $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log'); $logger = new \Zend\Log\Logger(); $logger->addWriter($writer); $logger->info('Execute Full Method Call When Re index using command line'); } /** * Add code here for execute partial indexation by ID list */ public function executeList(array $ids) { $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log'); $logger = new \Zend\Log\Logger(); $logger->addWriter($writer); $logger->info('Execute List Method Call When partial indextion by id list'); } /** * Add code here for execute partial indexation by ID */ public function executeRow($id) { $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log'); $logger = new \Zend\Log\Logger(); $logger->addWriter($writer); $logger->info('Execute Row Method Call When partial indextion by specific id'); } }
Create the indexer.xml file inside the etc folder of the module. Dolphin\CustomIndexer\etc\indexer.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Indexer/etc/indexer.xsd"> <indexer id="dolphin_customindexer_indexer" view_id="dolphin_customindexer_view" class="Dolphin\CustomIndexer\Model\Indexer"> <title translate="true">Custom Index Title</title> <description translate="true">Custom Index Description</description> </indexer> </config>
<?xml version="1.0" encoding="UTF-8"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Mview/etc/mview.xsd"> <view id="dolphin_customindexer_view" class="Dolphin\CustomIndexer\Model\Indexer" group="indexer"> <subscriptions> <table name="uploadtool_inventory" entity_column="id" /> </subscriptions> </view> </config>