Elasticsearch~2.0 php-无法建立连接-数组到字符串转换错误
在elasticsearch文档之后,我安装了当前的php库ie 2.0,我做到了这一点Elasticsearch~2.0 php-无法建立连接-数组到字符串转换错误,php,elasticsearch,Php,elasticsearch,在elasticsearch文档之后,我安装了当前的php库ie 2.0,我做到了这一点 $hosts = [ // This is effectively equal to: "https://username:password!#$?*abc@foo.com:9200/" [ 'host' => 'foo.com', 'port' => '9200', 'scheme' => 'https', 'user' => 'userna
$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();
但是它从buildConnectionsFromHosts方法引发了数组到字符串的转换错误。我无法建立连接
我检查了代码,发现没有以数组形式提供的代码来处理主机。这是库中的错误还是我遗漏了什么
谢谢。解决方案 主机选项中的“密码”键应替换为“密码” 需要修改库中的ClientBuilder.php文件。以下代码不在elasticsearch php 2.0 ClientBuilder.php文件中,而是在其主分支中 我替换了从主机构建连接的方法
/**
* @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;
}
/**
* @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;
}
并添加了normalizeExtendedHost方法
/**
* @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;
}
/**
* @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;
}
这是elasticsearch网站提供的文档中的配置示例。请看一看这个。如果你真的在遵循指南,在我看来,你正在打开一个https连接,而你的主机没有启用一个。如果这是您在本地使用的实际配置,那么不管怎样,它都不会工作,因为您将无法连接到foo上的elasticsearch。com@Bjorn我知道这个事实,这是我复制的示例配置,因为我无法在这里显示我的实际配置。至少这不会因此引发数组到字符串的转换错误。