Reading list Switch to dark mode

    How to create custom indexer in magento 2

    Updated 27 April 2023

    In Magento 2, an indexer is a process that creates and maintains an optimized data structure to improve the performance of the store. Magento 2 provides several default indexers, but you can also create custom indexers to add your own business logic to the indexing process.

    For creating your custom indexer use the steps below:

    • Create etc/mview.xml file.
    • Create etc/indexer.xml file.
    • Create indexer class

    Mview

    The mview.xml file is used to track database changes for a certain entity.

    <?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="webkul_custom_indexer" class="Webkul\CustomIndexer\Model\Indexer\Indexer" group="indexer">
           <subscriptions>
               <table name="catalog_product_entity" entity_column="entity_id" />
           </subscriptions>
       </view>
    </config>

    Indexer

    Searching for an experienced
    Magento Company ?
    Find out More

    Declare a new indexer process in the etc/indexer.xml file

    <?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="webkul_custom_indexer" view_id="webkul_custom_indexer" class="Webkul\CustomIndexer\Model\Indexer\Indexer">
           <title translate="true">Custom Indexer</title>
           <description translate="true">Custom Indexer</description>
       </indexer>
    </config>

    Indexer Class

    <?php
    namespace Webkul\CustomIndexer\Model\Indexer;
    
    class Indexer 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){
            //Used by mview, allows you to process multiple placed orders in the "Update on schedule" mode
        }
    
        /*
         * Will take all of the data and reindex
         * Will run when reindex via command line
         */
        public function executeFull(){
            //Should take into account all placed orders in the system
        }
    
        /*
         * Works with a set of entity changed (may be massaction)
         */
        public function executeList(array $ids){
            //Works with a set of placed orders (mass actions and so on)
        }
    
        /*
         * Works in runtime for a single entity using plugins
         */
        public function executeRow($id){
            //Works in runtime for a single order using plugins
        }
    }

    These settings start the \CustomIndexer\Model\Indexer\Indexer::execute method every time run when you change in any product.

    After this, please refresh the cache and go to System > Tools > Index Management through Admin to verify the custom indexer result.

    image-215

    Use the following command to reindex the custom indexer:

    bin/magento indexer:reindex webkul_custom_indexer

    Use the following command to invalidate the custom indexer:

    bin/magento indexer:reset webkul_custom_indexer

    We hope it will help you. Thank you!!

    If any issue or doubt please feel free to mention them in the comment section.

    We would be happy to help. Happy Coding!!

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home