You are here

public function SearchApiElasticsearchBackend::indexItems in Elasticsearch Connector 8.2

Same name and namespace in other branches
  1. 8.7 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::indexItems()
  2. 8 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 349
Contains the SearchApiElasticsearchBackend object.

Class

SearchApiElasticsearchBackend
Plugin annotation @SearchApiBackend( id = "elasticsearch", label = @Translation("Elasticsearch"), description = @Translation("Index items using an Elasticsearch server.") )

Namespace

Drupal\elasticsearch_connector\Plugin\search_api\backend

Code

public function indexItems(IndexInterface $index, array $items) {
  $elastic_type_exists = $this
    ->doesTypeExists($index);
  if (empty($elastic_type_exists) || empty($items)) {
    return array();
  }
  try {
    $response = $this->client
      ->bulk(IndexFactory::bulkIndex($index, $items));

    // If error throw the error we have.
    if (!empty($response['errors'])) {
      foreach ($response['items'] as $item) {
        if (!empty($item['index']['status']) && $item['index']['status'] == '400') {
          $this->logger
            ->error($item['index']['error']['reason'] . '. ' . $item['index']['error']['caused_by']['reason']);
        }
      }
      throw new SearchApiException($this
        ->t('An error occurred during indexing. Check your watchdog for more information.'));
    }
  } catch (ElasticsearchException $e) {
    drupal_set_message($e
      ->getMessage(), 'error');
  }
  return array_keys($items);
}