【发布时间】: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