In simple words, Index is a data structure that helps to improve the performance of data searches and queries on a table.
Steps to create custom index
A) Create an indexer class
B) Create etc/indexer.xml file
C) Create etc/mview.xml file
A) Create an indexer class
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
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 |
<?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'); } } |
B) Create etc/indexer.xml file
Create the indexer.xml file inside the etc folder of the module.
Dolphin\CustomIndexer\etc\indexer.xml
1 2 3 4 5 6 7 |
<?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> |
C) Create etc/mview.xml file
1 2 3 4 5 6 7 8 |
<?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> |

