Magento

How to create custom indexer in Magento 2 ?

Written by Jay Rangnani

Oct 10

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>
  • indexer

Written by Jay Rangnani

Our Magento Store

Do not miss a chance to grab exciting offers on Magento Development at Dolphin Web Solution. We are providing discounts on various Magento services this season. Go and grab yours today at our Magento Store.

Multiple Wishlist for Magento 2

Multiple Wishlist for Magento 2

₹ 3106

Wallet and Reward Points for Magento 2

Wallet and Reward Points for Magento 2

₹ 9476

RMA for Magento 2

RMA for Magento 2

₹ 11865

₹ 14254
Abandoned Cart Email for Magento 2

Abandoned Cart Email for Magento 2

₹ 6291

Simple Details on Configurable Product for Magento 2

Simple Details on Configurable Product for Magento 2

₹ 7883

₹ 9476
Frequently Bought Together for Magento 2

Frequently Bought Together for Magento 2

₹ 5494

₹ 7087

Let's Start Your Project

Get free consultation for your digital product idea to turn it into reality!

Copyright © 2023 DOLPHIN WEB SOLUTION. All rights reserved.

TO TOP