Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify elasticsearch username/password with Elasticsearch-PHP ClientBuilder

I have activated default "elastic" user and set a password for that user. I am using elasticsearch-php to connect and query my elasticsearch. It was working good, but after activating the password I cannot connect using my previous code.

While looking for providing authentication information with the ClientBuilder, I only get configurations regarding ssh connection. Did not find anything how can I use my elasticsearch username and password with the connection.

$hosts = ['127.0.0.1:9200'];
$es_user = 'elastic';
$es_pass = 'MY_PASS';
$elasticsearch = Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();

I am wondering how can I use my username/password in the connection above.

like image 886
Mainuddin Avatar asked Sep 27 '19 01:09

Mainuddin


1 Answers

You can do it with an extended host configuration like this:

$hosts = [
    [
        'host' => '127.0.0.1',
        'port' => '9200',
        'user' => 'elastic',
        'pass' => 'MY_PASS'
    ]
];
$elasticsearch = Elasticsearch\ClientBuilder::create()
                    ->setHosts($hosts)
                    ->build();
like image 85
Val Avatar answered Nov 07 '22 17:11

Val