1

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 Jul 8 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 Jul 8 at 2:10
  • If you're using the Search API integration, you could just call the cron handler search_api_cron() directly. – Shawn Conn Jul 8 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 Jul 8 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 Jul 8 at 19:51
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) {
  return;
}

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

It works like a charm :)

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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