I am using Drupal 8 Commerce module.

I am using Elastic Search on AWS platform.

From the Admin whenever product is saved, those changes are directly applied on Elastic Search server, but when I try with $product->save() it is not working 100%.

So if I have to update 10 products in Elastic Search how to achieve that programatically?

In particular, I have to update one field only. Because what is happening that, it will update that field value in database but not on Elastic Search.

I found this useful post for Node How To Force Search API To Reindex a Node / an Entity

  • You'll need to describe how you're connecting Drupal8 to ES to get this answered. Custom integration? Search API ES connector? If you're not doing a custom integration, running drupal cron will probably trigger the re-indexing you're looking for.
    – Shawn Conn
    Commented Jul 8, 2020 at 1:39
  • Hi @ShawnConn using Elastic Connector module. Not any custom integration. Cron job is working as expected. But we want to do like when needed
    – Jackson
    Commented Jul 8, 2020 at 2:10
  • If you're using the Search API integration, you could just call the cron handler search_api_cron() directly.
    – Shawn Conn
    Commented Jul 8, 2020 at 3:07
  • So does this will update any of the changes which I have in Drupal database but not in Elastic Search indexing?
    – Jackson
    Commented Jul 8, 2020 at 4:45
  • Without knowledge of the ES connector, I can't say for sure. But if you're using Seach API / ES integration; and see that a manual cron job fixes the issue; then search_api_cron() will execute the same function as it does when cron is ran (minus all other cron hooks that fire in Drupal)
    – Shawn Conn
    Commented Jul 8, 2020 at 19:51

1 Answer 1


So I have some updates answer relies in search_api.module file function search_api_entity_update.

So I am able to achieve for one product. Can loop & do for multiple.

use Drupal\commerce_product\Entity\Product;
use Drupal\search_api\Plugin\search_api\datasource\ContentEntity;

$entity = Product::load(1); // Replace 1 with your product id.

$indexes = ContentEntity::getIndexesForEntity($entity);

if (!$indexes) {

foreach ($indexes as $index) {
  $index->trackItemsUpdated('entity:commerce_product', ['1:en']);

It works like a charm :)

  • So you hacked the code in the search_api.module?
    – leymannx
    Commented Jul 9, 2020 at 8:05
  • Nope. I have take the reference and use it when ever it is required
    – Jackson
    Commented Jul 9, 2020 at 10:27
  • Ah okay!👌 It sounded a bit as if you where hacking the module. ^^
    – leymannx
    Commented Jul 9, 2020 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.