Dev Talk

Reindex only required indexers in Magento 2 from CLI

Reindex only required indexers

Don’t you just love it when you change some config option or switch to another branch or something similar and all of a sudden, a warning message pops up in Admin, saying:

One or more indexers are invalid. Make sure your Magento cron job is running.

Well, if you “love” it as much as I do, I’m going to show you how to address the particular indexer(s) without any hassle of knowing which one caused the warning to appear in the first place.

The following example deals with two arbitrary indexers on a project I am working on, but in reality code that will be presented in this article must work on any flagged indexer for indexation. Let’s jump to CLI and run the bin/magento indexer:status command and inspect the output:

+---------------------------------------+----------------------------------------------------+------------------+-----------+---------------------+---------------------+
| ID                                    | Title                                              | Status           | Update On | Schedule Status     | Schedule Updated    |
+---------------------------------------+----------------------------------------------------+------------------+-----------+---------------------+---------------------+
| amasty_xsearch_category_fulltext      | Amasty: Advanced Search  - Categories              | Ready            | Schedule  | idle (0 in backlog) | 2021-05-17 07:18:26 |
| amasty_elastic_relevance_rule_product | Amasty: ElasticSearch - Relevance Rules (Products) | Ready            | Schedule  | idle (0 in backlog) | 2021-05-17 07:18:31 |
| amasty_elastic_relevance_rule_rule    | Amasty: ElasticSearch - Relevance Rules (Rules)    | Ready            | Schedule  | idle (0 in backlog) | 2021-05-17 07:18:29 |
| amasty_elastic_popup_data             | Amasty: Xsearch - Popup Data                       | Ready            | Schedule  | idle (0 in backlog) | 2021-05-17 07:18:31 |
| catalogrule_product                   | Catalog Product Rule                               | Ready            | Schedule  | idle (0 in backlog) | 2021-05-17 07:18:24 |
| catalogrule_rule                      | Catalog Rule Product                               | Ready            | Schedule  | idle (0 in backlog) | 2021-05-17 07:18:19 |
| catalogsearch_fulltext                | Catalog Search                                     | Reindex required | Schedule  | idle (0 in backlog) | 2021-05-17 15:14:56 |
| catalog_category_flat                 | Category Flat Data                                 | Ready            | Schedule  | idle (0 in backlog) | 2021-05-17 07:18:17 |
| catalog_category_product              | Category Products                                  | Reindex required | Schedule  | idle (0 in backlog) | 2021-05-17 15:12:10 |
| customer_grid                         | Customer Grid                                      | Ready            | Schedule  | idle (0 in backlog) | 2021-05-17 07:18:12 |
| design_config_grid                    | Design Config Grid                                 | Ready            | Schedule  | idle (0 in backlog) | 2021-05-17 07:18:32 |
| catalog_product_category              | Product Categories                                 | Ready            | Schedule  | idle (0 in backlog) | 2021-05-17 15:12:10 |
| catalog_product_attribute             | Product EAV                                        | Ready            | Schedule  | idle (0 in backlog) | 2021-05-17 07:18:20 |
| catalog_product_flat                  | Product Flat Data                                  | Ready            | Schedule  | idle (0 in backlog) | 2021-05-17 07:18:15 |
| catalog_product_price                 | Product Price                                      | Ready            | Schedule  | idle (0 in backlog) | 2021-05-17 15:14:31 |
| cataloginventory_stock                | Stock                                              | Ready            | Schedule  | idle (0 in backlog) | 2021-05-17 15:14:33 |
+---------------------------------------+----------------------------------------------------+------------------+-----------+---------------------+---------------------+

In the following scenario you will notice there are two indexers that are flagged for indexing:

  • Catalog Search
  • Category Products

Now that we know which indexers became invalid, the only thing that needs to be done is to find a way to grab the indexer identifier (ID) which is used to run the individual indexer. Of course, without typing these values. Let’s start some bash programming!

Step 1 – grab the list of indexers that need to be run

The previous command needs to be connected to another one using pipe ‘|’, like so:

bin/magento indexer:status | grep required

The command does the following – take whatever output came from bin/magento indexer:status and use it to search for word required. Only if the results are found, it will be displayed back to CLI:

| catalogsearch_fulltext                | Catalog Search                                     | Reindex required | Schedule  | idle (0 in backlog) | 2021-05-17 15:14:56 |
| catalog_category_product              | Category Products                                  | Reindex required | Schedule  | idle (0 in backlog) | 2021-05-17 15:12:10 |

Step 2 – grab the ID value of an indexer

If you presumed you will use another pipe, you are correct. 🙂 In the next part of command, we will split output result (string) into array using awk command using delimiter “|”.

