The function search_reindex() in Drupal 7 had signature:
search_reindex($sid = NULL, $module = NULL, $reindex = FALSE)
It had two distinctly different functions, depending on its arguments. So, it was removed in Drupal 8, and refactored into two different functions.
Also, for consistency, the order of parameters to search_index() has been changed as well.
- If you call
search_reindex()in Drupal 7 with no arguments, the intent is that it should mark the entire search index for reindexing. In practice, what it does is invokehook_search_reset()on all modules. The only Core implementation of this hook in Drupal 7 isnode_search_reset(), which causes all of the 'node' items in the search index to be marked for reindexing (by setting the update time to the current request time).In Drupal 8, to achieve this functionality, you have two options:
- You can call the
search_mark_for_reindex()function (new to Drupal 8) with no arguments, which will mark everything in the Core search index for reindexing (via the same update time database mechanism used in Drupal 7). - You can call:
$search_page_repository = \Drupal::service('search.search_page_repository'); foreach ($search_page_repository->getIndexableSearchPages() as $entity) { $entity->getPlugin()->markForReindex(); }This will find all active search pages whose plugins have implemented the "indexable" interface, and ask each of these plugins to mark its items for reindexing. This will allow a hypothetical search plugin that uses its own indexing mechanism instead of the core Search index tables to use whatever method it wants to mark its own items for reindexing. The core NodeSearch plugin's method does this by calling
search_mark_for_reindex('node_search'), which uses the same database mechanism as Drupal 7 to update the reindex time in the core search index table.
- You can call the
- If you call
search_reindex($sid, $module)in Drupal 7 and pass in both $sid and $module, you are asking for an item to be removed completely from the search index.To achieve this in Drupal 8, instead call the new function
search_index_clear($type, $sid, $langcode)($langcode can be omitted). For example, to clear the node with ID = 3 from the index:// Drupal 7 search_reindex(3, 'node'); // Drupal 8 search_index_clear('node_search', 3);
Also, the order of parameters to search_index() has been changed to match the new functions. So in Drupal 7, it was search_index($sid, $module, $text), and in Drupal 8 it is now search_index($type, $sid, $langcode, $text) [$type was essentially $module in Drupal 7 and Drupal 7 didn't have $langcode either; this is covered on a previous change notice].