2012-03-22 3 views
0

Я пытаюсь индексировать документы в elasticsearch с помощью nodejs. Я использую http library from node.js. Когда я исполню мой тест я получаю:Индексирование документов в elasticsearch с помощью nodejs

> 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

В примере с скручиванием вы отправляете строку JSON, но в примере вашего узла вы отправляете строку с строкой, зависящей от строки. Я ожидаю, что замена:

var data = stringify(document) 

... с ...

var data = JSON.stringify(document) 

будет делать то, что вы хотите.

Смежные вопросы