【 Elasticsearch-PHP】No alive nodes found in your cluster in

<?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);

执行 php 1.php 


结果

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

解决: 需要指定节点的ip和端口号

<?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);
~

成功:

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

    [created] => 
)

猜你喜欢

转载自my.oschina.net/u/1377923/blog/740631
今日推荐