douwen9343 2016-09-22 02:24
浏览 495
已采纳

Elasticsearch~2.0 php - 无法建立连接-array到字符串转换错误

Following the elasticsearch documentation I installed the current php library ie 2.0 and I did this

$hosts = [
  // This is effectively equal to: "https://username:password!#$?*abc@foo.com:9200/"
  [
    'host' => 'foo.com',
    'port' => '9200',
    'scheme' => 'https',
    'user' => 'username',
    'password' => 'password!#$?*abc'
  ],

  // This is equal to "http://localhost:9200/"
  [
    'host' => 'localhost',    // Only host is required
  ]
];

$client = ClientBuilder::create()  // Instantiate a new ClientBuilder
              ->setHosts($hosts)   // Set the hosts
              ->build();

But it is throwing array to string conversion error from the buildConnectionsFromHosts method. I am unable to establish connection.

I inspected the code and found that there is no code to process host given in array form.Is this the bug in the library or I am missing something?

Thank you.

  • 写回答

1条回答 默认 最新

  • douli8428 2016-09-22 22:31
    关注

    The solution

    The "password" key in the host options should be replaced with "pass".

    The ClientBuilder.php file in the library needs to be modified. The below code is not there in elasticsearch-php 2.0 ClientBuilder.php file but is in its master branch.

    I replaced buildConnectionsFromHosts method

    /**
             * @param array $hosts
             *
             * @throws \InvalidArgumentException
             * @return \Elasticsearch\Connections\Connection[]
             */
            private function buildConnectionsFromHosts($hosts)
            {
                if (is_array($hosts) === false) {
                    $this->logger->error("Hosts parameter must be an array of strings, or an array of Connection hashes.");
                    throw new InvalidArgumentException('Hosts parameter must be an array of strings, or an array of Connection hashes.');
                }
    
                $connections = [];
                foreach ($hosts as $host) {
                    if (is_string($host)) {
                        $host = $this->prependMissingScheme($host);
                        $host = $this->extractURIParts($host);
                    } else if (is_array($host)) {
                        $host = $this->normalizeExtendedHost($host);
                    } else {
                        $this->logger->error("Could not parse host: ".print_r($host, true));
                        throw new RuntimeException("Could not parse host: ".print_r($host, true));
                    }
                    $connections[] = $this->connectionFactory->create($host);
                }
    
                return $connections;
            }
    

    and added normalizeExtendedHost method

     /**
         * @param $host
         * @return array
         */
        private function normalizeExtendedHost($host) {
            if (isset($host['host']) === false) {
                $this->logger->error("Required 'host' was not defined in extended format: ".print_r($host, true));
                throw new RuntimeException("Required 'host' was not defined in extended format: ".print_r($host, true));
            }
    
            if (isset($host['scheme']) === false) {
                $host['scheme'] = 'http';
            }
            if (isset($host['port']) === false) {
                $host['port'] = '9200';
            }
            return $host;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何实验stm32主通道和互补通道独立输出
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题