Javascript 使用nodejs将文档索引到elasticsearch
我正在尝试使用nodejs将文档索引到elasticsearch。我用这个。当我执行测试时,我得到:Javascript 使用nodejs将文档索引到elasticsearch,javascript,http,node.js,http-post,
elasticsearch,Javascript,Http,Node.js,Http Post,
elasticsearch,我正在尝试使用nodejs将文档索引到elasticsearch。我用这个。当我执行测试时,我得到: > node test2.js data=(bodyText=bodytext) STATUS: 400 HEADERS: {"content-type":"application/json; charset=UTF-8","content-length":"906"} response: {"error":"ElasticSearchParseException[Failed to de
> node test2.js
data=(bodyText=bodytext)
STATUS: 400
HEADERS: {"content-type":"application/json; charset=UTF-8","content-length":"906"}
response: {"error":"ElasticSearchParseException[Failed to derive xcontent from (offset=0, length=17): [98, 111, 100, 121, 84, 101, 120, 116, 61, 98, 111, 100, 121, 116, 101, 120, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]","status":400}
但是,对curl的调用按预期工作:
curl -XPOST 'http://localhost:9200/google/mail' -d '{
"bodytext": "bodytext"
}'
{ok":true,"_index":"google","_type":"mail","_id":"DMJXn80xTmqdny9l6BwV7w","_version":1}
代码
//elasticsearch.js
http = require('http');
stringify = require('querystring').stringify;
exports.indexDocument = function(document) {
var data = stringify(document);
//data = document;
var options = {
host: 'localhost',
port: '9200',
path: 'google/mail',
method: 'POST'
};
var request = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (response) {
console.log('response: ' + response);
});
});
console.log('data=('+data+')');
request.write(data);
request.end();
}
//test2.js
completeMsg = {
bodyText: "bodytext"
};
require('./elasticsearch').indexDocument(completeMsg);
在curl示例中,您发送的是JSON字符串,但在节点示例中,您发送的是querystring编码的字符串。我预计:
var data = stringify(document)
…与
var data = JSON.stringify(document)
在curl示例中,您发送的是一个JSON字符串,但在节点示例中,您发送的是一个querystring编码的字符串。我预计:
var data = stringify(document)
…与
var data = JSON.stringify(document)
我会做你想做的