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}
`
CodePudding user response:
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);
CodePudding user response:
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);