You are here

public function SearchApiSolrBackend::indexItems 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::indexItems()
  2. 8 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::indexItems()
  3. 8.2 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::indexItems()

Throws

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\search_api\SearchApiException

File

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

Class

SearchApiSolrBackend
Apache Solr backend for search api.

Namespace

Drupal\search_api_solr\Plugin\search_api\backend

Code

public function indexItems(IndexInterface $index, array $items) {
  $ret = [];
  $connector = $this
    ->getSolrConnector();
  $update_query = $connector
    ->getUpdateQuery();
  $documents = $this
    ->getDocuments($index, $items, $update_query);
  if ($documents) {
    $field_names = $this
      ->getSolrFieldNames($index);
    $endpoint = $this
      ->getCollectionEndpoint($index);
    try {
      $update_query
        ->addDocuments($documents);
      $connector
        ->update($update_query, $endpoint);
      foreach ($documents as $document) {

        // We don't use $item->id() because we want have the real value that
        // went into the index.
        $ret[] = $document
          ->getFields()[$field_names['search_api_id']];
      }
    } catch (SearchApiSolrException $e) {
      if ($this->configuration['index_single_documents_fallback_count']) {

        // It might be that a single document caused the exception. Try to index
        // one by one and create a meaningful error message if possible.
        $count = 0;
        foreach ($documents as $document) {
          if ($count++ < $this->configuration['index_single_documents_fallback_count']) {
            $id = $document
              ->getFields()[$field_names['search_api_id']];
            try {
              $update_query = $connector
                ->getUpdateQuery();
              $update_query
                ->addDocument($document);
              $connector
                ->update($update_query, $endpoint);
              $ret[] = $id;
            } catch (\Exception $e) {
              watchdog_exception('search_api_solr', $e, '%type while indexing item %id: @message in %function (line %line of %file).', [
                '%id' => $id,
              ]);

              // We must not throw an exception because we might have indexed
              // some documents successfully now and need to return these ids.
            }
          }
          else {
            break;
          }
        }
      }
      else {
        watchdog_exception('search_api_solr', $e, "%type while indexing: @message in %function (line %line of %file).");
        throw $e;
      }
    } catch (\Exception $e) {
      watchdog_exception('search_api_solr', $e, "%type while indexing: @message in %function (line %line of %file).");
      throw new SearchApiSolrException($e
        ->getMessage(), $e
        ->getCode(), $e);
    }
    if ($ret) {
      \Drupal::state()
        ->set('search_api_solr.' . $index
        ->id() . '.last_update', \Drupal::time()
        ->getCurrentTime());
    }
  }
  return $ret;
}