# ES ignores mapping when using Python DSL bulk

**URL:** <https://discuss.elastic.co/t/es-ignores-mapping-when-using-python-dsl-bulk/120050>\
**Category:** Elasticsearch\
**Created:** [February 15, 2018, 6:16pm UTC](https://discuss.elastic.co/t/es-ignores-mapping-when-using-python-dsl-bulk/120050 "2018-02-15T18:16:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![druzmieres](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/druzmieres/32/25828_2.png) [@druzmieres](https://discuss.elastic.co/u/druzmieres)\
**Post date:** [February 15, 2018, 6:16pm UTC](https://discuss.elastic.co/t/es-ignores-mapping-when-using-python-dsl-bulk/120050/1 "2018-02-15T18:16:43Z")

</div>

I'm trying to upload a CSV file to an elasticsearch index. Let's say the file is something like this (no headers, just data):

```
bird,10,dog
cat,20,giraffe

```

This is the code I have:

```
from elasticsearch_dsl import DocType, Integer, Keyword
from elasticsearch_dsl.connections import connections
from elasticsearch.helpers import bulk
import csv

connections.create_connection(hosts=["localhost"])

class Mapping(DocType):    
    animal1 = Keyword()
    number = Integer()
    animal2 = Keyword()

    class Meta:
        index = "index-name"
        doc_type = "doc-type" 

Mapping.init()
with open("/path/to/file", "r", encoding="latin-1") as f:
    reader = csv.DictReader(f)
    bulk(
        connections.get_connection(),
        (Mapping(**row).to_dict(True) for row in reader)
    )

```

The problem is that elasticsearch seems to be ignoring the mapping and using the first line of the file as headers (and creating a mapping based on that).

Edit: it really uses my mapping and the first line of the file. The mapping it generates is:

```
{
  "index-name": {
    "mappings": {
      "doc-type": {
        "properties": {
          "10": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "dog": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "animal1": {
            "type": "keyword"
          },
          "animal2": {
            "type": "keyword"
          },
          "bird": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "number": {
            "type": "integer"
          }
        }
      }
    }
  }
}

```

If I only create the index without uploading data, the mapping seems fine:

```
{
  "index-name": {
    "mappings": {
      "doc-type": {
        "properties": {
          "animal1": {
            "type": "keyword"
          },
          "animal2": {
            "type": "keyword"
          },
          "number": {
            "type": "integer"
          }
        }
      }
    }
  }
}

```

How can I make ES use the given mapping and just that?

---

<div class="post-metadata">

**Author:** ![druzmieres](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/druzmieres/32/25828_2.png) [@druzmieres](https://discuss.elastic.co/u/druzmieres)\
**Post date:** [February 19, 2018, 4:04pm UTC](https://discuss.elastic.co/t/es-ignores-mapping-when-using-python-dsl-bulk/120050/2 "2018-02-19T16:04:18Z")

</div>

The problem wasn't bulk. `csv.DictReader` always reads the first line from the file to get the headers for subsequent rows. So if you're going to use `DictReader`, the file needs a header.

---

<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:** [March 19, 2018, 4:04pm UTC](https://discuss.elastic.co/t/es-ignores-mapping-when-using-python-dsl-bulk/120050/3 "2018-03-19T16:04:27Z")

</div>

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