How to create custom indexer in Magento 2?

How to create custom indexer in Magento 2?


This article will help you to add your own indexer in Magento 2. Magento 2 has made performance improvement that now allows you to declare one or more shared indexers. You can add custom indexer using the steps given 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 at 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 Indexer folder inside your module’s model folder.

Example: KiwiCommerce\Demo\Model\Indexer;

After creating Indexer folder create indexer class file inside it.


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="" 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>

3. Create etc/mview.xml file.

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

Example: KiwiCommerce\Demo\etc\mview.xml

<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Mview/etc/mview.xsd">
   <view id="kiwicommerce_indexer" class="KiwiCommerce\Demo\Model\Indexer\CustomIndexer" group="indexer">
           <table name="sales_order" entity_column="entity_id" />

Now when an order is placed, 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 Indexer:

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

Facing issues with Magento 2 flow or required expertise touch, just connect with us.

No Comments

Leave a Reply

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