You are here

public function SearchApiElasticsearchConnector::indexItems in Elasticsearch Connector 7.2

Same name and namespace in other branches
  1. 7.5 modules/elasticsearch_connector_search_api/service.inc \SearchApiElasticsearchConnector::indexItems()
  2. 7 modules/elasticsearch_connector_search_api/service.inc \SearchApiElasticsearchConnector::indexItems()

Overrides indexItems().

Overrides SearchApiServiceInterface::indexItems

File

modules/elasticsearch_connector_search_api/service.inc, line 439
Provides a Elasticsearch-based service class for the Search API using Elasticsearch Connector module.

Class

SearchApiElasticsearchConnector
Search service class.

Code

public function indexItems(SearchApiIndex $index, array $items) {
  $elastic_type_exists = $this
    ->getElasticsearchTypeExists($index);
  if (!$elastic_type_exists) {
    $this
      ->fieldsUpdated($index);
  }
  if (empty($items)) {
    return array();
  }
  $params = $this
    ->getIndexParam($index, TRUE);
  $documents = array();
  $params['refresh'] = TRUE;
  foreach ($items as $id => $fields) {
    $data = array(
      'id' => $id,
    );
    foreach ($fields as $field_id => $field_data) {
      $data[$field_id] = $field_data['value'];
    }
    $params['body'][] = array(
      'index' => array(
        '_id' => $this
          ->getSafeId($data['id']),
      ),
    );
    $params['body'][] = $data;
  }

  // Let other modules alter params before sending them to Elasticsearch.
  drupal_alter('elasticsearch_connector_search_api_index_items', $index, $params, $items);
  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') {
          watchdog('Elasticsearch Search API', '<pre>' . print_r($item['index']['error'], TRUE) . '</pre>', array(), WATCHDOG_ERROR);
        }
      }
      throw new SearchApiElasticsearchConnectorException(t('An error occurred during indexing. Check your watchdog for more information.'));
    }
  } catch (Exception $e) {
    throw new SearchApiElasticsearchConnectorException($e
      ->getMessage(), NULL, $e);
  }
  return array_keys($items);
}