bin/magento indexer:status | grep required | awk -F '|' '{print $2}'

As a result, you should get only the ID of the indexer, one per each row.

catalogsearch_fulltext
catalog_category_product

Step 3 – run the indexers only for the results ID(s)

This is the final step in which we need to take the output of the awk command and run the well known bin/magento indexer:reindex command:

bin/magento indexer:status | grep required | awk -F '|' '{print $2}' | xargs bin/magento indexer:reindex

Once executed in CLI, the following output should be returned to screen:

Category Products index has been rebuilt successfully in 00:00:15
Catalog Search has been rebuilt successfully in 00:00:13

Congratulations, you have successfully indexed whatever indexer was required to be reindexed! 🙂

For those who want more

The final command from step 3 is somewhat short, but still quite long to remember and type each and every time you run into situation like this. That is why aliases come really handy, as they allow you to simplify big and juicy commands into something “human-friendly”, which will save you both time and effort.

Open your .bash_profile or .zshrc or any other config file for the terminal you are using and add the following:

alias m2rr="bin/magento indexer:status | grep required | awk -F '|' '{print \$2}' | xargs bin/magento indexer:reindex"

After you save the file, it is important to the close currently opened terminal and open a new one in order for the new alias to be accessible. Now, if you type the command:

m2rr

the same command shown above will get executed. Neat, isn’t it? 🙂

The alias should work on the following M2 versions:

  • 2.3.5 (thanks to Igor Perkusic for testing on his project)
  • 2.3.6
  • 2.4.2
Read more

Integrating ERP with eCommerce platform

eCommerce & ERP integration featured

ERP (Enterprise Resource Planning) presents the “backbone” of your business and powerful software system designed to integrate and unify the main functional areas of your organization. The benefits of ERP and eCommerce integration are immense when the process is done the right way. What should companies keep in mind when planning the integration of two key systems in their business operations – ERP and eCommerce platform?

Read more

Tips for writing maintainable code

Tips for writing maintainable code featured

If you have ever worked in a team or in any other way had to maintain others people code, you already know how important is to write maintainable code. Actually, this doesn’t need to be others people code at all – if you are trying to modify your year-old-code, you may already forgot all the details around it and it may be hard for you to read it, let alone modify it.

Read more

Extending Twig in Symfony 4

Extending Twig in Symfony 4

In the last article we have covered twig templating basics concepts. Now it’s time to extend functionalities, which can be very useful. Twig has a fair amount of built-in functions, tags, filters and operators but in some cases, you will find that not sufficient. We will cover how to extend twig and what can be extended with examples.

Read more

Unit testing in Magento 2

Unit testing in Magento 2

Magento 2 comes pre-installed with PHPUnit, an automated testing framework for PHP. It is included as one of the dependencies in Magento 2. Covering the basics of PHPUnit is out of the scope of this tutorial, after a short introduction we are going to focus on the practical example of using PHPUnit with Magento 2. For those who are interested in PHPUnit basics, I would recommend reading documentation or tutorials on the web since it is a very well documented topic.

Read more

Symfony templating with Twig

Symfony templating with Twig

Symfony 4, (further in the text – Symfony) for templating provides twig bundle, which is a very useful templating engine. It is built by SensioLabs – the company behind Symfony. Besides Symfony, there are more platforms supporting Twig such as Drupal8, eZPublish, phpBB, Piwik, OroCRM, etc. In this article we will cover the basics and show how to extend them. Twig is by default not installed with Symfony, you need to include symfony/twig-bundle with the composer (composer require symfony/twig-bundle).

Read more

Local development with Valet+

Valet Plus

Development has been rapidly growing. In fact, at this stage, it has grown so much that it puts a lot of pressure on developers to become super-humans. However, the developers should do what they’re best at, and that is writing code without having to struggle with a local setup. Luckily, there are tools that help developers focus more on code and less on the environment.

Read more

Symphony of PHP – Symfony 4

Symphony PHP Inchoo

You have probably heard of PHP framework called Symfony, today I will share my and Inchoo’s experience with this great framework. You can basically do anything in Symfony, but the most important question you should ask yourself is: Should I use Symfony for my application? In order to help you answer the question, I will try to explain basic concepts, give my thoughts and share some experiences in eCommerce development.

Read more

Declare static types to keep bugs away?

Declare static types to keep bugs away?

Introduction

A lot of programmers feel that for any larger projects static types offer a huge benefit.
Readability too, not only validation.
Some argue that tests can replace types, I agree if we are only talking about code validation.
But static types are empowering in other situations too. Reading code is easier since types describe code and fewer assumptions have to be made about inputs and outputs. Basically, additional documentation which is always in sync with the code.

Read more