Skip to content

Commit

Permalink
[KunstmaanNodeSearchBundle] use hosts in useVersion6 check (#2075)
Browse files Browse the repository at this point in the history
  • Loading branch information
bakie authored and Devolicious committed Aug 21, 2018
1 parent f4c5f67 commit b6d2674
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@
*/
class Configuration implements ConfigurationInterface
{
/**
* @var bool
*/
private $useElasticSearchVersion6;


/**
* Configuration constructor.
* @param bool $useElasticSearchVersion6
*/
public function __construct($useElasticSearchVersion6)
{
$this->useElasticSearchVersion6 = $useElasticSearchVersion6;
}

/**
* {@inheritDoc}
*/
Expand All @@ -36,13 +51,13 @@ public function getConfigTreeBuilder()
'boolean',
'binary',
];
if (!ElasticSearchUtil::useVersion6()) {
if (!$this->useElasticSearchVersion6) {
$types[] = 'string';
}

$properties->children()->scalarNode('type')->beforeNormalization()->ifNotInArray($types)->thenInvalid('type must be one of: ' . implode(', ', $types));

if (ElasticSearchUtil::useVersion6()) {
if ($this->useElasticSearchVersion6) {
$properties->children()->booleanNode('fielddata');
$properties->children()->booleanNode('doc_values');
$properties->children()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@
*/
class KunstmaanNodeSearchExtension extends Extension implements PrependExtensionInterface
{
/**
* @var bool
*/
private $useElasticSearchVersion6;

/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$configuration = new Configuration($this->useElasticSearchVersion6);
$config = $this->processConfiguration($configuration, $configs);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
Expand Down Expand Up @@ -49,7 +54,9 @@ public function load(array $configs, ContainerBuilder $container)
*/
public function prepend(ContainerBuilder $container)
{
if (ElasticSearchUtil::useVersion6()) {
$this->useElasticSearchVersion6 = ElasticSearchUtil::useVersion6(array($container->getParameter('kunstmaan_search.hostname').':'.$container->getParameter('searchport')));

if ($this->useElasticSearchVersion6) {
$mapping = [
'mapping' => [
'root_id' => [
Expand Down
12 changes: 8 additions & 4 deletions src/Kunstmaan/NodeSearchBundle/Helper/ElasticSearchUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ final class ElasticSearchUtil
private static $esClientInfo;

/**
* @var array $hosts
*
* @return bool
*/
public static function useVersion6()
public static function useVersion6($hosts = array())
{
if (PHP_MAJOR_VERSION < 7) {
return false;
}

$info = self::getESVersionInfo();
$info = self::getESVersionInfo($hosts);

if (null !== $info) {
$versionParts = explode('.', $info['version']['number']);
Expand All @@ -35,13 +37,15 @@ public static function useVersion6()
}

/**
* @var array $hosts
*
* @return array
*/
private static function getESVersionInfo()
private static function getESVersionInfo($hosts)
{
try {
if (null === self::$esClientInfo) {
$client = ClientBuilder::create()->build();
$client = ClientBuilder::create()->setHosts($hosts)->build();
self::$esClientInfo = $client->info();
}
} catch (NoNodesAvailableException $e) {
Expand Down

0 comments on commit b6d2674

Please sign in to comment.