Question

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);
    });
}
Was it helpful?

Solution

The _search API also accepts the POST verb.

OTHER TIPS

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

I had such an issue a few days ago.

tld;dr use POST

According to https://www.elastic.co/guide/en/elasticsearch/guide/current/_empty_search.html#get_vs_post you can also use POST with elastic.

I tried it with axios but it returns all data like with no body. So I used POST instead. It works for me and I hope it will help to someone else.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top