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.

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.

Indexing is how Magento transforms data such as products, categories, and so on, to improve the performance of your storefront. As data changes, the transformed data must be updated—or reindexed. Magento has a very sophisticated architecture that stores lots of merchant data (including catalog data, prices, users, stores, and so on) in many database tables. To optimize storefront performance, Magento accumulates data into special tables using indexers.

For example, suppose you change the price of an item from $8.99 to $6.99. Magento must reindex the price change to display it on your storefront.

Without indexing, Magento would have to calculate the price of every product on the fly—taking into account shopping cart price rules, bundle pricing, discounts, tier pricing, and so on. Loading the price for a product would take a long time, possibly resulting in cart abandonment.

Overview of creating indexer

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="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Indexer/etc/indexer.xsd">
    <indexer id="mageplaza_helloworld_indexer" view_id="mageplaza_helloworld_indexer" class="Mageplaza\HelloWorld\Model\Indexer\Test">
        <title translate="true">Mageplaza 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.

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

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){

		//code here!
	}

	/*
	 * Will take all of the data and reindex
	 * Will run when reindex via command line
	 */
	public function executeFull(){
		//code here!
	}
   
   
	/*
	 * Works with a set of entity changed (may be massaction)
	 */
	public function executeList(array $ids){
		//code here!
	}
   
   
	/*
	 * Works in runtime for a single entity using plugins
	 */
	public function executeRow($id){
		//code here!
	}
}

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

After this, please refresh the cache and go to System > Index Management from backend to see the result. It will show like this:

Magento 2 Indexing Reindex

Run Reindex by command

Run reindex by command line

php bin/magento indexer:reindex

What you need to know: Instead of manually completing four steps with complex lines of code to create an Indexer Reindex in Magento 2, which can be incredibly time-consuming, you can use the Magento 2 SEO Ultimate extension and Backend Reindex extension from Mageplaza. These extensions simplify the process, allowing you to complete the task quickly and efficiently.


SEO

SEO for M2

Drive more traffic to your online store and boost your rank on search engines

Check it out!


Related Post

Image Description
With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.

People also searched for

  • indexing in magento 2
  • magento 2 custom indexer
  • magento 2 create custom indexer
  • magento 2 indexer
  • magento 2 indexing command
  • indexer in magento 2
  • magento 2 create indexer
  • magento 2 reindex programmatically
  • magento 2 index
  • indexing magento 2
  • magento 2 index management
  • magento 2 cron job for indexing
  • magento 2 disable indexer
  • magento 2 index command
  • magento 2 indexers
  • magento 2 reindex order grid
  • magento 2 mview.xml
  • asynchronous indexing magento 2
  • magento 2 indexer command
  • indexer reindex magento 2
  • indexing in magento
  • magento 2 indexer reindex
  • magento 2 stop indexer
  • magento 2 indexer status
  • we couldn't change indexer(s)' mode because of an error.
  • magento 2 reindex one product programmatically
  • indexer:reindex
  • magento 2 reindex specific index
  • magento indexer reindex
  • magento 2 run indexer
  • magento 2 run indexer programmatically
  • magento 2 reindex products
  • magento 2 indexer stuck
  • magento indexer:reindex
  • 2.3.x, 2.4.x
x