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.

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:



Enjoyed the tutorial? Spread it to your friends!

magento-2-module-development
hello-world
indexing

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

$199
10 reviews

Layered Navigation

$149
12 reviews

Affiliate

$149
3 reviews

FAQ

$149
10 reviews

Zoho CRM

$249
3 reviews

Store Locator

$199
3 reviews

Shop By Brand

$99
3 reviews

Better Blog

FREE
8 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