Elasticsearch bulk set _id

Viewed 901

When I'm adding docs to elasticsearch with _id set I get:

Field [_id] is a metadata field and cannot be added inside a document. Use the index API request parameters.

Using client.bulk

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

I don't see a place to put the _id in the parameters.

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

Am I supposed to use a different method?

Thanks.

1 Answers

It needs to be inside the command part, but you also need to remove it from the source document in doc:

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