elasticsearch-phpでBonsai Elasticsearchを叩くとエラー

  • PHP 7.3
  • elasticsearch-php 6.7

elements.heroku.com

HerokuアドオンのBonsai Elasticsearchを使用。

これを追加すると環境変数BONSAI_URL=https://user:pass@xxxxxxxxxxxxx.us-east-1.bonsaisearch.net というような環境変数が追加される。

このエンドポイントに向けて操作すれば良いはずだと思い、

<?php

$client = ClientBuilder::create()
    ->setHosts([
        'https://user:pass@xxxxxxxxxxxxx.us-east-1.bonsaisearch.net'
    ])
     ->build();
$client->indices()->create([
    'index' => 'my_index',
]);

というようなindexを作成するコードを書いたが、動かすと、

Elasticsearch\Common\Exceptions\NoNodesAvailableException  : No alive nodes found in your cluster

とエラーとなる。

PHPクライアントを使わずに、

curl -XPUT https://user:pass@xxxxxxxxxxxxx.us-east-1.bonsaisearch.net/my_index

curlで叩くと正常に作成できる。

いろいろ調べて下記を発見。

github.com

Are you using SSL? @monothorn I suspect your issue may be SSL certificate validation related. The 1.x branch of ES-PHP didn't validate bad certificates, while 2.x does. So that could explain the behavior why downgrading works

とりあえずhttpsをhttpにしてみる。

<?php

$client = ClientBuilder::create()
    ->setHosts([
        'http://user:pass@xxxxxxxxxxxxx.us-east-1.bonsaisearch.net' // httpに変更
    ])
     ->build();
$client->indices()->create([
    'index' => 'my_index',
]);

こうすると成功した。

curl https://user:pass@xxxxxxxxxxxxx.us-east-1.bonsaisearch.net/_cat/indices?v

上記を叩いてindexが作成されていることも確認。

これでいいのか、httpsでやるべきなんじゃないか、という疑問は残るが、一旦記録しておく。