Elasticsearch批量设置_id

Har*_*rry 2 javascript node.js elasticsearch

当我使用 _id 设置将文档添加到 elasticsearch 时,我得到:

Field [_id] is a metadata field and cannot be added inside a document. Use the index API request parameters.
Run Code Online (Sandbox Code Playgroud)

使用 client.bulk

  const body = dataset.flatMap(doc => [{ index: { _index: 'myindex' } }, doc])  
  const { body: bulkResponse } = await client.bulk({ refresh: true, body })
Run Code Online (Sandbox Code Playgroud)

我没有看到在参数中放置 _id 的地方。

https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html

我应该使用不同的方法吗?

谢谢。

Val*_*Val 8

它需要位于命令部分内,但您还需要将其从源文档中删除doc

                                                                        here
                                                                         |
                                                                         v
const body = dataset.flatMap(doc => [{ index: { _index: 'myindex', _id: doc._id } }, doc])  
const { body: bulkResponse } = await client.bulk({ refresh: true, body })
Run Code Online (Sandbox Code Playgroud)