# Create Mapping Before Uploading Json Data

**URL:** <https://discuss.elastic.co/t/create-mapping-before-uploading-json-data/180272>\
**Category:** Elasticsearch\
**Created:** [May 9, 2019, 4:03am UTC](https://discuss.elastic.co/t/create-mapping-before-uploading-json-data/180272 "2019-05-09T04:03:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MNurhakimB](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mnurhakimb/32/46513_2.png) [@MNurhakimB](https://discuss.elastic.co/u/MNurhakimB)\
**Post date:** [May 9, 2019, 4:03am UTC](https://discuss.elastic.co/t/create-mapping-before-uploading-json-data/180272/1 "2019-05-09T04:03:11Z")

</div>

I have the following code in python that does upload JSON file to elastic search. What it does is it loops every file in the directory and upload it but unfortunately uses the default type, keyword, for all columns.

```
# Get the host address to connect to Elasticsearch
    es = elasticsearch.Elasticsearch([{'host': 'localhost', 'port': 9200}])
    in_file = r'{}'.format(str(jsonPath))

    for filename in os.listdir(in_file):
        if filename.endswith(".json"):
            # Open JSON File
            jsonFile = codecs.open(in_file + "/" + filename, "r", "utf-8")
            json_file = jsonFile.read()
            jsonFile.close()

            # Upload JSON File to Elasticsearch[body is the content, index is the name given in UI]
            res = es.bulk(body=json_file, index=jsonPathName, doc_type="doc", request_timeout=30)
            print(json.dumps(res, indent=3, separators=(',', ': ')))

```

So I added the following code that includes the mapping before the upload function so that the specified type will be reflected in the mapping:

```
es.indices.put_mapping(
        index=jsonPathName,
        doc_type="doc",
        body={
            "properties": {
                "location": {"type": "keyword"},
                "name_1": {"type": "keyword"},
                "name_2": {"type": "keyword"},
                "door_activity": {"type": "keyword"},
                "date": {"type": "date", "format": "yyyy-MM-dd HH:mm:ss:SSS"}
            }
        }
    )

```

Unfortunately the code above resulted the following error message:  
`*elasticsearch.exceptions.RequestError: RequestError(400, 'illegal_argument_exception', 'Rejecting mapping update to [door_activity_test_new_02] as the final mapping would have more than 1 type: [_doc, doc]')*`

Therefore, can anyone please advice me on how I can rectify the above mentioned issue?

---

<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:** [May 9, 2019, 4:49am UTC](https://discuss.elastic.co/t/create-mapping-before-uploading-json-data/180272/2 "2019-05-09T04:49:05Z")

</div>

Hi,  
the response is in the error message you already created an index with a type called "\_doc" and you set a mapping for the doc\_type="doc"

Try replace doc with \_doc.

```auto
es.indices.put_mapping(
        index=jsonPathName,
        doc_type="_doc",
        body={

```

---

<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:** [June 6, 2019, 4:49am UTC](https://discuss.elastic.co/t/create-mapping-before-uploading-json-data/180272/3 "2019-06-06T04:49:15Z")

</div>

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