You are here

public function SearchApiElasticsearchElastica::__construct in Search API Elasticsearch 7

Overrides __construct().

Overrides SearchApiAbstractService::__construct

File

modules/elastica/includes/SearchApiElasticsearchElastica.inc, line 21
Provides Elastica client for Search API Elasticsearch.

Class

SearchApiElasticsearchElastica
Search API Elasticsearch Elastica service class.

Code

public function __construct(SearchApiServer $server) {
  parent::__construct($server);
  if (search_api_elasticsearch_elastica_loaded()) {
    $config = array();
    $count_options = array();
    $options = $this
      ->getOptions();
    if (isset($options) && !empty($options)) {
      $count_options = $options;
      if (isset($count_options['facet_limit'])) {
        unset($count_options['facet_limit']);
      }
      if (count($count_options) > 1) {
        foreach ($count_options as $id => $option) {
          $config[] = $option;
        }
        try {
          $this->elasticaClient = new Elastica\Client(array(
            'servers' => $config,
          ));
        } catch (Exception $e) {
          watchdog('Elasticsearch', check_plain($e
            ->getMessage()), array(), WATCHDOG_ERROR);
          drupal_set_message(check_plain($e
            ->getMessage()), 'error');
          return FALSE;
        }
      }
      else {
        $config = reset($count_options);
        if (module_exists('search_api_facetapi')) {
          if (isset($options['facet_limit'])) {
            $config['facet_limit'] = $options['facet_limit'];
          }
        }
        try {
          $this->elasticaClient = new Elastica\Client($config);
        } catch (Exception $e) {
          watchdog('Elasticsearch', check_plain($e
            ->getMessage()), array(), WATCHDOG_ERROR);
          drupal_set_message(check_plain($e
            ->getMessage()), 'error');
          return FALSE;
        }
      }
      if (module_exists('psr3_watchdog')) {
        $logger = new Psr3Watchdog();
        $logger
          ->setType('Elastica');
        $this->elasticaClient
          ->setLogger($logger);
      }
      if (module_exists('monolog')) {
        $logger = new \Drupal\Monolog\Logger();
        $logger
          ->setName('Elastica');
        $this->elasticaClient
          ->setLogger($logger);
      }
    }
  }
  else {
    watchdog('Elasticsearch', 'Elastica Client does not exist', array(), WATCHDOG_ERROR);
    return FALSE;
  }
}