# How to bulk insert documents?

**URL:** <https://discuss.elastic.co/t/how-to-bulk-insert-documents/63063>\
**Category:** Elasticsearch\
**Created:** [October 14, 2016, 2:08pm UTC](https://discuss.elastic.co/t/how-to-bulk-insert-documents/63063 "2016-10-14T14:08:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![frederes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/frederes/32/12476_2.png) [@frederes](https://discuss.elastic.co/u/frederes)\
**Post date:** [October 14, 2016, 2:08pm UTC](https://discuss.elastic.co/t/how-to-bulk-insert-documents/63063/1 "2016-10-14T14:08:57Z")

</div>

Hello,

I need to index a lot of documents (types) into my index, what is the best mehod ? bulk ?  
How do i pass all the document fields to the bulk statement ? (because in the doc i only see '\_id')  
thank you

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [October 14, 2016, 2:19pm UTC](https://discuss.elastic.co/t/how-to-bulk-insert-documents/63063/2 "2016-10-14T14:19:25Z")

</div>

See [https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html). You need to pass a body that contains a number of lines that is a multiple of 2. Every even line contains metadata about the document (its id, which index it should go into, etc.) and every odd line contains the source of the json document that you want to index.

---

<div class="post-metadata">

**Author:** ![frederes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/frederes/32/12476_2.png) [@frederes](https://discuss.elastic.co/u/frederes)\
**Post date:** [October 14, 2016, 2:51pm UTC](https://discuss.elastic.co/t/how-to-bulk-insert-documents/63063/3 "2016-10-14T14:51:48Z")

</div>

Thank you for your quick response 🙂  
OK, i just tested it works.  
How do i specify the type of the fields in the index ? (string, float, date etc)

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [October 14, 2016, 2:56pm UTC](https://discuss.elastic.co/t/how-to-bulk-insert-documents/63063/4 "2016-10-14T14:56:37Z")

</div>

This should be done at index creation time in the `mappings` section. See [https://www.elastic.co/guide/en/elasticsearch/reference/2.4/indices-create-index.html#mappings](https://www.elastic.co/guide/en/elasticsearch/reference/2.4/indices-create-index.html#mappings)

---

<div class="post-metadata">

**Author:** ![frederes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/frederes/32/12476_2.png) [@frederes](https://discuss.elastic.co/u/frederes)\
**Post date:** [October 14, 2016, 3:46pm UTC](https://discuss.elastic.co/t/how-to-bulk-insert-documents/63063/5 "2016-10-14T15:46:23Z")

</div>

Ok, thanks.  
What about templates ?

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [October 14, 2016, 3:59pm UTC](https://discuss.elastic.co/t/how-to-bulk-insert-documents/63063/6 "2016-10-14T15:59:14Z")

</div>

Templates are fine too, they are a way to autematically put mappings at index creation time.

---

<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:** [July 5, 2017, 10:12pm UTC](https://discuss.elastic.co/t/how-to-bulk-insert-documents/63063/7 "2017-07-05T22:12:12Z")

</div>


