Magento 2: How to create new indexer for your custom module

For this tutorial we will consider an example of a custom module called Vendor_ModuleName. Create below files to generate a new VSBridge indexer and Elastic Index for your own module:

Vendor_ModuleName\etc\indexer.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* @author Reena Parekh <reena1.parekh@gmail.com>
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Indexer/etc/indexer.xsd">
<indexer id="my_custom_indexer" view_id="my_custom_indexer"
class="Vendor\ModuleName\Model\Indexer\CustomIndexer">
<title translate="true">My Custom Indexer</title>
<description translate="true">Create my new indexer</description>
</indexer>
</config>
view raw indexer.xml hosted with ❤ by GitHub

Vendor_ModuleName\etc\mview.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* @author Reena Parekh <reena1.parekh@gmail.com>
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Mview/etc/mview.xsd">
<view id="my_custom_indexer" class="Vendor\ModuleName\Model\Indexer\CustomIndexer" group="indexer">
<subscriptions>
<table name="my_custom_module_table" entity_column="entity_id"/>
</subscriptions>
</view>
</config>
view raw mview.xml hosted with ❤ by GitHub

Vendor\ModuleName\Model\Indexer\CustomIndexer.php
<?php
/**
* @author Reena Parekh <reena1.parekh@gmail.com>
*/
namespace Vendor\ModuleName\Model\Indexer;
/**
* Class CustomIndexer
*/
class CustomIndexer implements \Magento\Framework\Indexer\ActionInterface, \Magento\Framework\Mview\ActionInterface
{
/**
* Indexer ID in configuration
*/
const INDEXER_ID = 'my_custom_indexer';
/*
* Used by mview, allows process indexer in the "Update on schedule" mode
*/
public function execute($ids){
//Used by mview, allows you to process multiple records with given ids in the "Update on schedule" mode
}
/*
* Will take all of the data and reindex
* Will run when reindex via command line
*/
public function executeFull(){
//Should take into account all records in the system
}
/*
* Works with a set of entity changed (may be massaction)
*/
public function executeList(array $ids){
//Works with a set of records with given ids (mass actions and so on)
}
/*
* Works in runtime for a single entity using plugins
*/
public function executeRow($id){
//Works in runtime for a single record with given id using plugins
}
}

Now, open console and run below command:
rm -rf var/cache var/page_cache generated/ pub/static/ var/di var/view_preprocessed && php bin/magento setup:upgrade && php bin/magento setup:di:compile && php bin/magento setup:static-content:deploy -f
view raw gistfile1.txt hosted with ❤ by GitHub

You will now see following:
  • A new indexer job for your module in Magento admin panel > System > Tools > Index Management
  • A new index in elastic
  • You can refresh your indexer with
    php bin/magento indexer:reindex my_custom_indexer

Comments

Popular posts from this blog

SEO for your Press Releases [Updated 2025 list]

XDebug - PHPStorm - Mac: How to configure Xdebug with PHPStorm on Mac OS and PHP 7.3