具有PHP连接的Elasticsearch无法正常工作
我正在使用Elasticsearch 8.1
。它在端口9200
上运行。当我从Postman执行时,
https://localhost:9200/learn/_search
具有授权:类型:基本auth
和用户名&密码,我可以看到结果。
但是现在我想使用PHP实现此目标。我在作曲家中安装了“ Elasticsearch/elasticsearch”:“^8.0”
。这是我尝试过的代码。
use Elastic\Elasticsearch\ClientBuilder;
$hosts = [
'host' => 'localhost',
'port' => '9200',
'scheme' => 'https',
'user' => 'elastic',
'pass' => 'LisRrIul9oMKh2deJkMv'
];
$client = ClientBuilder::create()
->setHosts($hosts)
->build();
$params = [
'index' => 'learn',
];
$results = $client->search($params);
echo "<pre>";
print_r($results);
我得到了
`[Thu Mar 31 22:07:09 2022] 127.0.0.1:44396 [500]: GET / - Uncaught Elastic\Elasticsearch\Exception\ClientResponseException: 404 Not Found: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
<hr>
<address>Apache/2.4.41 (Ubuntu) Server at localhost Port 80</address>
</body></html>
in /home/user/Projects/Els/my-apps/esphpsearch/vendor/elasticsearch/elasticsearch/src/Response/Elasticsearch.php:65
Stack trace:
#0 /home/user/Projects/Els/my-apps/esphpsearch/vendor/elasticsearch/elasticsearch/src/Client.php(168): Elastic\Elasticsearch\Response\Elasticsearch->setResponse()
#1 /home/user/Projects/Els/my-apps/esphpsearch/vendor/elasticsearch/elasticsearch/src/Traits/ClientEndpointsTrait.php(1521): Elastic\Elasticsearch\Client->sendRequest()
#2 /home/user/Projects/Els/my-apps/esphpsearch/index.php(24): Elastic\Elasticsearch\Client->search()
#3 {main}
`
I am using Elasticsearch 8.1
. It is running on port 9200
. When I execute from POSTMAN,
https://localhost:9200/learn/_search
with Authorization: Type: Basic Auth
and username & password, I can see the result.
But now I want to implement this using PHP. I have installed "elasticsearch/elasticsearch": "^8.0"
in my composer. This is the code I have tried.
use Elastic\Elasticsearch\ClientBuilder;
$hosts = [
'host' => 'localhost',
'port' => '9200',
'scheme' => 'https',
'user' => 'elastic',
'pass' => 'LisRrIul9oMKh2deJkMv'
];
$client = ClientBuilder::create()
->setHosts($hosts)
->build();
$params = [
'index' => 'learn',
];
$results = $client->search($params);
echo "<pre>";
print_r($results);
I am getting
`[Thu Mar 31 22:07:09 2022] 127.0.0.1:44396 [500]: GET / - Uncaught Elastic\Elasticsearch\Exception\ClientResponseException: 404 Not Found: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
<hr>
<address>Apache/2.4.41 (Ubuntu) Server at localhost Port 80</address>
</body></html>
in /home/user/Projects/Els/my-apps/esphpsearch/vendor/elasticsearch/elasticsearch/src/Response/Elasticsearch.php:65
Stack trace:
#0 /home/user/Projects/Els/my-apps/esphpsearch/vendor/elasticsearch/elasticsearch/src/Client.php(168): Elastic\Elasticsearch\Response\Elasticsearch->setResponse()
#1 /home/user/Projects/Els/my-apps/esphpsearch/vendor/elasticsearch/elasticsearch/src/Traits/ClientEndpointsTrait.php(1521): Elastic\Elasticsearch\Client->sendRequest()
#2 /home/user/Projects/Els/my-apps/esphpsearch/index.php(24): Elastic\Elasticsearch\Client->search()
#3 {main}
`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试一下
Try this
您的 ClientBuilder 调用似乎有问题,因为响应表明至少端口设置不起作用(因为它包含 localhost 端口 80)。
尝试在一个 URL 字符串中传递参数:
要获取索引,您可以使用
cat
而不是search
:There seems to be something wrong with your
ClientBuilder
call, since the response indicates that at least the port setting did not work (as it containslocalhost Port 80
).Try passing the parameters in one URL string:
To then get the indices, you can use
cat
instead ofsearch
: