elasticsearch,Php,Curl,elasticsearch" /> elasticsearch,Php,Curl,elasticsearch" />

Elasticsearch-PHP批量

Elasticsearch-PHP批量,php,curl,elasticsearch,Php,Curl,elasticsearch,我正在尝试从PHP通过cURL将数据批量导入elasticsearch 首先,我想补充一点,我复制了PHP生成的导入数据格式,并将其粘贴到Sense中,批量导入工作正常。但是,通过cURL将相同的数据发送到相同的链接,使用我在Sense中使用的相同方法,我收到了以下错误消息: {"_index":"product","_type":"pid","_id":"_bulk",&qu

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

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

{"_index":"product","_type":"pid","_id":"_bulk","found":false}
或者,如果我没有通过link指定_索引和_类型,而是通过我发送的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();
}

考虑$JSONYA数据被正确格式化,同时,考虑我使用正确的链接/方法。

同样,我知道elasticsearch php github repo(甚至在那里搜索它们是如何批量的,这与我的方法类似),但我更喜欢编写自己的方法和库,因为我现在需要的不需要一个完整的elastic php库


我做错了什么?

您是否考虑过,正如ES文档中所述,当向批量端点发送请求时,内容类型头应设置为application/x-ndjson

另一个可能的情况是,您使用的是
CURLOPT_CUSTOMREQUEST=>“GET”
,但对ES的真正请求是POST

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


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

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);