簡體   English   中英

彈性搜索 - PHP 批量

[英]Elasticsearch - PHP Bulk

我正在嘗試批量 - 通過 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 存儲庫(甚至搜索了它們如何在其中進行批量處理,這與我的方法類似),但我現在更願意編寫自己的方法和庫,因為我現在需要的是moment 不需要完整的 elastic-php 庫。

我究竟做錯了什么?

您是否考慮過 ES 文檔中所寫的,當向 _bulk 端點發送請求時,Content-Type 標頭應設置為 application/x-ndjson

ES 批量文檔

另一件事可能是您正在使用CURLOPT_CUSTOMREQUEST => "GET"但對 ES 的真正請求是 POST。

根據 DOCS,它可能導致 libcurl 發送無效請求,並且可能嚴重混淆遠程服務器

CURL_OPT 文檔

如果其中一種方法有效,請告訴我,我會將答案編輯為正確的方法

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM