Magento 2 Indexing Reindex

In this article we will learn how to create an Indexer Reindex 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.

Overview of creating indexer

  • Step 1: Create Indexer configuration file
  • Step 2: Create Mview configuration file
  • Step 3: Create Indexer class
  • Step 4: Run a test

Let’s start to create a custom indexer.

Step 1: 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="http://www.w3.org/2001/XMLSchema-instance" 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>
   </indexer>
</config>

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.

Step 2: 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="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Mview/etc/mview.xsd">
   <view id="example_indexer" class="Mageplaza\HelloWorld\Model\Indexer\Test" group="indexer">
       <subscriptions>
           <table name="catalog_product_entity" entity_column="entity_id" />
       </subscriptions>
   </view>
</config>

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.

Step 3: 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

<?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.

Step 4: Run a test

Run reindex command

php bin/magento indexer:reindex


Next tutorial:

Module Development Series



Enjoyed the tutorial? Spread it to your friends!

magento-2-module-development
hello-world
indexing
indexer
featured

Module Development

Comments for Magento 2 Indexing Reindex

Please leave comments if you have any questions, feedbacks.

You also may like these Magento 2 Extensions

One Step Checkout

$199
10 reviews

Layered Navigation

$99
12 reviews

Shop By Brand

$99
3 reviews

Better SEO

FREE
2 reviews

People also searched for:

  • magento 2 indexing
  • magento 2 index
  • magento 2 indexer
  • indexer in magento 2
  • indexing in magento 2
  • magento 2 reindex command line
  • magento 2 reindex from admin
  • reindex magento 2 ssh
  • magento 2 reindex all
  • php bin/magento indexer:reindex
  • magento 2 indexing command line
  • magento 2 indexing not working
  • magento 2 indexing
  • magento 2 indexers are invalid
  • magento 2 indexer not working
  • magento 2 indexer
  • magento 2 index management
  • magento 2 index stuck on processing
  • magento 2 index update on save
  • magento 2 index schedule
  • magento 2 index command
  • magento 2 indexing issues
  • magento 2 indexing problem
  • magento 2 indexing processing
  • magento 2 indexing command line
  • magento 2 indexing programmatically
  • magento 2 indexing using ssh
  • magento 2 indexing stuck
  • magento 2 indexing cron job
  • magento 2 indexing optimization
  • magento 2 indexing extension
  • magento 2 automatic indexing
  • magento 2 asynchronous indexing
  • magento 2 async indexing
  • magento 2 admin indexing
  • magento 2 disable automatic indexing
  • magento 2 indexing code
  • magento 2 cancel indexing
  • magento 2 indexing deadlock
  • magento 2 disable indexing
  • magento 2 delete indexing
  • magento 2 what does indexing do
  • magento 2 fast asynchronous re-indexing download
  • magento 2 reindex
  • reindex magento 2
  • reindex for magento 2
  • magento reindex