You are here

public function FileSearch::updateIndex in Search File Attachments 8

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/FileSearch.php, line 257

Class

FileSearch
Executes a keyword search for files against {file_managed} database table.

Namespace

Drupal\search_file_attachments\Plugin\Search

Code

public function updateIndex() {

  // Interpret the cron limit setting as the maximum number of files to index
  // per cron run.
  $limit = (int) $this->searchSettings
    ->get('index.cron_limit');
  $result = $this->database
    ->queryRange("SELECT f.fid, MAX(sd.reindex) FROM {file_managed} f LEFT JOIN {search_dataset} sd ON sd.sid = f.fid AND sd.type = :type WHERE sd.sid IS NULL OR sd.reindex <> 0 GROUP BY f.fid ORDER BY MAX (sd.reindex) is null DESC, MAX (sd.reindex) ASC, f.fid ASC", 0, $limit, array(
    ':type' => $this
      ->getPluginId(),
  ), array(
    'target' => 'replica',
  ));
  $fids = $result
    ->fetchCol();
  if (!$fids) {
    return;
  }
  $file_storage = $this->entityManager
    ->getStorage('file');
  foreach ($file_storage
    ->loadMultiple($fids) as $file) {
    $this
      ->indexFile($file);
  }
}