2012-03-22 9 views
0

nodejを使用してelasticsearchに文書を索引作成しようとしています。私はhttp library from node.jsを使用します。私は私のテストを実行すると、私が得る: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 -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); 

答えて

5

curlの例では、JSON文字列を送信していますが、ノードの例では、文字列にエンコードされた文字列を送信しています。

var data = stringify(document) 

を... ...

var data = JSON.stringify(document) 

であなたがやりたいだろう:私は交換していることを期待しています。

関連する問題