You are here

function _drush_search_api_batch_indexing_create in Search API 7

Creates and sets a batch for indexing items for a particular index.

Parameters

SearchApiIndex $index: The index for which items should be indexed.

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

int $batch_size: (optional) The number of items to index per batch, or NULL to index all items at once.

Return value

bool TRUE if batch was created, FALSE otherwise.

1 call to _drush_search_api_batch_indexing_create()
drush_search_api_index in ./search_api.drush.inc
Index items.

File

./search_api.drush.inc, line 388
Drush commands for SearchAPI.

Code

function _drush_search_api_batch_indexing_create(SearchApiIndex $index, $limit = NULL, $batch_size = NULL) {

  // Get the number of remaining items to index.
  try {
    $datasource = $index
      ->datasource();
  } catch (SearchApiException $e) {
    drush_log($e
      ->getMessage(), 'error');
    return FALSE;
  }
  $index_status = $datasource
    ->getIndexStatus($index);
  $remaining = $index_status['total'] - $index_status['indexed'];
  if ($remaining <= 0) {
    drush_log(dt("The index !index is up to date.", array(
      '!index' => $index->name,
    )), 'ok');
    return FALSE;
  }

  // Get the number of items to index per batch run.
  if (!isset($batch_size)) {
    $batch_size = empty($index->options['cron_limit']) ? SEARCH_API_DEFAULT_CRON_LIMIT : $index->options['cron_limit'];
  }
  elseif ($batch_size <= 0) {
    $batch_size = $remaining;
  }

  // Get the total number of items to index.
  if (!isset($limit) || !is_int($limit += 0) || $limit <= 0) {
    $limit = $remaining;
  }
  drush_log(dt("Indexing a maximum number of !limit items (!batch_size items per batch run) for the index !index.", array(
    '!index' => $index->name,
    '!limit' => $limit,
    '!batch_size' => $batch_size,
  )), 'ok');

  // Create the batch.
  if (!_search_api_batch_indexing_create($index, $batch_size, $limit, $remaining, TRUE)) {
    drush_log(dt("Couldn't create a batch, please check the batch size and limit parameters."), 'error');
    return FALSE;
  }
  return TRUE;
}