You are here

public function SearchApiElasticsearchBackend::fieldsUpdated 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::fieldsUpdated()
  2. 8.2 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::fieldsUpdated()
  3. 8.5 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::fieldsUpdated()
  4. 8.6 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::fieldsUpdated()

Overrides fieldsUpdated().

1 call to SearchApiElasticsearchBackend::fieldsUpdated()
SearchApiElasticsearchBackend::addIndex in src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php
Overrides addIndex().

File

src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php, line 379
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 fieldsUpdated(IndexInterface $index) {
  $this
    ->connect();
  $params = $this
    ->getIndexParam($index, TRUE);
  $properties = array(
    'id' => array(
      'type' => 'string',
      'index' => 'not_analyzed',
      'include_in_all' => FALSE,
    ),
  );

  // Map index fields.

  /** @var \Drupal\search_api\Item\FieldInterface[] $field_data */
  foreach ($index
    ->getFields() as $field_id => $field_data) {
    $properties[$field_id] = $this
      ->getFieldMapping($field_data);
  }
  try {
    if ($this->elasticsearchClient
      ->getIndices()
      ->existsType($params)) {
      $current_mapping = $this->elasticsearchClient
        ->getIndices()
        ->getMapping($params);
      if (!empty($current_mapping)) {

        // If the mapping exits, delete it to be able to re-create it.
        $this->elasticsearchClient
          ->getIndices()
          ->deleteMapping($params);
      }
    }

    // TODO: We need also:
    // $params['index'] - (Required)
    // ['type'] - The name of the document type
    // ['timeout'] - (time) Explicit operation timeout
    $params['body'][$params['type']]['properties'] = $properties;
    $response = $this->elasticsearchClient
      ->getIndices()
      ->putMapping($params);
    if (!Cluster::elasticsearchCheckResponseAck($response)) {
      drupal_set_message(t('Cannot create the mapping of the fields!'), 'error');
    }
  } catch (\Exception $e) {
    drupal_set_message($e
      ->getMessage(), 'error');
    return FALSE;
  }
  return TRUE;
}