Magento 2: How to create a custom indexer?

Magento 2 Create Custom Indexer - SB Dev Blog

Let’s create a custom indexer in Magento 2. We have seen how we can reindex programmatically in Magento 2. We have also invalidated the indexer programmatically.

Now, We are going to create a custom indexer in this post. We need to consider the following three elements to create a custom indexer.

  • Indexer Configuration
  • MView Configuration
  • Indexer Logic

The file indexer.xml contains the indexer configuration. Similarly, the file mview.xml has the view configuration. The class we define inside the indexer.xml and view.xml includes the logic of the indexer.

Create a custom indexer in magento 2.

Create the XML file with the name indexer.xml inside the module’s etc directory.

<?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="sbdevblog_indexer" view_id="sbdevblog_indexer_view"
             class="SbDevBlog\Indexer\Model\Indexer\SbDevBlogIndexer">
        <title translate="true">The Demo Indexer</title>
        <description translate="true">Demo Indexer For Blog</description>
    </indexer>
</config>

The above configuration contains the id of the mview.xml. The mview.xml is responsible for tracking the database activity in the specified table. Based on the DB activity, the indexer status gets updated.

Could you create the mview.xml file inside the module’s etc directory?

<?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="sbdevblog_indexer_view" class="SbDevBlog\Indexer\Model\Indexer\SbDevBlogIndexer" group="indexer">
        <subscriptions>
            <table name="sbdevblog_demo_table" entity_column="sbd_entity_id" />
        </subscriptions>
    </view>
</config>

The subscription tag contains the table. The mview.xml monitors the activities of the table. In this case, it will watch the table sbdevblog_demo_table.

Finally, we must create the class defined in the indexer and mview configuration.

<?php
/**
 * @copyright Copyright (c) sbdevblog (http://www.sbdevblog.com)
 */

namespace SbDevBlog\Indexer\Model\Indexer;

use Magento\Framework\Indexer\ActionInterface;

class SbDevBlogIndexer implements ActionInterface, \Magento\Framework\Mview\ActionInterface
{

    /**
     * @inheritDoc
     */
    public function executeFull()
    {
        // Will take all of the data and reindex
        // Will run when reindex via command line
    }

    /**
     * @inheritDoc
     */
    public function executeList(array $ids)
    {
        //Works with a set of entity changed (E.G massaction)
    }

    /**
     * @inheritDoc
     */
    public function executeRow($id)
    {
        // Works in runtime for a single entity using plugins
    }

    public function execute($ids)
    {
        // Used by mview, allows process indexer in the "Update on schedule" mode
    }
}

The indexer class consists of four basic methods, as given in the above file.

  • executeFull: This function will take all of the data and reindex. This function runs when reindexing via the command line.
  • executeList: This function works with a set of entity changes. For example, Works with a group of entities changed (e.g., mass action).
  • executeRow: This function works in runtime for a single entity using plugins.
  • execute: This function is used by mview and allows the process indexer to run in the “Update on Schedule” mode.

That’s it. It is easy to create a custom indexer. The indexer we made in this post will be visible in the grid of index management.

You may use the index id to re-index this specific indexer by using the command. php bin/magento indexer:reindex sbdevblog_indexer.

We can also see our custom indexer in the result produced by the command php bin/magento indexer:info.

I will share the post for detailed information on the configuration of the indexer and mview.

Thanks for reading our blogs. Use the comment box to share your valuable feedback. Stay Tuned.

Download Source Code

Note: Please verify the code of this blog and the relevant git repository before using it in production.
sb dev blog adobe commece Magento 2

🙂 HAPPY CODING 🙂

One thought on “Magento 2: How to create a custom indexer?

Leave a Reply

Your email address will not be published. Required fields are marked *