javascript - 使用nodejs 将文档索引到elasticsearch

标签 javascript http node.js http-post elasticsearch

我正在尝试使用nodejs 将文档索引到elasticsearch。我用http library from node.js 。当我执行测试时,我得到:

> 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字符串,但在您的 Node 示例中,您正在发送一个查询字符串编码的字符串。我希望替换:

var data = stringify(document)

...与...

var data = JSON.stringify(document)

会做你想做的事。

关于javascript - 使用nodejs 将文档索引到elasticsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9824613/

相关文章:

javascript - 尝试使用 Python 和 Selenium 迭代地滚动和抓取网页

javascript - 如何从类别 channel 中删除 channel (文本或语音)?

javascript - 如何在 Node 中找到引用站点 URL?

node.js - 从 Electron 应用程序中设置服务器

javascript - 使用回调对函数进行排序

javascript - 如何获取在拖动事件 jqueryui/jquery 中单击了哪个 div/ui.helper?

javascript - if else 验证方法之间的区别

c# - ASP.NET WebApi - 如何将集合发布到 WebApi 方法?

python - Python 中的简单 URL GET/POST 函数

javascript - 使用 Cheerio 和 Response for Node 网络抓取工具,将响应函数结果传递到 View