You are here

public function SearchApiSolrBackend::indexItems in Search API Solr 8.2

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. 4.x src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::indexItems()

Indexes the specified items.

Parameters

\Drupal\search_api\IndexInterface $index: The search index for which items should be indexed.

\Drupal\search_api\Item\ItemInterface[] $items: An array of items to be indexed, keyed by their item IDs.

Return value

string[] The IDs of all items that were successfully indexed.

Throws

\Drupal\search_api\SearchApiException Thrown if indexing was prevented by a fundamental configuration error.

Overrides BackendSpecificInterface::indexItems

File

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

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) {
  $field_names = $this
    ->getSolrFieldNames($index);
  $connector = $this
    ->getSolrConnector();
  $update_query = $connector
    ->getUpdateQuery();
  $documents = $this
    ->getDocuments($index, $items, $update_query);
  if (!$documents) {
    return [];
  }
  try {
    $update_query
      ->addDocuments($documents);
    $connector
      ->update($update_query);
    $ret = [];
    foreach ($documents as $document) {
      $ret[] = $document
        ->getFields()[$field_names['search_api_id']];
    }
    \Drupal::state()
      ->set('search_api_solr.' . $index
      ->id() . '.last_update', \Drupal::time()
      ->getCurrentTime());
    return $ret;
  } 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);
  }
}