How to create custom indexer in Magento 2 ?

Written by Jay Rangnani

Oct 10, 2020

How to create custom indexer in Magento 2 ?

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 indexer in Magento 2

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

<?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

<?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

<?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>

Jay Rangnani

Author

We can help you with

  • Dedicated Team
  • Setup Extended Team
  • Product Development
  • Custom App Development

Schedule a Developer Interview And Get 7 Days Risk-Free Trial

Fill out This Form and one of Our Technical Experts will Contact you Within 12 Hours.

    Google
    |

    4.8

    Google
    |

    4.8

    Google
    |

    4.9

    Google
    |

    4.8

    Google
    |

    4.9

    Copyright © 2024 DOLPHIN WEB SOLUTION. All rights reserved.

    TO TOP