You are here

public function SearchApiElasticsearchConnector::removeIndex in Elasticsearch Connector 7.2

Same name and namespace in other branches
  1. 7.5 modules/elasticsearch_connector_search_api/service.inc \SearchApiElasticsearchConnector::removeIndex()
  2. 7 modules/elasticsearch_connector_search_api/service.inc \SearchApiElasticsearchConnector::removeIndex()

Overrides removeIndex().

Overrides SearchApiAbstractService::removeIndex

1 call to SearchApiElasticsearchConnector::removeIndex()
SearchApiElasticsearchConnector::deleteItems in modules/elasticsearch_connector_search_api/service.inc
Overrides deleteItems().

File

modules/elasticsearch_connector_search_api/service.inc, line 374
Provides a Elasticsearch-based service class for the Search API using Elasticsearch Connector module.

Class

SearchApiElasticsearchConnector
Search service class.

Code

public function removeIndex($index) {
  try {
    if ($this
      ->versionIs2x()) {
      $params = $this
        ->getIndexParam($index);
      if ($this->elasticsearchClient
        ->indices()
        ->exists($params)) {
        $this->elasticsearchClient
          ->indices()
          ->delete($params);
      }
    }
    else {
      $params = $this
        ->getIndexParam($index, TRUE);

      // If there is no connection there is nothing we can do.
      if (!$this->elasticsearchClient) {
        return;
      }
      $this->elasticsearchClient
        ->indices()
        ->deleteMapping($params);

      // Check if there are any other types in the index before deleting it.
      $safe_delete = FALSE;
      $result = $this->elasticsearchClient
        ->indices()
        ->getMapping(array(
        'index' => $params['index'],
      ));

      // The 'get mapping' API response changed in version 1.4.0.beta1
      // @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/1.4/indices-get-mapping.html
      $version = $this
        ->getClusterVersion();
      if (version_compare($version, '1.4.0', '<')) {
        $safe_delete = empty($result[$params['index']]);
      }
      else {
        $safe_delete = empty($result[$params['index']]['mappings']);
      }
      if ($safe_delete) {
        $response = $this->elasticsearchClient
          ->indices()
          ->delete(array(
          'index' => $params['index'],
        ));
      }
    }
  } catch (Exception $e) {
    drupal_set_message($e
      ->getMessage(), 'error');
  }
}