# Bulk insert with python

**URL:** <https://discuss.elastic.co/t/bulk-insert-with-python/269883>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [April 12, 2021, 1:25pm UTC](https://discuss.elastic.co/t/bulk-insert-with-python/269883 "2021-04-12T13:25:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![eliase](https://avatars.discourse-cdn.com/v4/letter/e/df788c/32.png) [@eliase](https://discuss.elastic.co/u/eliase)\
**Post date:** [April 12, 2021, 1:25pm UTC](https://discuss.elastic.co/t/bulk-insert-with-python/269883/1 "2021-04-12T13:25:41Z")

</div>

Hi everyone,

just as a foreword: I'm new to elasticsearch (and this forum 🙂) and still figuring out a lot.

So, I successfully tried to use bulk insertion from a file with the following content:

```auto
{ "index" : {"_type" : "_doc", "_id": "828ef1361dad4f289de8983e90f7ea96"} }
{"last_updated": "2020-02-03T14:15:01Z", "waiver": {"has_waiver": false}, "id": "828ef1361dad4f289de8983e90f7ea96"}

```

Then I tried to do the same with the python module and I wanted to use the existing "id" field as the "\_id" for the elasticsearch document. So I did the following in python:

```auto
>>> from elasticsearch import Elasticsearch
>>> from elasticsearch.helpers import bulk
>>> client = Elasticsearch()
>>> bulk(client, ['{"_id": "828ef1361dad4f289de8983e90f7ea96","last_updated": "2020-02-03T14:15:01Z", "waiver": {"has_waiver": false}}'], index="test")

```

However, I got the following error:

```auto
elasticsearch.helpers.errors.BulkIndexError: ('1 document(s) failed to index.', [{'index': {'_index': 'test', '_type': '_doc', '_id': 'CTg9xngBlOHxyFjlu4JA', 'status': 400, 'error': {'type': 'mapper_parsing_exception', 'reason': "failed to parse field [_id] of type [_id] in document with id 'CTg9xngBlOHxyFjlu4JA'. Preview of field's value: '828ef1361dad4f289de8983e90f7ea96'", 'caused_by': {'type': 'mapper_parsing_exception', 'reason': 'Field [_id] is a metadata field and cannot be added inside a document. Use the index API request parameters.'}}, 'data': '{"_id": "828ef1361dad4f289de8983e90f7ea96","last_updated": "2020-02-03T14:15:01Z", "waiver": {"has_waiver": false}}'}}])

```

Does anyone know how to get around the issue and use the ID field?

---

<div class="post-metadata">

**Author:** ![eliase](https://avatars.discourse-cdn.com/v4/letter/e/df788c/32.png) [@eliase](https://discuss.elastic.co/u/eliase)\
**Post date:** [April 13, 2021, 9:08am UTC](https://discuss.elastic.co/t/bulk-insert-with-python/269883/2 "2021-04-13T09:08:15Z")

</div>

Figured it out. It should be a dict and not a string.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 11, 2021, 9:08am UTC](https://discuss.elastic.co/t/bulk-insert-with-python/269883/3 "2021-05-11T09:08:42Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
