You are here

public function HelpSearch::updateIndex in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/help_topics/src/Plugin/Search/HelpSearch.php \Drupal\help_topics\Plugin\Search\HelpSearch::updateIndex()
  2. 10 core/modules/help_topics/src/Plugin/Search/HelpSearch.php \Drupal\help_topics\Plugin\Search\HelpSearch::updateIndex()

Updates the search index for this plugin.

This method is called every cron run if the plugin has been set as an active search module on the Search settings page (admin/config/search/pages). It allows your module to add items to the built-in search index by calling the index() method on the search.index service class, or to add them to your module's own indexing mechanism.

When implementing this method, your module should index content items that were modified or added since the last run. There is a time limit for cron, so it is advisable to limit how many items you index per run using config('search.settings')->get('index.cron_limit') or with your own setting. And since the cron run could time out and abort in the middle of your run, you should update any needed internal bookkeeping on when items have last been indexed as you go rather than waiting to the end of indexing.

Overrides SearchIndexingInterface::updateIndex

File

core/modules/help_topics/src/Plugin/Search/HelpSearch.php, line 306

Class

HelpSearch
Handles searching for help using the Search module index.

Namespace

Drupal\help_topics\Plugin\Search

Code

public function updateIndex() {

  // Update the list of items to be indexed.
  $this
    ->updateTopicList();

  // Find some items that need to be updated. Start with ones that have
  // never been indexed.
  $limit = (int) $this->searchSettings
    ->get('index.cron_limit');
  $query = $this->database
    ->select('help_search_items', 'hsi');
  $query
    ->fields('hsi', [
    'sid',
    'section_plugin_id',
    'topic_id',
  ]);
  $query
    ->leftJoin('search_dataset', 'sd', 'sd.sid = hsi.sid AND sd.type = :type', [
    ':type' => $this
      ->getType(),
  ]);
  $query
    ->where('sd.sid IS NULL');
  $query
    ->groupBy('hsi.sid')
    ->groupBy('hsi.section_plugin_id')
    ->groupBy('hsi.topic_id')
    ->range(0, $limit);
  $items = $query
    ->execute()
    ->fetchAll();

  // If there is still space in the indexing limit, index items that have
  // been indexed before, but are currently marked as needing a re-index.
  if (count($items) < $limit) {
    $query = $this->database
      ->select('help_search_items', 'hsi');
    $query
      ->fields('hsi', [
      'sid',
      'section_plugin_id',
      'topic_id',
    ]);
    $query
      ->leftJoin('search_dataset', 'sd', 'sd.sid = hsi.sid AND sd.type = :type', [
      ':type' => $this
        ->getType(),
    ]);
    $query
      ->condition('sd.reindex', 0, '<>');
    $query
      ->groupBy('hsi.sid')
      ->groupBy('hsi.section_plugin_id')
      ->groupBy('hsi.topic_id')
      ->range(0, $limit - count($items));
    $items = $items + $query
      ->execute()
      ->fetchAll();
  }

  // Index the items we have chosen, in all available languages.
  $language_list = $this->languageManager
    ->getLanguages(LanguageInterface::STATE_CONFIGURABLE);
  $section_plugins = [];
  $words = [];
  try {
    foreach ($items as $item) {
      $section_plugin_id = $item->section_plugin_id;
      if (!isset($section_plugins[$section_plugin_id])) {
        $section_plugins[$section_plugin_id] = $this
          ->getSectionPlugin($section_plugin_id);
      }
      if (!$section_plugins[$section_plugin_id]) {
        $this
          ->removeItemsFromIndex($item->sid);
        continue;
      }
      $section_plugin = $section_plugins[$section_plugin_id];
      $this->searchIndex
        ->clear($this
        ->getType(), $item->sid);
      foreach ($language_list as $langcode => $language) {
        $topic = $section_plugin
          ->renderTopicForSearch($item->topic_id, $language);
        if ($topic) {

          // Index the title plus body text.
          $text = '<h1>' . $topic['title'] . '</h1>' . "\n" . $topic['text'];
          $words += $this->searchIndex
            ->index($this
            ->getType(), $item->sid, $langcode, $text, FALSE);
        }
      }
    }
  } finally {
    $this->searchIndex
      ->updateWordWeights($words);
  }
}