javascript - 无法使用 node.js http.request 将记录插入 ElasticSearch - MapperParsingException[无法解析]

标签 javascript node.js rest http elasticsearch

尝试使用 node.js 的 http 模块将记录插入 ElasticSearch(使用第 3 方模块)

设置:在端口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 问题:

这些都没有回答我的问题。

任何帮助非常感谢。 谢谢!

注意:我故意尝试使用仅使用 Node.js Core(无第三方)模块的 ElasticSearch REST API(请不要建议使用 elasticsearch-jsesrequest 等)

最佳答案

回想起来,它显而易见
使用 querystring 模块是错误的。
ElasticSearch 期望数据以 JSON(“字符串化”)形式发送

所以代码需要是:

var http = require('http');

// ElasticSearch Expects JSON not Querystring!
var data = JSON.stringify({
  "text" :"everything is awesome"
});

// An object of options to indicate where to post to
var post_options = {
    host: 'localhost',
    port: '9200',
    path: '/twitter/tweets/1234',
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        '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();

按预期工作。确认使用:

curl -GET http://localhost:9200/twitter/tweets/1234?pretty

(感谢@FelipeAlmeida帮助我实现这一点)

关于javascript - 无法使用 node.js http.request 将记录插入 ElasticSearch - MapperParsingException[无法解析],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27473960/

相关文章:

node.js - zsh : command not found: express - express-generator version 4. 2.0 已安装

javascript - ng-model 命名问题,尝试构建一个数组,但得到了哈希值

javascript - rails 和 Backbone 一起工作

javascript - 需要在浏览器底部保留包含iFrame的div

performance - Node.js CPU 负载均衡

java - Rest API 因 javax.xml.bind.JAXBException(400 错误请求)而失败

javascript - 这个分配是否正确?

node.js - 使用 npm 和 CakePHP 3 - 文件位置

c# - 在 Base Controller 中实现依赖注入(inject)的最佳方式是什么

java - jackson 的 Access.WRITE_ONLY 在测试期间为空