You are here

public function CommandHelper::indexItemsToIndexCommand in Search API 8

Indexes items on one or more indexes.

Parameters

string[]|null $indexIds: (optional) An array of index IDs, or NULL if we should index items for all enabled indexes.

int|null $limit: (optional) The maximum number of items to index, or NULL to index all items.

int|null $batchSize: (optional) The maximum number of items to process per batch, or NULL to index all items at once.

Return value

bool TRUE if indexing for any index was queued, FALSE otherwise.

Throws

\Drupal\search_api\ConsoleException Thrown if an indexing batch process could not be created.

\Drupal\search_api\SearchApiException Thrown if one of the affected indexes had an invalid tracker set.

File

src/Utility/CommandHelper.php, line 276

Class

CommandHelper
Provides functionality to be used by CLI tools.

Namespace

Drupal\search_api\Utility

Code

public function indexItemsToIndexCommand(array $indexIds = NULL, $limit = NULL, $batchSize = NULL) {
  $indexes = $this
    ->loadIndexes($indexIds);
  if (!$indexes) {
    return FALSE;
  }
  $batchSet = FALSE;
  foreach ($indexes as $index) {
    if (!$index
      ->status() || $index
      ->isReadOnly()) {
      continue;
    }
    $tracker = $index
      ->getTrackerInstance();
    $remaining = $tracker
      ->getTotalItemsCount() - $tracker
      ->getIndexedItemsCount();
    if (!$remaining) {
      $this->logger
        ->info($this
        ->t("The index @index is up to date.", [
        '@index' => $index
          ->label(),
      ]));
      continue;
    }
    else {
      $arguments = [
        '@remaining' => $remaining,
        '@limit' => $limit ?: $this
          ->t('all'),
        '@index' => $index
          ->label(),
      ];
      $this->logger
        ->info($this
        ->t("Found @remaining items to index for @index. Indexing @limit items.", $arguments));
    }

    // If we pass NULL, it would be used as "no items". -1 is the correct way
    // to index all items.
    $current_limit = $limit ?: -1;

    // Get the batch size to use for this index (in case none was specified in
    // the command).
    $currentBatchSize = $batchSize;
    if (!$currentBatchSize) {
      $cron_limit = $index
        ->getOption('cron_limit');
      $currentBatchSize = $cron_limit ?: \Drupal::configFactory()
        ->get('search_api.settings')
        ->get('default_cron_limit');
    }

    // Get the number of items to index.
    $current_limit += 0;
    if (!is_int($current_limit) || $current_limit <= 0) {
      $current_limit = $remaining;
    }
    $arguments = [
      '@index' => $index
        ->label(),
      '@limit' => $current_limit,
      '@batch_size' => $currentBatchSize,
    ];
    $this->logger
      ->info($this
      ->t("Indexing a maximum number of @limit items (@batch_size items per batch run) for the index '@index'.", $arguments));

    // Create the batch.
    try {
      IndexBatchHelper::create($index, $currentBatchSize, $current_limit);
      $batchSet = TRUE;
    } catch (SearchApiException $e) {
      throw new ConsoleException($this
        ->t("Couldn't create a batch, please check the batch size and limit parameters."));
    }
  }
  return $batchSet;
}