You are here

public static function IndexFactory::getIndexName in Elasticsearch Connector 8.7

Same name and namespace in other branches
  1. 8.2 src/ElasticSearch/Parameters/Factory/IndexFactory.php \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Factory\IndexFactory::getIndexName()
  2. 8.5 src/ElasticSearch/Parameters/Factory/IndexFactory.php \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Factory\IndexFactory::getIndexName()
  3. 8.6 src/ElasticSearch/Parameters/Factory/IndexFactory.php \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Factory\IndexFactory::getIndexName()

Helper function. Returns the Elasticsearch name of an index.

Parameters

\Drupal\search_api\IndexInterface $index: Index object.

Return value

string The name of the index on the Elasticsearch server. Includes a prefix for uniqueness, the database name, and index machine name.

3 calls to IndexFactory::getIndexName()
IndexFactory::bulkIndex in src/ElasticSearch/Parameters/Factory/IndexFactory.php
Build parameters to bulk delete indexes.
IndexFactory::create in src/ElasticSearch/Parameters/Factory/IndexFactory.php
Build parameters required to create an index TODO: Add the timeout option.
IndexFactory::index in src/ElasticSearch/Parameters/Factory/IndexFactory.php
Build parameters required to index.

File

src/ElasticSearch/Parameters/Factory/IndexFactory.php, line 230

Class

IndexFactory
Create Elasticsearch Indices.

Namespace

Drupal\elasticsearch_connector\ElasticSearch\Parameters\Factory

Code

public static function getIndexName(IndexInterface $index) {

  // Get index machine name.
  $index_machine_name = is_string($index) ? $index : $index
    ->id();

  // Get prefix and suffix from the cluster if present.
  $cluster_id = $index
    ->getServerInstance()
    ->getBackend()
    ->getCluster();
  $cluster_options = Cluster::load($cluster_id)->options;
  $index_suffix = '';
  if (!empty($cluster_options['rewrite']['rewrite_index'])) {
    $index_prefix = isset($cluster_options['rewrite']['index']['prefix']) ? $cluster_options['rewrite']['index']['prefix'] : '';
    if ($index_prefix && substr($index_prefix, -1) !== '_') {
      $index_prefix .= '_';
    }
    $index_suffix = isset($cluster_options['rewrite']['index']['suffix']) ? $cluster_options['rewrite']['index']['suffix'] : '';
    if ($index_suffix && $index_suffix[0] !== '_') {
      $index_suffix = '_' . $index_suffix;
    }
  }
  else {

    // If a custom rewrite is not enabled, set prefix to db name by default.
    $options = \Drupal::database()
      ->getConnectionOptions();
    $index_prefix = 'elasticsearch_index_' . $options['database'] . '_';
  }
  return strtolower(preg_replace('/[^A-Za-z0-9_]+/', '', $index_prefix . $index_machine_name . $index_suffix));
}