【问题标题】:bulk index with elasticsearch and php curl使用 elasticsearch 和 php curl 的批量索引
【发布时间】:2016-10-08 14:55:48
【问题描述】:

我正在尝试向 ElasticSearch 执行 php CURL 请求以一次索引多个条目(使用 _bulk),但我总是根据请求正文收到不同的错误

>         $posted = '';
>         for ($i = 0; $i < 10; $i++) {
>             $posted .= json_encode(array(
>                 'index' => new stdClass(),
>             )) .'\n';
> 
>             $posted .= json_encode(array(
>                 'id' => $i,
>                 'name' => 'XX' . $i,
>             ));
>             
>             if($i < 9){
>                 $posted .'\n';
>             }
>         }
> 
>         $fullURL = 'http://127.0.0.1:9200/myindex/mytype/_bulk';
>         $conn = curl_init($fullURL);
> 
>         curl_setopt($conn, CURLOPT_RETURNTRANSFER, true);
>         curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, false);
>         curl_setopt($conn, CURLOPT_SSL_VERIFYHOST, false);
>         curl_setopt($conn, CURLOPT_FAILONERROR, false);
>         curl_setopt($conn, CURLOPT_CUSTOMREQUEST, 'POST');
>         curl_setopt($conn, CURLOPT_POSTFIELDS, $posted);
>         $res = curl_exec($conn);
>         echo $res;

使用上面的参数字符串我得到这个错误:

"error":{"root_cause":[{"type":"action_request_validation_exception","re​​ason":"验证失败:1:没有添加请求;"}],"type":"action_request_validation_exception","re​​ason ":"验证失败:1:未添加请求;"},"status":400}

我已经用 JSON 编码的单个请求对其进行了测试,它工作正常。
那么,如何使用 php curl 执行 _bulk 索引?

编辑: --- 为提高可读性而编辑,请参阅下面的答案。我尝试了我在网上找到的所有内容 2 天,希望对其他人有所帮助。

【问题讨论】:

    标签: php curl elasticsearch


    【解决方案1】:

    我终于搞定了!!!

    >         $b = array();
    >         $sets = array();
    > 
    >         $params = array(
    >             '_id' => null,
    >             '_index' => 'myindex',
    >             '_type' => 'mytype'
    >         );
    > 
    >         for ($i = 0; $i < 10; $i++) {
    >             $doc = array(
    >                 'id' => $i,
    >                 'name' => 'name ' . $i,
    >             );
    >             $set = array(
    >                 array('index' => $params),
    >                 $doc
    >             );
    >             $sets[] = $set;
    >         }
    > 
    >         foreach ($sets as $set) {
    >             foreach ($set as $s) {
    >                 $b[] = json_encode($s);
    >             }
    >         }
    >         $body =  join("\n", $b) . "\n";
    >         
    >         $conn = curl_init();
    >         $requestURL = 'http://127.0.0.1:9200/myindex/mytype/_bulk';
    >         curl_setopt($conn, CURLOPT_URL, $requestURL);
    >         curl_setopt($conn, CURLOPT_TIMEOUT, 5);
    >         curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, FALSE);
    >         curl_setopt($conn, CURLOPT_SSL_VERIFYHOST, FALSE);
    >         curl_setopt($conn, CURLOPT_RETURNTRANSFER, TRUE);
    >         curl_setopt($conn, CURLOPT_FAILONERROR, FALSE);
    >         curl_setopt($conn, CURLOPT_CUSTOMREQUEST, strtoupper('POST'));
    >         curl_setopt($conn, CURLOPT_FORBID_REUSE, 0);
    > 
    >         if (is_array($body) && count($body) > 0) {
    >             curl_setopt($conn, CURLOPT_POSTFIELDS, json_encode($body));
    >         } else {
    >             curl_setopt($conn, CURLOPT_POSTFIELDS, $body);
    >         }
    >         
    >         $response = curl_exec($conn);
    >         echo $response;
    

    【讨论】:

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