Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用nodejs将文档索引到elasticsearch_Javascript_Http_Node.js_Http Post_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch - Fatal编程技术网 elasticsearch,Javascript,Http,Node.js,Http Post,elasticsearch" /> elasticsearch,Javascript,Http,Node.js,Http Post,elasticsearch" />

Javascript 使用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

我正在尝试使用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 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)
我会做你想做的