为什么Elasticsearch批量API不能parsing我的JSON?

我将这个类似JSON的数据通过HTTP请求发送到Elasticsearch API / _bulk端点:

 {"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} 

当我通过邮递员(一个谷歌浏览器应用程序)的数据,它成功的工作。

当我通过我的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() { ... }); 

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

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