indexer.xml Structure
indexer.xml declares indexers that transform data for fast retrieval.
Basic indexer.xml:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Indexer:etc/indexer.xsd">
<indexer id="vendor_product_tag" view_id="vendor_product_tag_view"
class="Vendor\Module\Model\Indexer\ProductTag" view_type="Magento\Search\Model\SearchEngine">
<title>Product Tags</title>
<description>Index product tags for search</description>
</indexer>
</config>
Indexer attributes:
id— Unique indexer identifierview_id— Associated materialized viewclass— Indexer class implementing IndexerInterfaceview_type— Search engine type
Multiple indexers:
<indexer id="vendor_product_tag" view_id="vendor_product_tag_view"
class="Vendor\Module\Model\Indexer\ProductTag"
view_type="Magento\Search\Model\SearchEngine">
<title>Product Tags</title>
<description>Index product tags</description>
</indexer>
<indexer id="vendor_category_tag" view_id="vendor_category_tag_view"
class="Vendor\Module\Model\Indexer\CategoryTag"
view_type="Magento\Search\Model\SearchEngine">
<title>Category Tags</title>
<description>Index category tags</description>
</indexer>
Indexer Implementation
Indexer classes handle data transformation and indexing logic.
Indexer class:
<?php
namespace Vendor\Module\Model\Indexer;
use Magento\Framework\Indexer\ActionInterface;
use Magento\Framework\Mview\ViewInterface;
class ProductTag implements ActionInterface, ViewInterface
{
public function __construct(
private \Vendor\Module\Model\ResourceModel\Tag\CollectionFactory $tagCollection,
private \Magento\Search\Model\Indexer\IndexerFactory $indexerFactory
) {}
// ActionInterface: Execute full reindex
public function executeFull(): void
{
$tags = $this->tagCollection->create();
foreach ($tags as $tag) {
$this->reindexTag($tag->getId());
}
}
// ActionInterface: Reindex specific entity
public function execute($id): void
{
$this->reindexTag($id);
}
// ActionInterface: Reindex multiple entities
public function executeList(array $ids): void
{
foreach ($ids as $id) {
$this->reindexTag($id);
}
}
// ViewInterface: Subscribe to data changes
public function subscribe(): void
{
$this->mview->subscribe('vendor_tag', [$this, 'execute']);
}
private function reindexTag(int $tagId): void
{
// Reindex logic
}
}
Data provider:
<?php
namespace Vendor\Module\Model\Indexer;
class ProductTagDataProvider
{
public function getTagsForProduct(int $productId): array
{
return $this->tagResource->getTagsByProduct($productId);
}
}
Indexer Modes
Indexers operate in three modes: real-time, schedule, and manual.
Real-time mode:
- Updates index immediately on data change
- Best for frequently accessed data
- Higher resource usage
Schedule mode:
- Updates index via cron job
- Lower resource usage
- Data may be slightly stale
Manual mode:
- Only updates on explicit reindex command
- Best for rarely changed data
- Lowest resource usage
Configuring mode:
<!-- In mview.xml -->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Mview/etc/mview.xsd">
<view id="vendor_product_tag_view">
<subscriptions>
<table name="vendor_tag" entity_column="tag_id"/>
<table name="vendor_tag_product" entity_column="tag_id"/>
</subscriptions>
</view>
</config>
Managing modes:
# Check indexer status
php bin/magento indexer:status
# Set to real-time
php bin/magento indexer:set-mode realtime vendor_product_tag
# Set to schedule
php bin/magento indexer:set-mode schedule vendor_product_tag
# Set to manual
php bin/magento indexer:set-mode manual vendor_product_tag
Reindexing:
# Reindex all
php bin/magento indexer:reindex
# Reindex specific
php bin/magento indexer:reindex vendor_product_tag
# Check if reindex needed
php bin/magento indexer:status
Indexer Debugging and Management
Debugging and managing indexers effectively.
Indexer status commands:
# Show all indexers and their status
php bin/magento indexer:status
# Show specific indexer info
php bin/magento indexer:info vendor_product_tag
# Show indexer dependencies
php bin/magento indexer:info
Common indexer issues:
# Index shows 'UPDATE REQUIRED'
php bin/magento indexer:reindex
# Indexer stuck
php bin/magento indexer:reindex vendor_product_tag
# Performance issues
php bin/magento indexer:set-mode schedule vendor_product_tag
Debugging indexer:
// Check if indexer is working
$indexer = $this->indexerFactory->create()->load('vendor_product_tag');
echo $indexer->getStatus(); // 'valid' or 'invalid'
// Force reindex
$indexer->reindexAll();
Indexer logs:
-- Check reindex history
SELECT * FROM indexer_state WHERE indexer_id = 'vendor_product_tag';
-- Check scheduled changes
SELECT * FROM mview_state WHERE view_id = 'vendor_product_tag_view';
Performance tips:
1. Use schedule mode for non-critical indexers
2. Batch reindex for large datasets
3. Monitor indexer status in production
4. Set up cron for automatic reindexing
Quiz
1. What XML file declares indexers?
2. Which indexer mode updates immediately on data change?
3. How do you reindex all indexers?
4. What does the mview.xml file configure?
Flashcards
Question
What file declares indexers?
Click to reveal answer
Answer
indexer.xml
Question
What are the three indexer modes?
Click to reveal answer
Answer
real-time, schedule, manual
Question
How do you check indexer status?
Click to reveal answer
Answer
php bin/magento indexer:status
Question
What does mview.xml configure?
Click to reveal answer
Answer
Materialized view subscriptions for data change tracking
Question
How do you reindex a specific indexer?
Click to reveal answer
Answer
php bin/magento indexer:reindex indexer_id
Revision Notes
Key Takeaways
- 1. indexer.xml declares indexers with id, view_id, and class
- 2. Indexer classes implement ActionInterface and ViewInterface
- 3. Three modes: real-time (immediate), schedule (cron), manual (explicit)
- 4. mview.xml configures data change subscriptions
- 5. Reindex via php bin/magento indexer:reindex
- 6. Indexer status: valid, invalid, working
Interview Tips
- • Explain the difference between indexer modes
- • Describe how to create a custom indexer
- • Know how to debug and manage indexers
- • Discuss performance implications of each mode
Cheat Sheet
indexer.xml Cheat Sheet
Structure:
<indexer id="vendor_tag" view_id="view_id"
class="Vendor\Module\Model\Indexer\Tag"
view_type="Magento\Search\Model\SearchEngine">
<title>Tag Index</title>
<description>Index tags</description>
</indexer>
Modes:
- realtime: Immediate update
- schedule: Cron-based
- manual: Explicit reindex only
CLI:
- indexer:status — Show status
- indexer:reindex — Reindex all
- indexer:reindex id — Reindex specific
- indexer:set-mode mode id — Change mode