How to create a custom indexer in Magento 2

How to create a custom indexer in Magento 2

This article will help you to add your own indexer in Magento 2. Magento 2 has made performance improvements that now allow you to declare one or more shared indexers. You can add a custom indexer using the steps below:

  1. Create indexer class
  2. Create etc/indexer.xml file.
  3. Create etc/mview.xml file.

Example of a custom indexer implementation

In order to push best-selling products to the top of a category listing, you need to process the statistics related to sales which will help in changing the product position dynamically.

1. Create indexer class

Your custom indexer class should implement \Magento\Framework\Indexer\ActionInterface and the indexer should be able to perform three types of operations:

  • executeRow($id) – Processing a single entry from a dictionary.
  • executeList($ids) – Processing a set of dictionary entries.
  • executeFull() – Processing all entities from a specific dictionary.

Create an Indexer folder inside your module’s model folder.

Example: KiwiCommerce\Demo\Model\Indexer;

After creating the Indexer folder, create an indexer class file inside it.

Example:

class CustomIndexer implements \Magento\Framework\Indexer\ActionInterface, \Magento\Framework\Mview\ActionInterface
{
	public function executeFull(); //Should take into account all placed orders in the system

	public function executeList($ids); //Works with a set of placed orders (mass actions and so on)

	public function executeRow($id); //Works in runtime for a single order using plugins

	public function execute($ids); //Used by mview, allows you to process multiple placed orders in the "Update on schedule" mode 	
}

2. Create etc/indexer.xml file.

Create the indexer.xml file inside the etc folder of the module.

Example: KiwiCommerce\Demo\etc\indexer.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Indexer/etc/indexer.xsd">
   <indexer id="kiwicommerce_indexer" view_id="kiwicommerce_indexer" class="KiwiCommerce\Demo\Model\Indexer\CustomIndexer">
       <title translate="true">Custom Indexer</title>
       <description translate="true">Custom Indexer Description </description>
   </indexer>
</config>

3. Create etc/mview.xml file.

Next, you need to create an mview.xml file inside the etc folder of the module.

Example: KiwiCommerce\Demo\etc\mview.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Mview/etc/mview.xsd">
   <view id="kiwicommerce_indexer" class="KiwiCommerce\Demo\Model\Indexer\CustomIndexer" group="indexer">
       <subscriptions>
           <table name="sales_order" entity_column="entity_id" />
       </subscriptions>
   </view>
</config>

Now when an order is placed, the custom indexer calculates the sorting order of the products and stores this data in the index table so that it can be used in product displaying logic.

You can verify your index on Index Management.

To check the Indexer:

  • Log in to the Magento Admin.
  • Navigate to System > Index Management.

Facing issues with Magento 2 flow or require expertise and support? Connect with us.

No Comments

Leave a Reply

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