You are here

public function SearchApiSolrBackend::finalizeIndex in Search API Solr 4.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::finalizeIndex()
  2. 8.2 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::finalizeIndex()

Apply any finalization commands to a solr index.

Only if globally configured to do so and only the first time after changes to the index from the drupal side.

Parameters

\Drupal\search_api\IndexInterface $index: The Search API index entity.

Return value

bool TRUE if a finalization run, FALSE otherwise. FALSE doesn't indicate an error!

Throws

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\search_api\SearchApiException

\Drupal\search_api_solr\SearchApiSolrException

Overrides SolrBackendInterface::finalizeIndex

3 calls to SearchApiSolrBackend::finalizeIndex()
SearchApiSolrBackend::executeGraphStreamingExpression in src/Plugin/search_api/backend/SearchApiSolrBackend.php
SearchApiSolrBackend::executeStreamingExpression in src/Plugin/search_api/backend/SearchApiSolrBackend.php
SearchApiSolrBackend::search in src/Plugin/search_api/backend/SearchApiSolrBackend.php
Options on $query prefixed by 'solr_param_' will be passed natively to Solr as query parameter without the prefix. For example you can set the "Minimum Should Match" parameter 'mm' to '75%' like this:

File

src/Plugin/search_api/backend/SearchApiSolrBackend.php, line 1338

Class

SearchApiSolrBackend
Apache Solr backend for search api.

Namespace

Drupal\search_api_solr\Plugin\search_api\backend

Code

public function finalizeIndex(IndexInterface $index) {

  // Avoid endless loops if finalization hooks trigger searches or streaming
  // expressions themselves.
  static $finalization_in_progress = [];
  if ($index
    ->status() && !isset($finalization_in_progress[$index
    ->id()]) && !$index
    ->isReadOnly()) {
    $settings = Utility::getIndexSolrSettings($index);
    if (!empty($settings['finalize']) && \Drupal::state()
      ->get('search_api_solr.' . $index
      ->id() . '.last_update', 0) >= \Drupal::state()
      ->get('search_api_solr.' . $index
      ->id() . '.last_finalization', 0)) {
      $lock = \Drupal::lock();
      $lock_name = 'search_api_solr.' . $index
        ->id() . '.finalization_lock';
      if ($lock
        ->acquire($lock_name)) {
        $vars = [
          '%index_id' => $index
            ->id(),
          '%pid' => getmypid(),
        ];
        $this
          ->getLogger()
          ->debug('PID %pid, Index %index_id: Finalization lock acquired.', $vars);
        $finalization_in_progress[$index
          ->id()] = TRUE;
        $connector = $this
          ->getSolrConnector();
        $previous_query_timeout = $connector
          ->adjustTimeout($connector
          ->getFinalizeTimeout(), SolrConnectorInterface::QUERY_TIMEOUT);
        $previous_index_timeout = $connector
          ->adjustTimeout($connector
          ->getFinalizeTimeout(), SolrConnectorInterface::INDEX_TIMEOUT);
        try {
          if (!empty($settings['commit_before_finalize'])) {
            $this
              ->ensureCommit($index);
          }
          $this->moduleHandler
            ->invokeAll('search_api_solr_finalize_index', [
            $index,
          ]);
          if (!empty($settings['commit_after_finalize'])) {
            $this
              ->ensureCommit($index);
          }
          \Drupal::state()
            ->set('search_api_solr.' . $index
            ->id() . '.last_finalization', \Drupal::time()
            ->getRequestTime());
          $lock
            ->release($lock_name);
          $vars = [
            '%index_id' => $index
              ->id(),
            '%pid' => getmypid(),
          ];
          $this
            ->getLogger()
            ->debug('PID %pid, Index %index_id: Finalization lock released.', $vars);
        } catch (\Exception $e) {
          unset($finalization_in_progress[$index
            ->id()]);
          $lock
            ->release('search_api_solr.' . $index
            ->id() . '.finalization_lock');
          if ($e instanceof StreamException) {
            throw new SearchApiSolrException($e
              ->getMessage() . "\n" . ExpressionBuilder::indent($e
              ->getExpression()), $e
              ->getCode(), $e);
          }
          throw new SearchApiSolrException($e
            ->getMessage(), $e
            ->getCode(), $e);
        }
        unset($finalization_in_progress[$index
          ->id()]);
        $connector
          ->adjustTimeout($previous_query_timeout, SolrConnectorInterface::QUERY_TIMEOUT);
        $connector
          ->adjustTimeout($previous_index_timeout, SolrConnectorInterface::INDEX_TIMEOUT);
        return TRUE;
      }
      if ($lock
        ->wait($lock_name)) {

        // wait() returns TRUE if the lock isn't released within the given
        // timeout (default 30s).
        $vars = [
          '%index_id' => $index
            ->id(),
          '%pid' => getmypid(),
        ];
        $this
          ->getLogger()
          ->debug('PID %pid, Index %index_id: Waited unsuccessfully for finalization lock.', $vars);
        throw new SearchApiSolrException('The search index currently being rebuilt. Try again later.');
      }
      $this
        ->finalizeIndex($index);
    }
  }
  return FALSE;
}