You are here

public function SearchApiElasticsearchBackend::indexItems in Elasticsearch Connector 8

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.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()

Overrides indexItems().

Overrides BackendSpecificInterface::indexItems

File

src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php, line 472
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) {
  $this
    ->connect();
  $elastic_type_exists = $this
    ->getElasticsearchTypeExists($index);

  /*
  if (empty($elastic_type_exists) || empty($items)) {
    return array();
  }
  */
  if (empty($items)) {
    return array();
  }

  // TODO: We need to handle the following params as well:
  // ['consistency'] = (enum) Explicit write consistency setting for the operation
  // ['refresh']     = (boolean) Refresh the index after performing the operation
  // ['replication'] = (enum) Explicitly set the replication type
  // ['fields']      = (list) Default comma-separated list of fields to return in the response for updates
  $params = $this
    ->getIndexParam($index, TRUE);

  /** @var \Drupal\search_api\Item\ItemInterface[] $items */
  foreach ($items as $id => $item) {
    $data = array(
      'id' => $id,
    );

    /** @var \Drupal\search_api\Item\FieldInterface $field */
    foreach ($item as $name => $field) {
      $data[$field
        ->getFieldIdentifier()] = $field
        ->getValues();
    }
    $params['body'][] = array(
      'index' => array(
        '_id' => $data['id'],
      ),
    );
    $params['body'][] = $data;
  }
  try {
    $response = $this->elasticsearchClient
      ->bulk($params);

    // 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') {

          // TODO: This foreach maybe is better to return only the indexed items for return
          // instead of throwing an error and stop the process cause we are in bulk
          // and some of the items can be indexed successfully.
          throw new SearchApiException($item['index']['error']['reason'] . '. ' . $item['index']['error']['caused_by']['reason']);
        }
      }
    }
  } catch (\Exception $e) {
    drupal_set_message($e
      ->getMessage(), 'error');
  }
  return array_keys($items);
}