You are here

public function SearchApiElasticsearchConnector::removeIndex in Elasticsearch Connector 7

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

Overrides removeIndex().

Overrides SearchApiAbstractService::removeIndex

File

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

Class

SearchApiElasticsearchConnector
Search service class.

Code

public function removeIndex($index) {
  $params = $this
    ->getIndexParam($index, TRUE);
  try {

    // 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');
  }
}