doroidpanic.com

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"
}
}
}
}

ElasticsearchにWordPressのRSS/atomを投入するのに初期設定メモ


dp@elasticsearch-1-vm:~$curl -XPUT 'localhost:9200/mytest_index'
{"acknowledged":true,"shards_acknowledged":true,"index":"mytest_index"}
dp@elasticsearch-1-vm:~$ curl -X GET 'localhost:9200/mytest_index?pretty'
{
  "usedoore_index" : {
    "aliases" : { },
    "mappings" : { },
    "settings" : {
      "index" : {
        "creation_date" : "1538907897984",
        "number_of_shards" : "5",
        "number_of_replicas" : "1",
        "uuid" : "pAkv7kWAQJKUnxyy91M9PA",
        "version" : {
          "created" : "6040099"
        },
        "provided_name" : "mytest_index"
      }
    }
  }
}