You are here

function elasticsearch_connector_load_library in Elasticsearch Connector 7.2

Same name and namespace in other branches
  1. 7.5 elasticsearch_connector.module \elasticsearch_connector_load_library()
  2. 7 elasticsearch_connector.module \elasticsearch_connector_load_library()

Parameters

object $cluster: The cluster object for which we want to load the Elasticsearch client.

Return value

\nodespark\DESConnector\ClientInterface

6 calls to elasticsearch_connector_load_library()
elasticsearch_connector_cluster_indices_add in ./elasticsearch_connector.admin.inc
Create new index in the cluster with shard settings and other settings.
elasticsearch_connector_cluster_indices_add_submit in ./elasticsearch_connector.admin.inc
Submit the values of index create form.
elasticsearch_connector_cluster_indices_delete_submit in ./elasticsearch_connector.admin.inc
Delete an index.
elasticsearch_connector_get_client_by_id in ./elasticsearch_connector.module
Return the cluster object based on Cluster ID.
elasticsearch_connector_get_cluster_info in ./elasticsearch_connector.module
Return cluster info.

... See full list

File

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

Code

function elasticsearch_connector_load_library($cluster) {
  $clients =& drupal_static(__FUNCTION__, []);
  if (!isset($clients[$cluster->cluster_id])) {
    $clients[$cluster->cluster_id] = FALSE;

    // TODO: Handle cluster connection. This should be accomplished if the setting is enabled.
    // If enabled, discover all the nodes in the cluster initialize the Pool connection.
    if (valid_url($cluster->url)) {
      $options = array(
        'hosts' => array(
          $cluster->url,
        ),
        'curl' => array(
          CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
          CURLOPT_CONNECTTIMEOUT => !empty($cluster->options['timeout']) ? $cluster->options['timeout'] : ELASTICSEARCH_CONNECTOR_DEFAULT_TIMEOUT,
        ),
      );
      if (!empty($cluster->options['use_authentication'])) {
        $options['auth'][$cluster->url] = array(
          'username' => $cluster->options['username'],
          'password' => $cluster->options['password'],
          'method' => $cluster->options['authentication_type'],
        );
      }
      try {
        if (!class_exists('\\nodespark\\DESConnector\\ClientFactory') && module_exists('composer_manager')) {

          // If the class doesn't exists try to explicitly load the classes!
          // The issues is in update.php where search api call this function, but the function
          // has not been invoked.
          composer_manager_register_autoloader();
        }
        if (!class_exists('\\nodespark\\DESConnector\\ClientFactory')) {

          // No library available!
          return FALSE;
        }
        drupal_alter('elasticsearch_connector_load_library_options', $options, $cluster);
        $clients[$cluster->cluster_id] = \nodespark\DESConnector\ClientFactory::create($options);
      } catch (Exception $e) {
        drupal_set_message($e
          ->getMessage(), 'error');
      }
    }
  }
  return $clients[$cluster->cluster_id];
}