使用PHP curl在ElasticSearch中索引文档

Boo*_*aka 4 elasticsearch

我正在尝试将文档添加到ElasticSearch.我在命令行中使用curl没有问题,但是在PHP中使用curl时遇到了麻烦.我在文档中关注此示例:http: //www.elasticsearch.org/guide/reference/api/index_.html

以下代码给出了这个错误: {"error":"IndexMissingException[[twitter] missing]","status":404}

$search_host = '127.0.0.1';
$search_port = '9200';
$index = 'twitter';
$doc_type = 'tweet';
$doc_id = 1;

    $json_doc = array(
                "user" => "kimchy",
                "post_date" => "2012-11-15T14:12:12",
                "message" => "trying out Elastic Search"
            );
    $json_doc = json_encode($json_doc);

    $baseUri = 'http://'.$search_host.':'.$search_port.'/'.$index.'/'.$doc_type.'/'.$doc_id;

    $ci = curl_init();
    curl_setopt($ci, CURLOPT_URL, $baseUri);
    curl_setopt($ci, CURLOPT_PORT, $search_port);
    curl_setopt($ci, CURLOPT_TIMEOUT, 200);
    curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ci, CURLOPT_FORBID_REUSE, 0);
    curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'XPUT');
    curl_setopt($ci, CURLOPT_POSTFIELDS, $json_doc);
    $response = curl_exec($ci);
Run Code Online (Sandbox Code Playgroud)

Tho*_*ten 9

您必须将curl选项设置CURLOPT_CUSTOMREQUESTPUT,而不是XPUT.

curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'PUT');
Run Code Online (Sandbox Code Playgroud)