【问题标题】:Why can't Elasticsearch bulk API parse my JSON?为什么 Elasticsearch 批量 API 无法解析我的 JSON?
【发布时间】:2016-12-23 19:31:31
【问题描述】:

我通过对 Elasticsearch API /_bulk 端点的 HTTP 请求获得了 PUTing 这个类似 JSON 的数据:

{"update":{"_id":1,"_type":"myType","_index":"myIndex"}}
{"doc":{"id":1,"name":"Foo"},"doc_as_upsert":true}
{"update":{"_id":2,"_type":"myType","_index":"myIndex"}}
{"doc":{"id":2,"name":"Bar"},"doc_as_upsert":true}

当我通过 Postman(Google Chrome 应用程序)PUT 数据时,它可以成功运行。

当我通过我的 Node.js 脚本(request() 调用)PUT 数据时,我收到此错误:

{
  "error" : {
    "root_cause" : [ {
      "type" : "parse_exception",
      "reason" : "Failed to derive xcontent"
    } ],
    "type" : "parse_exception",
    "reason" : "Failed to derive xcontent"
  },
  "status" : 400
}

我正在双向发送(我假设是)完全相同的数据,但只有邮递员在工作。我以 \n 字符(包括最后一个字符)结束每一行,并且我相信我的格式是正确的。

是不是我做错了什么?

编辑:节点代码(简化):

var sourceData = JSON.parse(fs.readFileSync('test-data.json', 'utf8')),

sourceData.forEach(function(data) {
    var docid = data.id;

    updates.push(JSON.stringify({
        update: {
            _id: docid,
            _type: config.elasticsearch.type,
            _index: config.elasticsearch.index
        }
    }));

    updates.push(JSON.stringify({
        doc: data,
        doc_as_upsert: true
    }));
});

updates = updates.join("\n") + "\n";

request({
    uri: 'http://my-endpoint/_bulk',
    method: 'POST',
    data: updates,
    proxy: 'http://' + config.proxy.host + ':' + config.proxy.port
}, function() { ... });

【问题讨论】:

  • 你能展示你的node.js代码吗?
  • 您确定 Content-Type 标头设置为 text/plain 而不是 application/json?
  • 如果我没记错的话,您需要使用body 参数来发送更新,而不是data(用于多部分请求)
  • @Val .........我使用的是data而不是body。他妈的。谢谢。如果您提出新的答案,我会打勾。
  • 很高兴我们弄明白了。

标签: json node.js amazon-web-services elasticsearch


【解决方案1】:

您需要使用body 参数来发送更新,而不是data(用于多部分请求)

request({
    uri: 'http://my-endpoint/_bulk',
    method: 'POST',
    body: updates,               // <---- change this
    proxy: 'http://' + config.proxy.host + ':' + config.proxy.port
}, function() { ... });

【讨论】:

    猜你喜欢
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    相关资源
    最近更新 更多