具有PHP连接的Elasticsearch无法正常工作

发布于 2025-01-18 03:25:28 字数 1864 浏览 0 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尝试一下

$client = ClientBuilder::create()
    ->setBasicAuthentication('elastic', 'LisRrIul9oMKh2deJkMv')
    ->setCABundle('/etc/elasticsearch/certs/http_ca.crt')
    ->setHosts(['https://localhost:9200'])
    ->build();

$params = [
    'index' => 'learn',
];

$results = $client->search($params);

Try this

$client = ClientBuilder::create()
    ->setBasicAuthentication('elastic', 'LisRrIul9oMKh2deJkMv')
    ->setCABundle('/etc/elasticsearch/certs/http_ca.crt')
    ->setHosts(['https://localhost:9200'])
    ->build();

$params = [
    'index' => 'learn',
];

$results = $client->search($params);
╰ゝ天使的微笑 2025-01-25 03:25:28

您的 ClientBuilder 调用似乎有问题,因为响应表明至少端口设置不起作用(因为它包含 localhost 端口 80)。

尝试在一个 URL 字符串中传递参数:

$hosts = ['https://elastic:LisRrIul9oMKh2deJkMv@localhost:9200'];

要获取索引,您可以使用 cat 而不是 search

$indices = $client->cat()->indices($params);

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 contains localhost Port 80).

Try passing the parameters in one URL string:

$hosts = ['https://elastic:LisRrIul9oMKh2deJkMv@localhost:9200'];

To then get the indices, you can use cat instead of search:

$indices = $client->cat()->indices($params);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文