Drupal - How To Force Search API To Reindex a Node / an Entity

You can dive deep into the code of search_api to find solution for issue. See function search_api_entity_update() and search_api_search_api_index_update(). In case reindex number of comment, you can try this code:

// Example reindex after has new comment at node article id 1
$entity = Node::load(1);
$indexes = ContentEntity::getIndexesForEntity($entity);

foreach ($indexes as $index) {
    // Becarefull with indexes. Just for this case.
    // $datasource_id: entity:node (entity type)
    // $updated_item_ids: 1:en (node ID and Language)
    $index->trackItemsUpdated('entity:node', ['1:en']);
}

You can debug in search_api.module to more info.


Same as D7, you do not need to implicitly mark the entity to be reindexed by Search API. It already does the work for you.

In the module file, there is an implementation of hook_entity_update(), which takes care of reindexing the items immediately. Not required to wait for the cron run.

I have created a site on simplytest https://du6hi.ply.st (admin/admin) to confirm.

Tags:

Search

8