php - Elasticsearch批量数据插入-JsonParseException [意外字符-使用PHP

标签 php json curl elasticsearch bulk

使用PHP curl方法为大容量数据建立索引时,出现了"error":"JsonParseException[Unexpected character (':' (code 58)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: [B@14abb68; line: 1, column: 18]]","status":500}这个异常

请在下面找到我正在使用的相同代码,并让我知道这里可能有什么问题。

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

?>

最佳答案

您使用的格式不正确(第二个元素还有一个逗号)。
它应该是:

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的文档中提取

因此,您应该使用以下格式添加数据:
{"index":{"_index": "myindex","_type":"test"}}
{"product_id": 1,"title": "mobile"}
{"index":{"_index": "myindex","_type":"test"}}
{"product_id": 2,"title": "laptop"}

关于php - Elasticsearch批量数据插入-JsonParseException [意外字符-使用PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33107738/

相关文章:

php - 使用 array_key_exists 将总金额与 id 配对

php - 将 NodeJS 服务器端口重定向到子域或文件夹 (amazon ec2)

javascript - 既然 Twitter 需要 JavaScript,我该如何抓取它们?

php - 如何在 Laravel 的链接中检索 http get 参数?

php - 如何从 View 行中获取节点 ID?

python - 代码未从 json GET 响应中删除 "\n"

android - 使用 sencha 创建 Android 应用程序时出错

java - 将 json 文件解析为 imageButton

redirect - 使用基本身份验证返回 401 而不是 301 重定向的 HTTP 请求

macos - 为什么curl(由 Homebrew 程序启动)在.nix-profile中查找?