You are here

public function PhotosImageSearch::updateIndex in Album Photos 8.5

Same name and namespace in other branches
  1. 6.0.x src/Plugin/Search/PhotosImageSearch.php \Drupal\photos\Plugin\Search\PhotosImageSearch::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

src/Plugin/Search/PhotosImageSearch.php, line 467

Class

PhotosImageSearch
Handles searching for photos_image entities using the Search module index.

Namespace

Drupal\photos\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('photos_image', 'p');
  $query
    ->addField('p', 'id');
  $query
    ->leftJoin('search_dataset', 'sd', 'sd.sid = p.id 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('p.id')
    ->groupBy('p.id')
    ->range(0, $limit);
  $photosImageIds = $query
    ->execute()
    ->fetchCol();
  if (!$photosImageIds) {
    return;
  }
  $photosImageStorage = $this->entityTypeManager
    ->getStorage('photos_image');
  $words = [];
  try {
    foreach ($photosImageStorage
      ->loadMultiple($photosImageIds) as $photosImage) {
      $words += $this
        ->indexPhotosImage($photosImage);
    }
  } finally {
    $this->searchIndex
      ->updateWordWeights($words);
  }
}