function NodeSearch::updateIndex

Same name and namespace in other branches
  1. 11.x core/modules/node/src/Plugin/Search/NodeSearch.php \Drupal\node\Plugin\Search\NodeSearch::updateIndex()

File

core/modules/node/src/Plugin/Search/NodeSearch.php, line 459

Class

NodeSearch
Handles searching for node entities using the Search module index.

Namespace

Drupal\node\Plugin\Search

Code

public function updateIndex() {
  // Interpret the cron limit setting as the maximum number of nodes to index
  // per cron run.
  $limit = (int) $this->searchSettings
    ->get('index.cron_limit');
  $query = $this->databaseReplica
    ->select('node', 'n');
  $query->addField('n', 'nid');
  $query->leftJoin('search_dataset', 'sd', '[sd].[sid] = [n].[nid] AND [sd].[type] = :type', [
    ':type' => $this->getPluginId(),
  ]);
  $query->addExpression('CASE MAX([sd].[reindex]) WHEN NULL THEN 0 ELSE 1 END', 'ex');
  $query->addExpression('MAX([sd].[reindex])', 'ex2');
  $query->condition($query->orConditionGroup()
    ->where('[sd].[sid] IS NULL')
    ->condition('sd.reindex', 0, '<>'));
  $query->orderBy('ex', 'DESC')
    ->orderBy('ex2')
    ->orderBy('n.nid')
    ->groupBy('n.nid')
    ->range(0, $limit);
  $nids = $query->execute()
    ->fetchCol();
  if (!$nids) {
    return;
  }
  $node_storage = $this->entityTypeManager
    ->getStorage('node');
  $words = [];
  try {
    foreach ($node_storage->loadMultiple($nids) as $node) {
      $words += $this->indexNode($node);
    }
  } finally {
    $this->searchIndex
      ->updateWordWeights($words);
  }
}

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