# Auto mapping a csv file

**URL:** <https://discuss.elastic.co/t/auto-mapping-a-csv-file/44695>\
**Category:** Logstash\
**Created:** [March 17, 2016, 10:47am UTC](https://discuss.elastic.co/t/auto-mapping-a-csv-file/44695 "2016-03-17T10:47:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Croos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/croos/32/29489_2.png) [@Croos](https://discuss.elastic.co/u/Croos)\
**Post date:** [March 17, 2016, 10:47am UTC](https://discuss.elastic.co/t/auto-mapping-a-csv-file/44695/1 "2016-03-17T10:47:01Z")

</div>

I'm uploading a csv file to elasticsearch by logstash befor that do the mapping manually. How to automate the mapping when the csv file is uploading?

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [March 17, 2016, 12:10pm UTC](https://discuss.elastic.co/t/auto-mapping-a-csv-file/44695/2 "2016-03-17T12:10:26Z")

</div>

Not sure what you mean here. Dynamic mapping is enabled by default so normally you don't _have to_ specify mappings explicitly. Please explain what you want to do and why.

---

<div class="post-metadata">

**Author:** ![Croos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/croos/32/29489_2.png) [@Croos](https://discuss.elastic.co/u/Croos)\
**Post date:** [March 17, 2016, 12:48pm UTC](https://discuss.elastic.co/t/auto-mapping-a-csv-file/44695/3 "2016-03-17T12:48:20Z")

</div>

> [@magnusbaeck](#):
>
> Dynamic mapping is enabled by default so normally you don't have to specify mappings explicitly.

Correct. But in Dynamic mapping I have to specify the data type of every field in logstash. Otherwise it is mapping as text type value. Am I correct?

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [March 17, 2016, 12:53pm UTC](https://discuss.elastic.co/t/auto-mapping-a-csv-file/44695/4 "2016-03-17T12:53:00Z")

</div>

No, ES will attempt to guess the mapping type based on the contents. Perhaps you just need to convert the fields to the most suitable type before you send them to ES?

---

<div class="post-metadata">

**Author:** ![Croos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/croos/32/29489_2.png) [@Croos](https://discuss.elastic.co/u/Croos)\
**Post date:** [March 17, 2016, 1:10pm UTC](https://discuss.elastic.co/t/auto-mapping-a-csv-file/44695/5 "2016-03-17T13:10:44Z")

</div>

For an Example,  
I have a file

```
+-----------+---------+
| Name | age |
+-----------+---------+
| croos | 26 |
| nilu | 30 |
+-----------+---------+   

```

Would you please give me a example for the mapping of the above csv file.

Field name and data types have to mapped as `name (text), age (integer)`.

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [March 17, 2016, 6:36pm UTC](https://discuss.elastic.co/t/auto-mapping-a-csv-file/44695/6 "2016-03-17T18:36:52Z")

</div>

If you convert the `age` field to an integer field (e.g. using the mutate filter's convert option) then ES should map that field as an integer.

---

<div class="post-metadata">

**Author:** ![Croos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/croos/32/29489_2.png) [@Croos](https://discuss.elastic.co/u/Croos)\
**Post date:** [March 18, 2016, 8:19am UTC](https://discuss.elastic.co/t/auto-mapping-a-csv-file/44695/7 "2016-03-18T08:19:54Z")

</div>

ya. That is what I'm using now.

```
input {
  file {
    sincedb_path => "/null"
    path => "\user.csv"
    start_position => "beginning"
    type => "data"
  }
}
filter {
  csv{
    columns => ["name","age"]
    separator => ","
  }
  mutate {
    convert => { "age" => "integer" }
  }
}

```

But my questios are,

1. If I not use the mutate filter, Will ES attempt to guess the type of age as integer? (because the two values are looking like integer)

2. How to mention the first row is field name? so do the mapping for name and age automatically?

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [March 18, 2016, 8:36am UTC](https://discuss.elastic.co/t/auto-mapping-a-csv-file/44695/8 "2016-03-18T08:36:31Z")

</div>

1. Maybe, I'm not sure. Should be easy to test, right?
2. That's not possible out of the box. The file input doesn't necessarily read the file from the top and it has no knowledge of CSV columns.

---

<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 6, 2017, 5:06am UTC](https://discuss.elastic.co/t/auto-mapping-a-csv-file/44695/9 "2017-07-06T05:06:31Z")

</div>


