Skip to content

Latest commit

 

History

History
74 lines (51 loc) · 3.73 KB

indexing-custom.md

File metadata and controls

74 lines (51 loc) · 3.73 KB
layout group subgroup title menu_title menu_order version level3_menu_node level3_subgroup github_link
default
extension-dev-guide
99_Module Development
Adding a custom indexer
Adding a custom indexer
14
2.0
level3child
index
extension-dev-guide/indexing-custom.md

Adding a custom indexer {#m2devgde-indexing-custom}

This topic discusses how to create a custom indexer. We've recently made a performance improvment that enables you to declare one or more shared indexers; if one of the shared indexes is already up-to-date, it doesn't need to be reindexed.

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

Custom indexer logic

Your custom indexer class should implement \Magento\Framework\Indexer\ActionInterface, 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
  • Rull reindex: processing all entities from a specific dictionary; responsibility of executeFull()

Indexer configuration

In the `etc` directory of your module, add `indexer.xml` with the following:
  • indexer ID
  • indexer class name
  • indexer title
  • indexer description
  • indexer view ID
  • shared indexes, if any

Use the optional shared_index= parameter to improve performance if your indexer is related to another indexer. In this example, if catalog rule product needs to be reindexed, but othe catalog product rule index is up-to-date, then only catalog rule product is reindexed.

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

MView configuration

Add 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:

<script src="https://gist.github.com/xcomSteveJohnson/ef9be4963011bb13efe5.js"></script>

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

<script src="https://gist.github.com/xcomSteveJohnson/5780857cdd5343cafacf.js"></script>

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

<script src="https://gist.github.com/xcomSteveJohnson/4313c5246b38ff8193df.js"></script>

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.