Elasticsearch批量数据插入 - JsonParseException [意想不到的人物 - 使用PHP

问题描述:

在使用PHP卷曲的方法,我得到异常作为"error":"JsonParseException[Unexpected character (':' (code 58)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: [[email protected]; line: 1, column: 18]]","status":500}Elasticsearch批量数据插入 - JsonParseException [意想不到的人物 - 使用PHP

请在下面找到它,我使用了相同的代码,做让我索引批量数据知道这里有什么可能是错的。

<?php 
$ch = curl_init(); 
$method = "POST"; 
$url = "http://192.168.1.204/myindex/test/_bulk"; 


$qry = ' 
{"index":{"_index": "myindex","_type":"test"}} 
{ 
    "product_id": 1, 
    "title": "mobile" 
} 
{"index":{"_index": "myindex","_type":"test"}} 
{ 
    "product_id": 2, 
    "title": "laptop", 
} 
'; 

curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_PORT, 9200); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method)); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $qry); 

$result = curl_exec($ch); 
curl_close($ch); 
    echo $result; 

?> 

您没有使用正确的格式(并且在第二个元素上有一个逗号)。 它应该是:

{"index":{"_index": "myindex","_type":"test"}} 
{"product_id": 1,"title": "mobile"} 
{"index":{"_index": "myindex","_type":"test"}} 
{"product_id": 2,"title": "laptop"} 

action_and_meta_data\n 
optional_source\n 
action_and_meta_data\n 
optional_source\n 
.... 
action_and_meta_data\n 
optional_source\n 

elasticsearch - bulk

所以文件提取出来,你应该使用以下格式添加数据