2018年 10月 10日の投稿一覧

Elastic CloudにElasticsearchPhpでデータ登録 – Error Content-Type header [application/octet-stream] is not supported


$params = [
    'index' => 'test_index',
    'type'  => 'test_type',
    'id'    => 1,
    'body'  => [ "title" => "タイトルだよ" ]
    ];
$response = $client->index($params);
var_dump( $response );

で実行


$php elastic.php
PHP Fatal error:  Uncaught Elasticsearch\Common\Exceptions\BadRequest400Exception: {"error":"Content-Type header [application/octet-stream] is not supported","status":406} in /Users/dp/ElasticsearchPhp/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:669
Stack trace:

ContentTypeがダメか、Curlの時は注意してましたが、Elasticsearch-PHP [6.0] のマニュアルに指定ってあったかな?

引用:https://dev.classmethod.jp/server-side/elasticsearch/elasticsearch-6-breaking-changes/

Elasticsearch への API は Transport通信を除いて、HTTP(s) リクエストによって操作します。Elasticsearch 5系までは自動検出していた Content-Type ヘッダですが、Elasticsearch 6系より指定が必須となりました。

下記のようにClietにCurlのパラメータ指定を追加してOK


$params = [
    'index' => 'test_index',
    'type'  => 'test_type',
    'id'    => 1,
    'body'  => [ "title" => "タイトルだよ" ]
    'client' => [
        'curl'  => [
            CURLOPT_HTTPHEADER => [
                'Content-type: application/json',
            ]
        ]
    ]
];
$response = $client->index($params);
var_dump( $response );

Elastic CloudにElasticsearchPhpでindex作成メモ


require_once __DIR__ . "/vendor/autoload.php";
use Elasticsearch\ClientBuilder;

$hosts = [[
    'host' => '今回はGCPで作った.us-west1.gcp.cloud.es.io',
    'port' => '9243',
    'scheme' => 'https',
    'user' => 'elastic',
    'pass' => 'elastic cloudで作成時もしくはSecurityから再発行'
    ]];

$client = ClientBuilder::create()->setHosts( $hosts )->build();
$params = [
    'index' => 'test_index'
];
$response = $client->indices()->create( $params );
var_dump( $response );

$php elasticphp.php
array(3) {
  ["acknowledged"]=>
  bool(true)
  ["shards_acknowledged"]=>
  bool(true)
  ["index"]=>
  string(14) "test_index"
}

ブラウザからアクセス


$https://****.us-west1.gcp.cloud.es.io:9243/test_index

{
"test_index": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1539144639943",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "emZtlTPUsssexa-hgfIr7w",
"version": {
"created": "6040299"
},
"provided_name": "test_index"
}
}
}
}