You are here

public function SolrCommandHelper::finalizeIndexCommand in Search API Solr 4.x

Same name and namespace in other branches
  1. 8.3 src/Utility/SolrCommandHelper.php \Drupal\search_api_solr\Utility\SolrCommandHelper::finalizeIndexCommand()

Finalizes one ore more indexes.

Parameters

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

bool $force: (optional) Force the finalization, even if the index isn't "dirty".

Throws

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\search_api\SearchApiException

\Drupal\search_api_solr\SearchApiSolrException

File

src/Utility/SolrCommandHelper.php, line 80

Class

SolrCommandHelper
Provides functionality to be used by CLI tools.

Namespace

Drupal\search_api_solr\Utility

Code

public function finalizeIndexCommand(array $indexIds = NULL, $force = FALSE) {
  $servers = search_api_solr_get_servers();
  if ($force) {

    // It's important to mark all indexes as "dirty" before the first
    // finalization runs because there might be dependencies between the
    // indexes. Therefor we do the loop two times.
    foreach ($servers as $server) {
      foreach ($server
        ->getIndexes() as $index) {
        if ($index
          ->status() && !$index
          ->isReadOnly() && (!$indexIds || in_array($index
          ->id(), $indexIds))) {
          \Drupal::state()
            ->set('search_api_solr.' . $index
            ->id() . '.last_update', \Drupal::time()
            ->getRequestTime());
        }
      }
    }
  }
  foreach ($servers as $server) {

    /** @var \Drupal\search_api_solr\SolrBackendInterface $backend */
    $backend = $server
      ->getBackend();
    foreach ($server
      ->getIndexes() as $index) {
      if ($index
        ->status() && !$index
        ->isReadOnly() && (!$indexIds || in_array($index
        ->id(), $indexIds))) {
        $backend
          ->finalizeIndex($index);
      }
    }
  }
}