function HelpSearch::updateIndex

Same name in other branches
  1. 8.9.x core/modules/help_topics/src/Plugin/Search/HelpSearch.php \Drupal\help_topics\Plugin\Search\HelpSearch::updateIndex()
  2. 10 core/modules/help/src/Plugin/Search/HelpSearch.php \Drupal\help\Plugin\Search\HelpSearch::updateIndex()
  3. 11.x core/modules/help/src/Plugin/Search/HelpSearch.php \Drupal\help\Plugin\Search\HelpSearch::updateIndex()

Overrides SearchIndexingInterface::updateIndex

File

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

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);
        $this->updateIndexState();
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.