Magento, Magento 2

How to Create Custom Indexer in Magento 2

How to Create Custom Indexer in Magento 2

In this tutorial, Today I will explain to how to create custom indexer in Magento 2. There are by default many indexer provide by Magento 2. If you want to perform some action after action complete and want to update records by command line then, indexer will be helpful.

For indexer, we need to implement \Magento\Framework\Indexer\ActionInterface interface in our Model file. Basically, Indexer should be able to perform with 3 types of action :

  • Row index : For single entity. executeRow($id) method will be call in this indexer process.
  • List index : For processing set of entity. executeList(array $ids) method will be call in this indexer process.
  • Full index : For processing all entities from specific dictionary. executeFull() method will be call in this indexer process.

You may also like this :

Steps to create custom indexer in Magento 2 :

1) First of all, Let’s assume that you have created simple module. Now, Create indexer.xml at app/code/RH/CustomIndexer/etc/ and paste the below code :

<?xml version="1.0"?>
<!--
/**
 * Code standard by : RH
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Indexer/etc/indexer.xsd">
    <indexer id="rh_customindexer_indexer" view_id="rh_customindexer_indexer" class="RH\CustomIndexer\Model\Indexer" shared_index="rh_customindexer_indexer">
        <title translate="true">RH Custom Indexer Title</title>
        <description translate="true">RH Custom Indexer Description</description>
    </indexer>
</config>

Let us see in details all attributes of indexer.xml file :

  • id : unique indexer id.
  • view_id : id of the view element which is defined in mview.xml file
  • class : Add class of indexer method.
  • shared_index : To improve performance if your indexer is related to another indexer.
  • title :  Add title of indexer.
  • description :  Add description of indexer.

2) After that, Create mview.xml file at app/code/RH/CustomIndexer/etc/ and paste the below code to triggers a type of event when data is modified in a database column :

<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
 * Code standard by : RH
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Mview/etc/mview.xsd">
    <view id="rh_customindexer_indexer" class="RH\CustomIndexer\Model\Indexer" group="indexer">
        <subscriptions>
            <table name="rh_custom_indexer_table" entity_column="entity_id" />
        </subscriptions>
    </view>
</config>

In table name, You need to mention table name and primary column name. So, When data will be change in that table, indexer will be display notification.

3) In Last, Create Indexer.php file at app/code/RH/CustomIndexer/Model/ and paste the below code to add code logic for indexer action :

<?php
/**
 * Code standard by : RH
 */
namespace RH\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/Rohan.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/Rohan.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/Rohan.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/Rohan.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info('Execute Row Method Call When partial indextion by specific id');
    }
}

That’s it !!!

Now, Just need to remove generated and clean cache.

Output :

RH Custom Indexer

I hope this blog is easy to understand about how to create custom indexer in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution.

Stay Safe and Stay Connected !!

Tagged ,