Hello Everyone,
In this blog, we will learn about how to Create Custom Indexers in Magento 2.
Magento 2 Indexer is a process that creates and maintains optimized data structure to improve the store performance.
By default Magento 2 provides default Indexers.
But sometimes we need to create a custom indexer for specific business needs.
Without wasting your time, let us guide you straight away. Follow the easy step given below to Create Custom Indexer in Magento 2.
STEPS FOR CREATE CUSTOM INDEXER IN MAGENTO 2
Step 1: Create mview.xml file
app/code/Vendor/Extension/etc/mview.xml
<?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="magecurious_custom_indexer" class="Vendor\Extension\Model\Indexer\Indexer" group="indexer">
<subscriptions>
<table name="catalog_product_entity" entity_column="entity_id" />
</subscriptions>
</view>
</config>
Step 2: Create indexer.xml file
app/code/Vendor/Extension/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="magecurious_custom_indexer" view_id="magecurious_custom_indexer" class="Vendor\Extension\Model\Indexer\Indexer">
<title translate="true">Magecurious Custom Indexer</title>
<description translate="true">Magecurious Custom Indexer</description>
</indexer>
</config>
Step 3: Create Indexer.php file
app/code/Vendor/Extension/Model/Indexer/Indexer.php
<?php
namespace Vendor\Extension\Model\Indexer;
class Indexer implements \Magento\Framework\Indexer\ActionInterface, \Magento\Framework\Mview\ActionInterface
{
/*
* Used by mview, allows process indexer in the "Update on schedule" mode
*/
public function execute($ids){
//Execute Method Call When Update on Schedule
}
/*
* Will take all of the data and reindex
* Will run when reindex via command line
*/
public function executeFull(){
//Execute Full Method Call When Re index using command line
}
/*
* Works with a set of entity changed (may be massaction)
*/
public function executeList(array $ids){
//Execute List Method Call When partial indextion by id list
}
/*
* Works in runtime for a single entity using plugins
*/
public function executeRow($id){
//Execute Row Method Call When partial indextion by specific id
}
}
Step 4: Finally run the below commands
$ php bin/magento indexer:reindex
$ php bin/magento cache:clean
$ php bin/magento cache:flush
Step 5: Output:
go to system -> Tools -> Index Management
Final Thoughts:
So this was the easiest way which we have told you in this blog. This is how you can Create Custom Indexer in Magento 2. Hope you liked the blog.
So quickly go to the comment box and tell me how you like this blog?
Stay tuned with us on our site to get new updates of Magento.
Thanks for reading and visiting our site.
