【发布时间】:2015-08-31 02:29:30
【问题描述】:
尝试使用 node.js 的 http 模块将记录插入 ElasticSearch(不 使用 3rd 方模块)
设置:在端口 9200 上本地运行 ElasticSearch 实例(默认)
Node.js 代码:
var querystring = require('querystring'); // to build our post string
var http = require('http');
// Build the post string from an object
var data = querystring.stringify({
"text" :"hello world"
});
// An object of options to indicate where to post to
var post_options = {
host: 'localhost',
port: '9200',
path: '/twitter/tweets/1',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(data)
}
};
// Set up the request
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
});
});
// post the data
post_req.write(data);
post_req.end();
我收到以下错误:
{"error":"MapperParsingException[failed to parse]; nested:
ElasticsearchParseException[Failed to derive xcontent
from (offset=0, length=18): [116, 101, 120, 116, 61, 104,
101, 108, 108, 111, 37, 50, 48, 119, 111, 114, 108, 100]]; ",
"status":400}
但是执行以下 CURL 可以按预期工作:
curl -XPOST 'http://localhost:9200/twitter/tweet/1' -d '{"message" : "hello world"}'
我查看了以下具有类似错误消息的 StackOverflow 问题:
- Getting error mapper parsing exception while indexing
- Elasticsearch Parse Exception error when attempting to index PDF
- ElasticSearch Error: MapperParsingException failed to parse
这些都没有回答我的问题。
任何帮助非常感谢。 谢谢!
注意:我故意尝试使用仅使用 Node.js Core 的 ElasticSearch REST API( 没有第 3 方)模块(请不要建议使用 elasticsearch-js 或 es 或 request 等)
【问题讨论】:
标签: javascript node.js rest http elasticsearch