You are here

public function SearchApiElasticsearchElastica::indexItems in Search API Elasticsearch 7

Overrides indexItems().

Overrides SearchApiServiceInterface::indexItems

File

modules/elastica/includes/SearchApiElasticsearchElastica.inc, line 249
Provides Elastica client for Search API Elasticsearch.

Class

SearchApiElasticsearchElastica
Search API Elasticsearch Elastica service class.

Code

public function indexItems(SearchApiIndex $index, array $items) {
  $elastica_type = $this
    ->getElasticaType($index);
  if (empty($elastica_type) || empty($items)) {
    return array();
  }
  $documents = array();
  foreach ($items as $id => $fields) {
    $data = array(
      'id' => $id,
    );
    foreach ($fields as $field_id => $field_data) {
      if (isset($field_data['value']) && is_array($field_data['value'])) {
        $data[$field_id] = array();
        foreach ($field_data['value'] as $token) {
          if (is_array($token) && isset($token['value'])) {
            $data[$field_id][] = $token['value'];
          }
          else {
            $data[$field_id][] = $token;
          }
        }
      }
      else {
        $data[$field_id] = $field_data['value'];
      }
    }
    $documents[] = new Elastica\Document($id, $data);
  }

  // Allow other modules to alter documents.
  drupal_alter('search_api_elasticsearch_elastica_documents', $documents, $index, $items);
  try {
    $elastica_type
      ->addDocuments($documents);
  } catch (Exception $e) {
    watchdog('Elasticsearch', check_plain($e
      ->getMessage()), array(), WATCHDOG_ERROR);
    drupal_set_message(check_plain($e
      ->getMessage()), 'error');
  }
  $elastica_type
    ->getIndex()
    ->refresh();
  return array_keys($items);
}