Magento 2 Indexing

In this article we will learn how to create an Indexer in Magento 2. Indexer is an important feature in Magento 2 Indexing. To understand how to create a Hello World module, you can read it here

We will use the example module Mageplaza_HelloWorld for this exercise. Please check our previous post to know how to create a sample module in Magento 2.

Let’s start to create a custom indexer.

Create Indexer configuration file

This configuration file will define the indexer.

File app/code/Mageplaza/HelloWorld/etc/indexer.xml

<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Indexer/etc/indexer.xsd">
   <indexer id="example_indexer" view_id="example_indexer" class="Mageplaza\HelloWorld\Model\Indexer\Test">
       <title translate="true">HelloWorld Indexer</title>
       <description translate="true">HelloWorld of custom indexer</description>

In this file, we declare a new indexer process with the attribute:

  • The id attribute is used to identify this indexer. You can call it when you want to check status, mode or reindex this indexer by command line.
  • The view_id is the id of view element which will be defined in the mview configuration file.
  • The class attribute is the name to the class which we process indexer method.

The simple magento 2 indexing will have some child elements:

  • The title element is used to define the Title of this when showing in indexer grid.
  • The description element is used to define the Description of this when showing in indexer grid.

Create Mview configuration file

The mview.xml file is used to track database changes for a certain entity and running change handle (execute() method).

File: app/code/Mageplaza/HelloWorld/etc/mview.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Mview/etc/mview.xsd">
   <view id="example_indexer" class="Mageplaza\HelloWorld\Model\Indexer\Test" group="indexer">
           <table name="catalog_product_entity" entity_column="entity_id" />

In this file, we define a view element with an id attribute to call from indexer and a class which contain the execute() method. This method will run when the table in subscriptions is changed.

To declare the table, we use the table name and the column of this table which will be sent to the execute() method. In this example, we declare the table ‘catalog_product_entity’. So whenever one or more products is saved, the execute() method in class “Mageplaza\HelloWorld\Model\Indexer\Test” will be called.

Create Indexer class

Follow the indexer.xml and mview.xml above, we will define an Indexer class for both of them: Mageplaza\HelloWorld\Model\Indexer\Test

File: app/code/Mageplaza/HelloWorld/Model/Indexer/Test.php

namespace Mageplaza\HelloWorld\Model\Indexer;

class Test 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){}
   * Will take all of the data and reindex
   * Will run when reindex via command line
   public function executeFull(){}
   * Works with a set of entity changed (may be massaction)
   public function executeList(array $ids){}
   * Works in runtime for a single entity using plugins
   public function executeRow($id){}

You can write the code to add data to your indexer table in the methods in Indexer class.

Enjoyed the tutorial? Spread it to your friends!


Comments for Magento 2 Indexing

Please leave comments if you have any questions, feedbacks.

You also may like these Magento 2 Extensions

One Step Checkout

10 reviews

Layered Navigation

12 reviews


6 reviews


3 reviews


10 reviews

Zoho CRM

3 reviews

Store Locator

1 review

Better Blog

8 reviews