如何批量发送请求?弹性搜索
How to send request with bulk? Elasticsearch
我有一个包含数据的对象,我想将此数据发送到 elasticsearch 容器
for(let key in params)
{
bulk.push(JSON.stringify({
index: {
_id: params[ key ][ 'id' ],
_type: 'id',
_index: 'geo'
}
}));
bulk.push(JSON.stringify(params[key]));
}
let bulks = bulk.join("\n") + "\n";
我提出了要求
let cat = request(
{
'method' : 'PUT',
'uri' : 'http://dev4.int10h.net:40024/_bulk',
'body' : bulks ,
'json' : true,
'headers':
[
'Content-Type: application/x-ndjson'
],
'agent' : false
}
);
但有错误
Unhandled rejection StatusCodeError: 400 - {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"The bulk request must be terminated by a newline [\n]"}],"type":"illegal_argument_exception","reason":"The bulk request must be terminated by a newline [\n]"},"status":400}
at new StatusCodeError (/usr/lib/node_modules/request-promise/node_modules/request-promise-core/lib/errors.js:32:15)
at Request.plumbing.callback (/usr/lib/node_modules/request-promise/node_modules/request-promise-core/lib/plumbing.js:104:33)
at Request.RP$callback [as _callback] (/usr/lib/node_modules/request-promise/node_modules/request-promise-core/lib/plumbing.js:46:31)
at Request.self.callback (/usr/lib/node_modules/request/request.js:185:22)
at Request.emit (events.js:182:13)
at Request. (/usr/lib/node_modules/request/request.js:1161:10)
at Request.emit (events.js:182:13)
at IncomingMessage. (/usr/lib/node_modules/request/request.js:1083:12)
at Object.onceWrapper (events.js:273:13)
at IncomingMessage.emit (events.js:187:15)
at endReadableNT (_stream_readable.js:1098:12)
at process.internalTickCallback (internal/process/next_tick.js:72:19)
如何正确发送批量?
批量类型string
不确定这是否对您有帮助,但 here 我在 _bulk
和 JSON.stringify
中发现了类似的问题。
答案是:
It looks like the meta characters in your payload does not get
translated into newlines. If you instead used the
elasticsearch.js client, it would handle this for you.
我有一个包含数据的对象,我想将此数据发送到 elasticsearch 容器
for(let key in params)
{
bulk.push(JSON.stringify({
index: {
_id: params[ key ][ 'id' ],
_type: 'id',
_index: 'geo'
}
}));
bulk.push(JSON.stringify(params[key]));
}
let bulks = bulk.join("\n") + "\n";
我提出了要求
let cat = request(
{
'method' : 'PUT',
'uri' : 'http://dev4.int10h.net:40024/_bulk',
'body' : bulks ,
'json' : true,
'headers':
[
'Content-Type: application/x-ndjson'
],
'agent' : false
}
);
但有错误
Unhandled rejection StatusCodeError: 400 - {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"The bulk request must be terminated by a newline [\n]"}],"type":"illegal_argument_exception","reason":"The bulk request must be terminated by a newline [\n]"},"status":400} at new StatusCodeError (/usr/lib/node_modules/request-promise/node_modules/request-promise-core/lib/errors.js:32:15) at Request.plumbing.callback (/usr/lib/node_modules/request-promise/node_modules/request-promise-core/lib/plumbing.js:104:33) at Request.RP$callback [as _callback] (/usr/lib/node_modules/request-promise/node_modules/request-promise-core/lib/plumbing.js:46:31) at Request.self.callback (/usr/lib/node_modules/request/request.js:185:22) at Request.emit (events.js:182:13) at Request. (/usr/lib/node_modules/request/request.js:1161:10) at Request.emit (events.js:182:13) at IncomingMessage. (/usr/lib/node_modules/request/request.js:1083:12) at Object.onceWrapper (events.js:273:13) at IncomingMessage.emit (events.js:187:15) at endReadableNT (_stream_readable.js:1098:12) at process.internalTickCallback (internal/process/next_tick.js:72:19)
如何正确发送批量?
批量类型string
不确定这是否对您有帮助,但 here 我在 _bulk
和 JSON.stringify
中发现了类似的问题。
答案是:
It looks like the meta characters in your payload does not get translated into newlines. If you instead used the elasticsearch.js client, it would handle this for you.