【问题标题】:Elasticsearch - PHP BulkElasticsearch - PHP 批量
【发布时间】:2017-12-10 03:09:20
【问题描述】:

我正在尝试通过 cURL 从 PHP 批量导入数据到 elasticsearch。

首先,我想补充一点,我复制了 PHP 生成的导入数据格式并将其粘贴到 Sense 中,批量导入工作正常。但是通过 cURL 将相同的数据发送到相同的链接,使用我在 Sense 中使用的相同方法,我收到以下错误消息:

{"_index":"product","_type":"pid","_id":"_bulk","found":false}

或者,如果我没有通过链接指定 _index 和 _type,而是通过我发送的 json 指定它,我会收到以下错误

{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"No endpoint or operation is available at [_bulk]"}],"type":"illegal_argument_exception","reason":"No endpoint or operation is available at [_bulk]"},"status":400}

我创建 cURL 请求的方式如下

protected $curl_opts = array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_TIMEOUT => 10
);

......

public function sendcURL() {
    $this->curl->create('http://localhost:9200/_bulk';

    foreach($this->curl_opts as $option => $option_value)
        $this->curl->option($option, $option_value);

    $this->curl->http_header('Content-Type', 'application/json');
    $this->curl->option(CURLOPT_BINARYTRANSFER, true);
    $this->curl->option(CURLOPT_HEADER, true);
    $this->curl->post($json_data);
    $this->execute();
}

考虑到 $json_data 的格式正确,同时考虑到我使用了正确的链接/方法。

同样,我知道 elasticsearch-php github 存储库(甚至搜索了它们是如何在其中进行批量处理的,这与我的方法相似),但我现在更喜欢编写自己的方法和库,因为我目前需要不需要完整的 elastic-php 库。

我做错了什么?

【问题讨论】:

    标签: php curl elasticsearch


    【解决方案1】:

    您是否考虑过,正如 ES 文档中所写,当向 _bulk 端点发送请求时,Content-Type 标头应设置为 application/x-ndjson

    ES bulk Docs

    其他情况可能是您使用的是CURLOPT_CUSTOMREQUEST => "GET",但对 ES 的真正请求是 POST。

    根据 DOCS,它可能导致 libcurl 发送无效请求,并可能严重混淆远程服务器

    CURL_OPT DOCS

    如果其中一种方法有效,请告诉我,我会将答案编辑为正确的方法

    【讨论】:

      【解决方案2】:

      CURLOPT_CUSTOMREQUEST => 'GET'可以和php curl一起使用:

      $requests = '';
      foreach ($queries as $query) {
          list ($data, $headers) = $query;
          $requests .= json_encode($headers) . "\n";
          $requests .= json_encode($data) . "\n";
      }
      
      $ch = $this->_ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $requests);
      curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-ndjson']);
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
      $response = @curl_exec($ch);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-12
        • 2014-11-27
        • 1970-01-01
        • 2016-04-16
        • 1970-01-01
        • 2017-03-22
        • 2017-05-17
        • 2018-01-17
        相关资源
        最近更新 更多