【问题标题】:Indexing documents in ElasticSearch with PHP curl使用 PHP curl 在 ElasticSearch 中索引文档
【发布时间】:2014-02-28 21:59:52
【问题描述】:

我正在尝试将文档添加到 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);

【问题讨论】:

  • 你试过用 XPOST 代替 XPUT 吗?
  • 是的,XPOST的错误是一样的,但是感谢您的建议。

标签: elasticsearch


【解决方案1】:

您必须将 curl 选项 CURLOPT_CUSTOMREQUEST 设置为 PUT,而不是 XPUT

curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'PUT');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-07
    • 1970-01-01
    • 2015-01-22
    • 2016-01-28
    相关资源
    最近更新 更多