【问题标题】:ElasticSearch bulk API using JS SDK使用 JS SDK 的 ElasticSearch 批量 API
【发布时间】:2019-09-09 23:32:23
【问题描述】:

我有这个电话:

  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

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

ResponseError: 非法参数异常在 传入消息。 (/Users/alex/codes/interos/@interos/elastic-search/node_modules/@elastic/elasticsearch/lib/Transport.js:287:25) 在 incomingMessage.emit (events.js:208:15) 在 endReadableNT (_stream_readable.js:1154:12) 在 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 并确保正文末尾有一个换行符,仍然是同样的错误:(

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

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

    你的body 应该这样构造:

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

    【讨论】:

      猜你喜欢
      • 2020-08-12
      • 2014-11-27
      • 1970-01-01
      • 2017-05-24
      • 2018-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-18
      相关资源
      最近更新 更多