如何在 Magento 2 中创建索引器重新索引索引器是Magento 2 索引中的一个重要功能。

我们将使用示例模块 Example_HelloWorld 进行此练习。请查看我们之前的文章,了解如何在 Magento 2 中创建示例模块。

索引是 Magento 转换产品、类别等数据以提高店面性能的方式。当数据发生变化时,必须更新转换后的数据或重新索引。Magento 具有非常复杂的架构,在许多数据库表中存储大量商家数据(包括目录数据、价格、用户、商店等)。为了优化店面性能,Magento 使用索引器将数据累积到特殊表中。

例如,假设您将商品的价格从 8.99 美元更改为 6.99 美元。Magento必须重新索引价格变化才能将其显示在您的店面上。

如果没有索引,Magento 就必须动态计算每种产品的价格——考虑到购物车价格规则、捆绑定价、折扣、分级定价等。加载产品价格需要很长时间,可能会导致购物车放弃。

创建索引器概述

让我们开始创建一个自定义索引器:

  • 第 1 步:创建索引器配置文件
  • 第 2 步:创建Mview配置文件
  • 第 3 步:创建索引器类
  • 第 4 步:运行测试

创建索引器配置文件

该配置文件将定义索引器。

文件app/code/Example/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_helloworld_indexer" view_id="example_helloworld_indexer" class="Example\HelloWorld\Model\Indexer\Test">
        <title translate="true">Example HelloWorld Indexer</title>
        <description translate="true">HelloWorld of custom indexer</description>
    </indexer>
</config>

在此文件中,我们使用以下属性声明一个新的索引器进程:

  • id 属性用于标识该索引器。当您想通过命令行检查状态、模式或重新索引该索引器时,可以调用它。
  • view_id是将在配置文件中定义的视图元素的 id mview
  • class 属性是我们处理索引器方法的类的名称。

简单的Magento 2 索引将有一些子元素:

  • title 元素用于定义在索引器网格中显示时的标题。
  • 描述元素用于定义在索引器网格中显示时的描述。

创建Mview配置文件

mview.xml文件用于跟踪某个实体的数据库更改并运行更改句柄(execute() 方法)。

文件:app/code/Example/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_helloworld_indexer" class="Example\HelloWorld\Model\Indexer\Test" group="indexer">
        <subscriptions>
            <table name="catalog_product_entity" entity_column="entity_id" />
        </subscriptions>
    </view>
</config>

在此文件中,我们定义一个视图元素,该元素具有要从索引器调用的 id 属性和包含该方法的类execute()。当订阅中的表发生更改时,将运行此方法。

为了声明表,我们使用表名和该表的列,它们将被发送到该execute()方法。在此示例中,我们声明了 table catalog_product_entity。因此,每当保存一个或多个产品时,类中的execute()方法Example\HelloWorld\Model\Indexer\Test就会被调用。

创建索引器类

按照上面的内容indexer.xmlmview.xml我们将为它们定义一个 Indexer 类:Example\HelloWorld\Model\Indexer\Test

文件:app/code/Example/HelloWorld/Model/Indexer/Test.php

<?php
namespace Example\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!
	}
}

您可以在 Indexer 类的方法中编写代码以将数据添加到索引器表中。

之后,请刷新缓存并System > Index Management从后端查看结果。

通过命令运行重建索引

通过命令行运行重新索引

php bin/magento indexer:reindex