使用 JS SDK 的 ElasticSearch 批量 API



我有这个电话:

const lst = [];  // an array with a few object in it 
c.bulk({
index: 'foo',
body: lst.map(v => JSON.stringify(v)).join('n')
})
.catch(e => {
log.error(e);
});

我正在尝试将多个项目插入 ES。批量 API 在这里: https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html

但是由于上述调用的某种原因,我收到此错误:

响应错误:illegal_argument_exception 传入消息。 (/Users/alex/codes/interos/@interos/elastic-search/node_modules/@elastic/elasticsearch/lib/Transport.js:287:25( at IncomingMessage.emit (events.js:208:15( at endReadableNT (_stream_readable.js:1154:12( at processTicksAndRejections (内部/流程/task_queues.js:77:11(

我尝试使用这个:

c.bulk({
method: 'POST',
index: 'foo',
body: lst.map(v => JSON.stringify(v)).join('n') + 'n'
})
.catch(e => {
log.error(e);
});

使用method:POST并确保在正文末尾有一个换行符,仍然是相同的错误:(

使用 JS 批量 API 时,您不需要字符串化任何内容,最重要的是,您要索引的每个对象之前都缺少命令行。

您的body应该像这样构造:

const body = lst.flatMap(doc => [{ index: { _index: 'foo' } }, doc])
c.bulk({
index: 'foo',
body: body
})
.catch(e => {
log.error(e);
});

最新更新