# Best way to Index and Map large csv files with Python into Elasticsearch

**URL:** <https://discuss.elastic.co/t/best-way-to-index-and-map-large-csv-files-with-python-into-elasticsearch/183965>\
**Category:** Elasticsearch\
**Created:** [June 3, 2019, 12:50pm UTC](https://discuss.elastic.co/t/best-way-to-index-and-map-large-csv-files-with-python-into-elasticsearch/183965 "2019-06-03T12:50:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maximilian\_Maigler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/maximilian_maigler/32/45217_2.png) [@Maximilian\_Maigler](https://discuss.elastic.co/u/Maximilian_Maigler)\
**Post date:** [June 3, 2019, 12:50pm UTC](https://discuss.elastic.co/t/best-way-to-index-and-map-large-csv-files-with-python-into-elasticsearch/183965/1 "2019-06-03T12:50:09Z")

</div>

Hey guys,

I have a two questions regarding indexing large csv files into Elasticsearch and mapping the data:

1. Should I first create an index and mapping and then send the csv data to Elastic?  
If yes, could someone provide a code snippet / template for creating an index and mapping (corresponding to the csv header) with the elasticsearch-py package?

2. I use helpers.bulk() to index my csv data, but I am not able to map the data with Python then in Elasticsearch. Could someone provide also a code snippet for indexing csv files into a **specifical** index and that the csv data corresponds to the mapping of this index?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)\
**Post date:** [June 4, 2019, 6:00am UTC](https://discuss.elastic.co/t/best-way-to-index-and-map-large-csv-files-with-python-into-elasticsearch/183965/2 "2019-06-04T06:00:57Z")

</div>

Hi,

You can also use template to define your mapping so you don't need to care about creating index and mapping. Index and mapping will be created when you save your csv file.

[https://www.elastic.co/guide/en/elasticsearch/reference/7.1/indices-templates.html](https://www.elastic.co/guide/en/elasticsearch/reference/7.1/indices-templates.html)

[https://docs.python.org/3/library/csv.html](https://docs.python.org/3/library/csv.html) \<--- check the class csv.DictReader

As it return a dict you can build your bulk then send to elastic.

---

<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 2, 2019, 6:01am UTC](https://discuss.elastic.co/t/best-way-to-index-and-map-large-csv-files-with-python-into-elasticsearch/183965/3 "2019-07-02T06:01:12Z")

</div>

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