从0.10.40升级后,节点HTTP删除请求不再有效

Dav*_*ave 2 http node.js elasticsearch

我一直在尝试将我的大多数节点内容升级到4.x但是在执行对Elasticsearch的删除查询时遇到了一些问题.以下工作在0.10.40和之前,但不适用于4.xx或5.7.0.我没有想法,似乎节点没有发送我的请求正文,因为我从Elasticsearch返回的错误是{"error":"ActionRequestValidationException[Validation Failed: 1: source is missing;]","status":400}.

var http = require('http');
var request = http.request({
    host: 'localhost',
    port: 9200,
    path: 'test/col/_query',
    method: 'DELETE'
});

request.on('response', function(res) {
    res.setEncoding('utf8');
    res.on('data', function(response) {
        if(response.indexOf('"failed":0') === -1) {
            console.log('Failed. Response: ', response);
            process.exit(1);
        }
    });
    res.on('end',  function() {
        console.log('completed successfully');
        process.exit(0);
    });
});
request.on('error', function(err) { cb(err); });

var q = {
    query: {
        "bool": {
            "must_not": [
                {"ids": {"values": ['1','2','3'] } }
            ]
        }
    }
};

request.write(JSON.stringify(q));
request.end();
Run Code Online (Sandbox Code Playgroud)

Val*_*Val 11

HTTP DELETE请求不应包含有效负载.但是,在0.10中,它得到了支持,因为Transfer-Encoding: Chunked即使没有向服务器发送有效负载,HTTP报头也是沿着请求发送的.这由问题61640.11.14中修复.

从现在开始,如果您确实需要发送带有DELETE请求的正文,您还需要添加一个Content-Length标题来指定您发送的内容的长度,否则服务器将忽略任何有效负载.

因此,如果您构建这样的请求,它将起作用:

var q = {
    query: {
        "bool": {
            "must_not": [
                {"ids": {"values": ['1','2','3'] } }
            ]
        }
    }
};
var payload = JSON.stringify(q);

var request = http.request({
    host: 'localhost',
    port: 9200,
    path: 'test/col/_query',
    method: 'DELETE',
    headers: {                                    <--- add this
      'Content-Type': 'application/json',
      'Content-Length': Buffer.byteLength(payload)
    }
});

...

request.write(payload);
request.end();
Run Code Online (Sandbox Code Playgroud)