No alive nodes found in your cluster

I installed elasticsearch with the help of this link .

require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;

$client = ClientBuilder::create()->setHosts(["localhost:9200"])->build();  
$response = ''; 
try{
    $params = [
        'index' => 'my_index',
        'type' => 'my_type',
        'id' => 'my_id',
        'body' => ['testField' => 'abc']
    ]; 
    $response = $client->indices()->create($params);        
}catch(Exception $e){
    echo "Exception : ".$e->getMessage();
}
print_r($response);
die('End : Elastic Search');

It returns me No alive nodes found in your cluster. When I change the port to 80

$client = ClientBuilder::create()->setHosts(["localhost:80"])->build();

it gave me below error.

Method Not Allowed. The requested method PUT is not allowed for the URL /my_index.

Upvotes: 0

Views: 20938

Answers (5)

check if your network is watching the ip of elastic service, if you are using docker, before composing container, check inside your web app server over shell if you are watching the ip of service and adding the ip in the .env file and execute again the docker compose. in another cases check if you vps o development environment is pinging o watching the ip of elastic service

Upvotes: 2

Mcile
Mcile

Reputation: 128

set breackpoint on vendor\guzzlehttp\ringphp\src\Client\CurlHandler.php on method CurlHandler::_invokeAsArray and see $response also use

$client = ClientBuilder::create()->setHosts([['host' =>self::$CONF['elasticSearchHost'],'port' => '80','scheme'=>'http']])->build();

Upvotes: -1

Paul
Paul

Reputation: 362

Did you try to restart the elasticsearch daemon? Like:

systemctl restart elasticsearch

Then, check if it's running:

systemctl status elasticsearch

If not, check the log files for any exceptions.

Upvotes: 1

Yao Li
Yao Li

Reputation: 2286

If you use Elasticsearch server in docker as the doc, https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html

But it uses a different network (networks: - esnet) with other services and it cannot talk to the application network. After remove the networks setting and it works well.

Upvotes: 0

MosheZada
MosheZada

Reputation: 2399

You mixed between index and document creation, first you need to create the index (like table in regular relational database), and then insert now document

require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;

$indexParams = [
    'index' => 'my_index',
    'body' => [
        'settings' => [
            'number_of_shards' => 5,
            'number_of_replicas' => 1
        ]
    ]
];

$docParams = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => 'my_id',
    'body' => ['testField' => 'abc']
];    

$client = ClientBuilder::create()->setHosts(["localhost:9200"])->build();  
$response = ''; 
try {
    /* Create the index */
    $response = $client->indices()->create($indexParams);
    print_r($response);

    /* put doc in the index */
    $response = $client->index($docParams);
    print_r($response);

} catch(Exception $e) {
    echo "Exception : ".$e->getMessage();
}
die('End : Elastic Search');

Upvotes: -1

Related Questions