You are here

public function SearchApiElasticsearchBackend::fieldsUpdated 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::fieldsUpdated()
  2. 8 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()
1 call to SearchApiElasticsearchBackend::fieldsUpdated()
SearchApiElasticsearchBackend::addIndex in src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php
Adds a new index to this server.

File

src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php, line 292
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) {
  $params = IndexFactory::index($index, TRUE);
  try {
    if ($this->client
      ->indices()
      ->existsType($params)) {
      $current_mapping = $this->client
        ->indices()
        ->getMapping($params);
      if (!empty($current_mapping)) {
        try {

          // If the mapping exits, delete it to be able to re-create it.
          $this->client
            ->indices()
            ->deleteMapping($params);
        } catch (ElasticsearchException $e) {

          // If the mapping exits, delete the index and recreate it.
          // In Elasticsearch 2.3 it is not possible to delete a mapping,
          // so don't use $this->client->indices()->deleteMapping as doing so
          // will throw an exception.
          $this
            ->removeIndex($index);
          $this
            ->addIndex($index);
        }
      }
    }
    $response = $this->client
      ->indices()
      ->putMapping(IndexFactory::mapping($index));
    if (!$this->client
      ->CheckResponseAck($response)) {
      drupal_set_message(t('Cannot create the mapping of the fields!'), 'error');
    }
  } catch (ElasticsearchException $e) {
    drupal_set_message($e
      ->getMessage(), 'error');
    return FALSE;
  }
  return TRUE;
}