[ Elasticsearch-PHP] No alive nodes found in your cluster in

Geeks, please accept the hero post of 2021 Microsoft x Intel hacking contest>>>

<?php

    use Elasticsearch\ClientBuilder;

    require 'vendor/autoload.php';

    $client = ClientBuilder::create()->build();
$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => 'my_id',
    'body' => ['testField' => 'abc']
];

$response = $client->index($params);
print_r($response);

25191;-php 1.php

32467;- 26524;

PHP Fatal error: Uncaught Elasticsearch\Common\Exceptions\NoNodesAvailableException: No alive nodes found in your cluster in /usr/local/src/soft-source/vendor/elasticsearch/elasticsearch/src/Elasticsearch/ConnectionPool/StaticNoPingConnectionPool.php:51
Stack trace:
#0 /usr/local/src/soft-source/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Transport.php(71): Elasticsearch\ConnectionPool\StaticNoPingConnectionPool->nextConnection()
#1 /usr/local/src/soft-source/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Transport.php(89): Elasticsearch\Transport->getConnection()
#2 /usr/local/src/soft-source/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php(228): Elasticsearch\Transport->performRequest('PUT', '/my_index/my_ty...', Array, '{"testField":"a...', Array)
#3 /usr/local/src/soft-source/vendor/react/promise/src/FulfilledPromise.php(25): Elasticsearch\Connections\Connection->Elasticsearch\Connections\{closure}(Array)
#4 /usr/local/src/soft-source/vendor/guzzlehttp/ringphp/src/Future/Compl in /usr/local/src/soft-source/vendor/elasticsearch/elasticsearch/src/Elasticsearch/ConnectionPool/StaticNoPingConnectionPool.php on line 51

Solution: you need to specify the IP and port number of the node

<?php

    use Elasticsearch\ClientBuilder;

    require 'vendor/autoload.php';

$host=['192.168.1.142:9200'];
$client = ClientBuilder::create()->setHosts($host)->build();

$params = [
    'index' => 'my_liming',
    'type' => 'my_type',
    'id' => 'my_id',
    'body' => ['testField' => '123213213c']
];

$response = $client->index($params);
print_r($response);
~

Success:

Array
(
  [_index] => my_liming
  [_type] => my_type
  [_id] => my_id
  [_version] => 2
  [_shards] => Array
    (
      [total] => 2
      [successful] => 1
      [failed] => 0
    )

  [created] =>
)

Similar Posts: