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
Vendor_ModuleName\etc\mview.xml
Vendor\ModuleName\Model\Indexer\CustomIndexer.php
Now, open console and run below command:
You will now see following:
Vendor_ModuleName\etc\indexer.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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>
Vendor_ModuleName\etc\mview.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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>
Vendor\ModuleName\Model\Indexer\CustomIndexer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
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
Post a Comment