使用php curl插入Elasticsearch批量文档

时间:2017-10-25 13:35:07

标签: php curl elasticsearch elasticsearch-plugin

我正在尝试将产品批量插入弹性搜索服务器。当我通过bash命令执行此操作时它工作正常,但当我尝试通过php执行此操作时出现错误

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_PORT => "9200",
    CURLOPT_URL => $elasticHost."/ecommerce/products/_bulk",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 300,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => $myProducts,
    CURLOPT_HTTPHEADER => array(
        "authorization: Basic ".$credentials,
        "cache-control: no-cache",
        "postman-token: 26ce113f-ab54-b1af-aec6-c154d451548f"
    )
));

这是我正在使用的json布局

{ "create": {"_id": "1"}}
{"id":"1","title":"Title goes here"}

{ "create": {"_id": "2"}}
{"id":"2","title":"Title goes here"}

{ "create": {"_id": "3"}}
{"id":"3","title":"Title goes here"}

错误:我收到每个产品的错误

Response: {"took":1345,"errors":true,"items":[{"create":{"_index":"ecommerce","_type":"products","_id":"14737","status":400,"error":{"type":"mapper_parsing_exception","reason":"failed to parse","caused_by":{"type":"json_parse_exception","reason":"Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: org.elasticsearch.common.io.stream.InputStreamStreamInput@745dc80e; line: 1, column: 793]"}}}}

我正在使用的命令:

Curl命令:curl -H“Content-Type:application / json”-XPOST“http://ipaddress:port/ecommerce/products/_bulk?pretty” - data-binary“@ products.json”

0 个答案:

没有答案