You are here

function elasticsearch_connector_recreate_index in Elasticsearch Connector 7.5

Recreate an index by droping it and create it again with the old settings.

Parameters

\nodespark\DESConnector\ClientInterface $client: The connection to the Elasticsearch cluster.

string $index_name: The name of the index we want to recreate.

Return value

bool If the recreation was successful or not.

1 call to elasticsearch_connector_recreate_index()
elasticsearch_watchdog_clear_log_submit in modules/elasticsearch_watchdog/elasticsearch_watchdog.admin.inc
Form submission handler for elasticsearch_watchdog_clear_log_form().

File

./elasticsearch_connector.module, line 1012
This module provide an interface to connecting to the elasticsearch cluster and implementing the official Elasticsearch library.

Code

function elasticsearch_connector_recreate_index(\nodespark\DESConnector\ClientInterface $client, $index_name) {

  //
  try {
    $params = array(
      'index' => $index_name,
    );
    if ($client
      ->indices()
      ->exists($params)) {
      $settings = $client
        ->indices()
        ->getSettings($params);
      $mappings = $client
        ->indices()
        ->getMapping($params);
      $client
        ->indices()
        ->delete($params);
      $params['body']['settings']['index']['number_of_shards'] = $settings[$index_name]['settings']['index']['number_of_shards'];
      $params['body']['settings']['index']['number_of_replicas'] = $settings[$index_name]['settings']['index']['number_of_replicas'];
      $params['body']['mappings'] = $mappings[$index_name]['mappings'];
      $res = $client
        ->indices()
        ->create($params);
      if ($client
        ->CheckResponseAck($res)) {
        return TRUE;
      }
    }
    else {
      watchdog('elasticsearch_connector', 'Cannot recreate an index that does not exists.', array(), WATCHDOG_ERROR);
    }
  } catch (Exception $e) {
    watchdog('elasticsearch_connector', $e
      ->getMessage(), array(), WATCHDOG_ERROR);
  }
  return FALSE;
}