# My Mapping with python is not working

**URL:** <https://discuss.elastic.co/t/my-mapping-with-python-is-not-working/302545>\
**Category:** Elasticsearch\
**Created:** [April 16, 2022, 9:27am UTC](https://discuss.elastic.co/t/my-mapping-with-python-is-not-working/302545 "2022-04-16T09:27:30Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Celal\_Celik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/celal_celik/32/96397_2.png) [@Celal\_Celik](https://discuss.elastic.co/u/Celal_Celik)\
**Post date:** [April 16, 2022, 9:27am UTC](https://discuss.elastic.co/t/my-mapping-with-python-is-not-working/302545/1 "2022-04-16T09:27:30Z")

</div>

Hello community,  
I was trying to upload a simple csv-File but unfortunatelty my Mapping is not working. All my fields are still "keywords". What am I missing?

```auto
def csv_reader(filename,indexname):
    # connection to ElasticSearch: port and host are variable, standard is like this
    es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
    # if es is reached, it prints connected else it reads not connected
    if es.ping():
        print("connected")
    else:
        print("Not connected")

    with open(filename, 'r') as outfile:
        reader = csv.DictReader(outfile)
        Settings = {
            "settings": {
                "numer_of_shards": 1,
                "number_of_replicas": 0
            },
            "mappings": {
                "members": {
                    "dynamic": "strict",
                    "properties": {
                        "_id": {
                            "type": "long"
                        },
                        "name": {
                            "type": "text"
                        },
                        "gefundene_fehler": {
                            "type": "long"
                        },
                        "behobene_fehler": {
                            "type": "long"
                        },
                        "updated_time": {
                            "type": "date"
                        },
                        "time": {
                            "type": "date"
                        },
                        "timestamp": {
                            "type": "date"
                        }
                    }
                }
            }
        }
        if es.indices.exists(indexname):
            print("deleting existing index")
            es.indices.delete(index=indexname, ignore=[400,404])
            print("creating new index1")
            es.indices.create(index=indexname, ignore=400, body=Settings)
            helpers.bulk(es, reader, index=indexname)
        else:
            es.indices.create(index=indexname, ignore=400, body=Settings)
            helpers.bulk(es, reader, index=indexname)
            print("creating new index2")
    print("all lines loaded")

```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 16, 2022, 9:43am UTC](https://discuss.elastic.co/t/my-mapping-with-python-is-not-working/302545/2 "2022-04-16T09:43:59Z")

</div>

It sounds like you are using an old version of Elasticsearch. Is that right?

---

<div class="post-metadata">

**Author:** ![Celal\_Celik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/celal_celik/32/96397_2.png) [@Celal\_Celik](https://discuss.elastic.co/u/Celal_Celik)\
**Post date:** [April 16, 2022, 9:54am UTC](https://discuss.elastic.co/t/my-mapping-with-python-is-not-working/302545/3 "2022-04-16T09:54:10Z")

</div>

Hello,  
I am using ES 7.10.2

Is it maybe possible, that when I use the csv.DictReader(outfile)- comand, that my columns will be read in a String format?  
Thank you

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [April 16, 2022, 7:46pm UTC](https://discuss.elastic.co/t/my-mapping-with-python-is-not-working/302545/4 "2022-04-16T19:46:19Z")

</div>

> [@Celal\_Celik](#):
>
> `numer_of_shards`

The correct is " **number\_of\_shards**"

> [@Celal\_Celik](#):
>
> ` "members": {`

in version 7.10.2 this type will not work

> [@Celal\_Celik](#):
>
> ```auto
> "_id": {
> "type": "long"
> },
> 
> ```

The \_id is reserved, change it to another name like "[something]\_id"

---

<div class="post-metadata">

**Author:** ![Celal\_Celik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/celal_celik/32/96397_2.png) [@Celal\_Celik](https://discuss.elastic.co/u/Celal_Celik)\
**Post date:** [April 17, 2022, 11:11am UTC](https://discuss.elastic.co/t/my-mapping-with-python-is-not-working/302545/5 "2022-04-17T11:11:26Z")

</div>

Thank you for your response. But unfortunately its not working. I just want to update a csv-File to Elasticsearch. If I try it like this, then I get following errors in PyCharm:

> 1. line 399, in bulk for ok, item in streaming\_bulk(client, actions, \*args, \*\*kwargs)
> 2. line 320, in streaming\_bulk for data, (ok, info) in zip(
> 3. line 249, in \_process\_bulk\_chunk for item in gen:
> 4. line 188, in \_process\_bulk\_chunk\_success raise BulkIndexError("%i document(s) failed to index." % len(errors), errors)  
> elasticsearch.helpers.errors.BulkIndexError: ('2 document(s) failed to index.', [{'index': {'\_index': 'mdfadf', '\_type': '\_doc', '\_id': 'UmY3N4ABaUVkc-44MXpt', 'status': 400, 'error': {'type': 'mapper\_parsing\_exception', 'reason': "failed to parse field [time] of type [date] in document with id 'UmY3N4ABaUVkc-44MXpt'. Preview of field's value: '2022-01-28 13:03:29'", 'caused\_by': {'type': 'illegal\_argument\_exception', 'reason': 'failed to parse date field [2022-01-28 13:03:29] with format [strict\_date\_optional\_time||epoch\_millis]', 'caused\_by': {'type': 'date\_time\_parse\_exception', 'reason': 'Failed to parse with all enclosed parsers'}}}, 'data': {'name': 'ProjectR', 'gefunde\_fehler': '7', 'behobene\_fehler': '9', 'time': '2022-01-28 13:03:29', 'updated\_time': '2022-04-17 13:10:01.337150'}}}, {'index': {'\_index': 'mdfadf', '\_type': '\_doc', '\_id': 'U2Y3N4ABaUVkc-44MXpt', 'status': 400, 'error': {'type': 'mapper\_parsing\_exception', 'reason': "failed to parse field [time] of type [date] in document with id 'U2Y3N4ABaUVkc-44MXpt'. Preview of field's value: '2022-02-16 17:00:26'", 'caused\_by': {'type': 'illegal\_argument\_exception', 'reason': 'failed to parse date field [2022-02-16 17:00:26] with format [strict\_date\_optional\_time||epoch\_millis]', 'caused\_by': {'type': 'date\_time\_parse\_exception', 'reason': 'Failed to parse with all enclosed parsers'}}}, 'data': {'name': 'ProjectB', 'gefunde\_fehler': '2', 'behobene\_fehler': '8', 'time': '2022-02-16 17:00:26', 'updated\_time': '2022-04-17 13:10:01.337150'}}}])

This is my mapping:

```
    Settings = {
        "settings": {
            "number_of_shards": 1,
            "number_of_replicas": 0
        },
        "mappings": {
                "properties": {
                    "name": {
                        "type": "text"
                    },
                    "gefundene_fehler": {
                        "type": "long"
                    },
                    "behobene_fehler": {
                        "type": "long"
                    },
                    "updated_time": {
                        "type": "date"
                    },
                    "time": {
                        "type": "date"
                    },
                    "timestamp": {
                        "type": "date"
                    }
                }
            }
        }

```

---

<div class="post-metadata">

**Author:** ![Celal\_Celik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/celal_celik/32/96397_2.png) [@Celal\_Celik](https://discuss.elastic.co/u/Celal_Celik)\
**Post date:** [April 17, 2022, 11:14am UTC](https://discuss.elastic.co/t/my-mapping-with-python-is-not-working/302545/6 "2022-04-17T11:14:34Z")

</div>

If I try to upload the csv-File in Kibana with the import csv-File funciton, then I get the following error:

> [@CSV file uploading error](https://discuss.elastic.co/t/csv-file-uploading-error/302552):
>
> Hello, I am using ES 7.10 and tried to upload a csv file, so I can make a dashboard in Kibana. But unfortunately I get this error. My csv file looks like this: [error3] My Mapping looks like this: [error1] and this is the error: Thank you

I don't know how to solve this issue.  
Thanks

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 17, 2022, 11:55am UTC](https://discuss.elastic.co/t/my-mapping-with-python-is-not-working/302545/7 "2022-04-17T11:55:12Z")

</div>

You need to fix the date format for your date fields.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [April 18, 2022, 1:55am UTC](https://discuss.elastic.co/t/my-mapping-with-python-is-not-working/302545/8 "2022-04-18T01:55:57Z")

</div>

You need to apply date formats to your field.

to '2022-02-16 17:00:26' use yyyy-MM-dd HH:mm:ss.

to '2022-04-17 13:10:01.337150' use strict\_date\_optional\_time\_nanos.

Note that you need to add the 'T' in values for "updated\_time" like this: 2022-04-17T13:10:01.337150 or save without the nanoseconds.  
If you choose a date without nanosecond, you can use the field format **time**.

```auto
  "updated_time": {
          "type": "date",
          "format": "strict_date_optional_time_nanos"
        },
        "time": {
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss"
        },

```

See more info here:

> **[format | Elasticsearch Guide \[8.1\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html)**

---

<div class="post-metadata">

**Author:** ![Celal\_Celik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/celal_celik/32/96397_2.png) [@Celal\_Celik](https://discuss.elastic.co/u/Celal_Celik)\
**Post date:** [April 18, 2022, 10:34am UTC](https://discuss.elastic.co/t/my-mapping-with-python-is-not-working/302545/9 "2022-04-18T10:34:02Z")

</div>

Hello,  
first of all I want to thank you both for your help. It worked finally. You guys are legends.

```
                   "updated_time": {
                        "type": "date",
                        "format": "yyyy-MM-dd HH:mm:ss"
                    },
                    "time": {
                        "type": "date",
                        "format": "yyyy-MM-dd HH:mm:ss"
                    }

```

This is my mapping and instead of

> datetime.now()

I used for updated\_time

> datetime.now().strftime("%Y-%m-%d %H:%M:%S")

Thanks again for your help

---

<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 16, 2022, 10:34am UTC](https://discuss.elastic.co/t/my-mapping-with-python-is-not-working/302545/10 "2022-05-16T10:34:17Z")

</div>

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