You are here

public function SearchApiElasticsearchBackend::indexItems in Elasticsearch Connector 8.7

Same name and namespace in other branches
  1. 8 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::indexItems()
  2. 8.2 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::indexItems()
  3. 8.5 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::indexItems()
  4. 8.6 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::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/SearchApiElasticsearchBackend.php, line 491

Class

SearchApiElasticsearchBackend
Elasticsearch Search API Backend definition.

Namespace

Drupal\elasticsearch_connector\Plugin\search_api\backend

Code

public function indexItems(IndexInterface $index, array $items) {
  if (empty($items)) {
    return [];
  }
  try {
    $response = $this->client
      ->bulk($this->indexFactory
      ->bulkIndex($index, $items));

    // If there were any errors, log them and throw an exception.
    if (!empty($response['errors'])) {
      foreach ($response['items'] as $item) {
        if (!empty($item['index']['status']) && $item['index']['status'] == '400') {
          $this->logger
            ->error('%reason. %caused_by for id: %id', [
            '%reason' => $item['index']['error']['reason'],
            '%caused_by' => $item['index']['error']['caused_by']['reason'],
            '%id' => $item['index']['_id'],
          ]);
        }
      }
      throw new SearchApiException($this
        ->t('An error occurred during indexing. Check your watchdog for more information.'));
    }
  } catch (ElasticsearchException $e) {
    \Drupal::messenger()
      ->addError($e
      ->getMessage());
  }
  return array_keys($items);
}