forked from Smile-SA/elasticsuite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix client connection in branch 2.5.x (Smile-SA#763 and Smile-SA#738).
- Loading branch information
Aurélien FOUCRET
committed
Feb 20, 2018
1 parent
30e64bf
commit 007aca7
Showing
5 changed files
with
161 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,19 +14,15 @@ | |
|
||
namespace Smile\ElasticsuiteCore\Client; | ||
|
||
use Smile\ElasticsuiteCore\Api\Client\ClientInterface; | ||
use Smile\ElasticsuiteCore\Api\Client\ClientConfigurationInterface; | ||
use Elasticsearch\ClientBuilder; | ||
use Psr\Log\LoggerInterface; | ||
use Smile\ElasticsuiteCore\Api\Client\ClientInterface; | ||
|
||
/** | ||
* ElasticSearch client implementation. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCore | ||
* @author Aurelien FOUCRET <[email protected]> | ||
* | ||
* @SuppressWarnings(TooManyPublicMethods) | ||
*/ | ||
class Client implements ClientInterface | ||
{ | ||
|
@@ -38,19 +34,12 @@ class Client implements ClientInterface | |
/** | ||
* Constructor. | ||
* | ||
* @param ClientConfigurationInterfaceFactory $clientConfigurationFactory Client configuration factory. | ||
* @param ClientBuilder $clientBuilder ES client builder. | ||
* @param LoggerInterface $logger Logger. | ||
* @param array $options Client options. | ||
* @param ClientConfigurationInterface $clientConfiguration Client configuration factory. | ||
* @param ClientBuilder $clientBuilder ES client builder. | ||
*/ | ||
public function __construct( | ||
\Smile\ElasticsuiteCore\Client\ClientConfigurationFactory $clientConfigurationFactory, | ||
ClientBuilder $clientBuilder, | ||
LoggerInterface $logger, | ||
$options = [] | ||
) { | ||
$clientConfiguration = $clientConfigurationFactory->create(['options' => $options]); | ||
$this->esClient = $this->createClient($clientConfiguration, $clientBuilder, $logger); | ||
public function __construct(ClientConfigurationInterface $clientConfiguration, ClientBuilder $clientBuilder) | ||
{ | ||
$this->esClient = $clientBuilder->build($clientConfiguration->getOptions()); | ||
} | ||
|
||
/** | ||
|
@@ -187,63 +176,4 @@ public function termvectors($params) | |
{ | ||
return $this->esClient->termvectors($params); | ||
} | ||
|
||
/** | ||
* Create an ES Client form configuration. | ||
* | ||
* @param ClientConfigurationInterface $clientConfiguration Client configuration. | ||
* @param ClientBuilder $clientBuilder ES client builder. | ||
* @param LoggerInterface $logger Logger | ||
* | ||
* @return \Elasticsearch\Client | ||
*/ | ||
private function createClient( | ||
ClientConfigurationInterface $clientConfiguration, | ||
ClientBuilder $clientBuilder, | ||
LoggerInterface $logger | ||
) { | ||
$hosts = $this->getHosts($clientConfiguration); | ||
|
||
if (!empty($hosts)) { | ||
$clientBuilder->setHosts($hosts); | ||
} | ||
|
||
if ($clientConfiguration->isDebugModeEnabled()) { | ||
$clientBuilder->setLogger($logger); | ||
} | ||
|
||
return $clientBuilder->build(); | ||
} | ||
|
||
/** | ||
* Return hosts config used to connect to the cluster. | ||
* | ||
* @param ClientConfigurationInterface $clientConfiguration Client configuration. | ||
* | ||
* @return array | ||
*/ | ||
private function getHosts(ClientConfigurationInterface $clientConfiguration) | ||
{ | ||
$hosts = []; | ||
|
||
foreach ($clientConfiguration->getServerList() as $host) { | ||
if (!empty($host)) { | ||
list($hostname, $port) = array_pad(explode(':', $host, 2), 2, 9200); | ||
$currentHostConfig = [ | ||
'host' => $hostname, | ||
'port' => $port, | ||
'scheme' => $clientConfiguration->getScheme(), | ||
]; | ||
|
||
if ($clientConfiguration->isHttpAuthEnabled()) { | ||
$currentHostConfig['user'] = $clientConfiguration->getHttpAuthUser(); | ||
$currentHostConfig['pass'] = $clientConfiguration->getHttpAuthPassword(); | ||
} | ||
|
||
$hosts[] = $currentHostConfig; | ||
} | ||
} | ||
|
||
return $hosts; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
/** | ||
* DISCLAIMER | ||
* | ||
* Do not edit or add to this file if you wish to upgrade Smile Elastic Suite to newer | ||
* versions in the future. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCore | ||
* @author Aurelien FOUCRET <[email protected]> | ||
* @copyright 2016 Smile | ||
* @license Open Software License ("OSL") v. 3.0 | ||
*/ | ||
|
||
namespace Smile\ElasticsuiteCore\Client; | ||
|
||
/** | ||
* ElasticSearch client builder. | ||
* | ||
* @category Smile | ||
* @package Smile\ElasticsuiteCore | ||
* @author Aurelien FOUCRET <[email protected]> | ||
*/ | ||
class ClientBuilder | ||
{ | ||
/** | ||
* @var \Elasticsearch\ClientBuilder | ||
*/ | ||
private $clientBuilder; | ||
|
||
/** | ||
* @var \Psr\Log\LoggerInterface | ||
*/ | ||
private $logger; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $defaultOptions = [ | ||
'servers' => 'localhost:9200', | ||
'enable_http_auth' => false, | ||
'http_auth_user' => null, | ||
'http_auth_pwd' => null, | ||
'is_debug_mode_enabled' => false, | ||
]; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param \Elasticsearch\ClientBuilder $clientBuilder Client builder. | ||
* @param \Psr\Log\LoggerInterface $logger Logger. | ||
*/ | ||
public function __construct(\Elasticsearch\ClientBuilder $clientBuilder, \Psr\Log\LoggerInterface $logger) | ||
{ | ||
$this->clientBuilder = $clientBuilder; | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* Build an ES client from options. | ||
* | ||
* @param array $options Client options. See self::defaultOptions for available options. | ||
* | ||
* @return \Elasticsearch\Client | ||
*/ | ||
public function build($options = []) | ||
{ | ||
$options = array_merge($this->defaultOptions, $options); | ||
|
||
$clientBuilder = $this->clientBuilder->create(); | ||
|
||
$hosts = $this->getHosts($options); | ||
|
||
if (!empty($hosts)) { | ||
$clientBuilder->setHosts($hosts); | ||
} | ||
|
||
if ($options['is_debug_mode_enabled']) { | ||
$clientBuilder->setLogger($logger); | ||
} | ||
|
||
return $clientBuilder->build(); | ||
} | ||
|
||
/** | ||
* Return hosts config used to connect to the cluster. | ||
* | ||
* @param array $options Client options. See self::defaultOptions for available options. | ||
* | ||
* @return array | ||
*/ | ||
private function getHosts($options) | ||
{ | ||
$hosts = []; | ||
|
||
if (is_string($options['servers'])) { | ||
$options['servers'] = explode(',', $options['servers']); | ||
} | ||
|
||
foreach ($options['servers'] as $host) { | ||
if (!empty($host)) { | ||
list($hostname, $port) = array_pad(explode(':', trim($host), 2), 2, 9200); | ||
$currentHostConfig = [ | ||
'host' => $hostname, | ||
'port' => $port, | ||
'scheme' => isset($options['enable_https_mode']) ? 'https' : $options['scheme'] ?? 'http', | ||
]; | ||
|
||
if ($options['enable_http_auth']) { | ||
$currentHostConfig['user'] = $options['http_auth_user']; | ||
$currentHostConfig['pass'] = $options['http_auth_pwd']; | ||
} | ||
|
||
$hosts[] = $currentHostConfig; | ||
} | ||
} | ||
|
||
return $hosts; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters