You are here

public function SearchApiElasticsearchBackend::fieldsUpdated in Elasticsearch Connector 8.5

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

Create or re-create the given index's field mapping.

Parameters

\Drupal\search_api\IndexInterface $index: Index to update fields for.

Return value

bool TRUE on success, FALSE otherwise.

1 call to SearchApiElasticsearchBackend::fieldsUpdated()
SearchApiElasticsearchBackend::updateIndex in src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php
Notifies the server that an index attached to it has been changed.

File

src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php, line 418

Class

SearchApiElasticsearchBackend
Elasticsearch Search API Backend definition.

Namespace

Drupal\elasticsearch_connector\Plugin\search_api\backend

Code

public function fieldsUpdated(IndexInterface $index) {
  $params = $this->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($this->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;
}