You are here

public function SearchApiElasticsearchBackend::removeIndex in Elasticsearch Connector 8.7

Same name and namespace in other branches
  1. 8 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::removeIndex()
  2. 8.2 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::removeIndex()
  3. 8.5 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::removeIndex()
  4. 8.6 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::removeIndex()

Removes an index from this server.

This might mean that the index has been deleted, or reassigned to a different server. If you need to distinguish between these cases, inspect $index->getServerId().

If the index wasn't added to the server previously, the method call should be ignored.

Implementations of this method should also check whether $index->isReadOnly() and don't delete any indexed data if it is.

Parameters

\Drupal\search_api\IndexInterface|string $index: Either an object representing the index to remove, or its ID (if the index was completely deleted).

Throws

\Drupal\search_api\SearchApiException Thrown if an error occurred while removing the index.

Overrides BackendPluginBase::removeIndex

1 call to SearchApiElasticsearchBackend::removeIndex()
SearchApiElasticsearchBackend::deleteAllIndexItems in src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php
Deletes all the items from the index.

File

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

Class

SearchApiElasticsearchBackend
Elasticsearch Search API Backend definition.

Namespace

Drupal\elasticsearch_connector\Plugin\search_api\backend

Code

public function removeIndex($index) {
  $index_entity = $index instanceof IndexInterface ? $index : $this->entityTypeManager
    ->getStorage('search_api_index')
    ->load($index);

  // Do not remove read-only indexes.
  if ($index_entity && $index_entity
    ->isReadOnly()) {
    return;
  }
  $params = $this->indexFactory
    ->index($index);
  try {
    if ($this->client
      ->indices()
      ->exists($params)) {
      $this->client
        ->indices()
        ->delete($params);
    }
  } catch (ElasticsearchException $e) {
    \Drupal::messenger()
      ->addError($e
      ->getMessage());
  }
}