Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node+ElasticSearch: Sending a body on a GET request?

I am using Node.js and the request module to create a backend, and we've chose Elasticsearch as our data storage. All fine so far, except it seems Node doesn't support request bodies on GET requests? This is necessary for Elasticsearch's _search API, which expects only GET requests as part of their semantic design. Is there a solution to force Node to send the request body even in the cases of a GET request, or a mean to use _search on Elasticsearch with another HTTP verb?

function elasticGet(url, data) {
    data = data || {};
    return Q.nfcall(request.get, {
        uri: url,
        body: JSON.stringify(data) //<-- noop
    }).then(function(response) {
        return JSON.parse(response[1]);
    }, function(err) {
        console.log(err);
    });
}
like image 857
Kroltan Avatar asked Mar 20 '23 04:03

Kroltan


2 Answers

The _search API also accepts the POST verb.

like image 71
Mattias Nordberg Avatar answered Mar 22 '23 23:03

Mattias Nordberg


For simplicity, why not use their api rather than manually making requests?

simple example:

 var elasticsearch = require('elasticsearch'),
    client = new elasticsearch.Client({
        host: '127.0.0.1:9200',
        log: 'trace'
    });
    client.search({
        index: '[your index]',
        q: 'simple query',
        fields: ['field']
    }, function (err, results) {
        if (err) next(err);
        var ids = []
        if (results && results.hits && results.hits.hits) {
            ids = results.hits.hits.map(function (h) {
                return h._id;
            })
        }
        searchHandler(ids, next)
    })

You can combine it with fullscale labs elastic.js to build really complex queries, really fast. https://github.com/fullscale/elastic.js

like image 31
Chris Tinsley Avatar answered Mar 22 '23 23:03

Chris Tinsley