You are here

public static function DESConnector82::getInstance in Elasticsearch Connector 8

Initializes the needed client.

TODO: We need to check the available options for the ClientBuilder and set them after the alter hook.

Parameters

array $hosts: The URLs of the Elasticsearch hosts.

Return value

Client

Overrides DESConnector::getInstance

1 call to DESConnector82::getInstance()
DESConnector::getInstance in src/DESConnector/DESConnector.php
Initializes the needed client.

File

src/DESConnector/DESConnector82.php, line 66
Provides Elasticsearch Client for Drupal's Elasticsearch Connector module.

Class

DESConnector82
Drupal Elasticsearch Interface.

Namespace

Drupal\elasticsearch_connector\DESConnector

Code

public static function getInstance(array $hosts) {
  $hash_hosts = array();
  foreach ($hosts as $host) {
    $hash_hosts[] = $host['url'];
  }
  $hash = md5(implode(':', $hash_hosts));
  if (!isset($instances[$hash])) {
    foreach ($hosts as $host) {
      $cluster_url = self::buildClusterUrl($host['url'], $host['options']);
      $options['hosts'][] = $cluster_url;
    }

    // TODO: Remove this from the abstraction!
    // It should be passed via parameter.
    \Drupal::moduleHandler()
      ->alter('elasticsearch_connector_load_library_options', $options);
    $builder = ClientBuilder::create();
    $builder
      ->setHosts($options['hosts']);
    $instances[$hash] = new DESConnector82($builder
      ->build());
  }
  return $instances[$hash];
}