Shopping cart

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.

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

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

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

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

		//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

Step 4: Run a test

Run reindex command

php bin/magento indexer:reindex





Enjoyed the tutorial? Spread it to your friends!

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

Comments for Magento 2 Indexing Reindex

Please leave comments if you have any questions, feedbacks.

Module Development

You also may like these Magento 2 Extensions

One Step Checkout

$199
69 reviews

Layered Navigation

$99
12 reviews

SEO

FREE
no review

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