Close [x]

Contents

Introduction to indexing

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鈥攐r 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 $4.99 to $3.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鈥攖aking 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.

Indexing terminology:

Dictionary
Original data entered to the system. Dictionaries are organized in normal form to facilitate maintenance (updating the data).
Index
Representation of the original data for optimized reading and searching. Indexes can contain results of aggregations and various calculations. Index data can be always re-created from a dictionary using a certain algorithm.
Indexer
Object that creates an index.

Create custom indexers

Magento contains several indexers out of the box, but you might want to add your own if your customization requires data searches, which are not optimized by the Magento default indexers.

This topic provides a high level description of how indexing is implemented from a developer鈥檚 point of view, and practical advice of how to add your own indexer.

How Magento implements indexing

The following components are involved in the indexing process:

Component Description
Magento_Indexer Implements:
  • indexer declaration
  • indexer running
  • indexer running mode configuration
  • indexer status
Magento\Framework\Mview
  • Allows tracking database changes for a certain entity (product, category and so on) and running change handler.
  • Emulates the materialized view technology for MySQL using triggers and separate materialization process (provides executing PHP code instead of SQL queries, which allows materializing multiple queries).

Magento_Indexer replaces the Magento 1.x Magento_Index module. Use Magento_Indexer for all new development.

Indexing types

Each index can perform the following types of reindex operations:

  • Full reindex, which means rebuilding all the indexing-related database tables.

    Full reindexing can be caused by a variety of things, including creating a new web store or new customer group.

    You can optionally fully reindex at any time using the command line.

  • Partial reindex, which means rebuilding the database tables only for the things that changed (for example, changing a single product attribute or price).

The type of reindex performed in each particular case depends on the type of changes made in the dictionary or in the system. This dependency is specific for each indexer.

The following figure shows the logic for partial reindexing.

The image displays the partial reindex workflow

Indexing modes

Reindexing can be performed in two modes:

  • Update on Save: index tables are updated immediately after the dictionary data is changed.
  • Update by Schedule: index tables are updated by cron job according to the configured schedule.

To set these options:

  1. Log in to the Magento Admin.
  2. Click System > Index Management.
  3. Select the check box next to each type of indexer to change.
  4. From the Actions list, click the indexing mode.
  5. Click Submit.

The following figure shows an example of setting indexers to Update by Schedule.

Changing indexer modes

You can also reindex from the command line.

How to reindex

You can reindex in any of the following ways:

Magento indexers

Out of the box the Magento system has the following indexers implemented:

Indexer name Indexer method name Indexer class Description
Category products catalog_category_product Magento\Catalog\Model\Indexer\Category\Flat Creates category/products association
Product categories catalog_product_category Magento\Catalog\Model\Indexer\Product\Flat Creates category/products association
Product price catalog_product_price Magento\Catalog\Model\Indexer\Product\Price Pre-calculates product prices
Product entity attribute value catalog_product_attribute Magento\Catalog\Model\Indexer\Category\Product Reorganizes the EAV product structure to flat structure
Stock cataloginventory_stock Magento\CatalogInventory\Model\Indexer\Stock
Catalog rule product catalogrule_rule Magento\CatalogRule\Model\Indexer\Rule\RuleProductIndexer
Catalog product rule catalogrule_product Magento\CatalogRule\Model\Indexer\Product\ProductRuleIndexer
Catalog search catalogsearch_fulltext Magento\CatalogSearch\Model\Indexer\Fulltext

Adding a custom indexer

We strongly recommend you not modify core Magento code; instead, add your own modules.

To implement your own indexer, add the following code in your module:

  • indexer logic
  • indexer configuration
  • MView configuration

There are more details about each of these in the following paragraphs.

Custom indexer logic

Your custom indexer class should implement \Magento\Indexer\ , and the indexer should be able to perform three types of operations:

  • row reindex: processing a single entry from a dictionary; responsibility of executeRow($id)
  • list reindex: processing a set of dictionary entries; responsibility of executeList($ids), where $ids is an array of entity IDs
  • full reindex: processing all entities from a specific dictionary; responsibility of executeFull()

Indexer configuration

In the the etc directory of your module, add indexer.xml with the following:

  • indexer ID
  • indexer class name
  • indexer title
  • indexer description
  • indexer view ID

Example

All indexers related to a module should be declared in one file.

MView configuration

Add the the mview.xml configuration file in the etc module directory, where you declare the following:

  • indexer view ID
  • indexer class
  • the database tables the indexer tracks
  • what column data is sent to the indexer

Example

All Mview declarations related to a module should be declared in one file.

Example of a custom indexer implementation

Suppose you want to push best-selling products to the top of a category listing. This requires processing statistics about sales to change the product position dynamically.

Assuming your module is named <VendorName>_Merchandizing, you must write the appropriate code in the indexer class:

Next, declare the indexer in Merchandizing/etc/indexer.xml:

Finally, declare the indexer view (merchandizsing_popular_order) that tracks sales (Merchandizing/etc/mview.xml):

These settings start <VendorName>\Merchandizing\Model\Indexer\Popular::execute method every time an order is changed.

Now when an order is placed, the Popular Products indexer calculates the sorting order of the products by popularity and stores this data in the index table, so that it can be used in product displaying logic.