<?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
結果node
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和端口號react
<?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); ~
成功:promise
Array ( [_index] => my_liming [_type] => my_type [_id] => my_id [_version] => 2 [_shards] => Array ( [total] => 2 [successful] => 1 [failed] => 0 ) [created] => )