# Python interface with elasticsearch base & PUT request

**URL:** <https://discuss.elastic.co/t/python-interface-with-elasticsearch-base-put-request/219067>\
**Category:** Elasticsearch\
**Created:** [February 12, 2020, 7:07pm UTC](https://discuss.elastic.co/t/python-interface-with-elasticsearch-base-put-request/219067 "2020-02-12T19:07:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cyrduf](https://avatars.discourse-cdn.com/v4/letter/c/bbe5ce/32.png) [@cyrduf](https://discuss.elastic.co/u/cyrduf)\
**Post date:** [February 12, 2020, 7:07pm UTC](https://discuss.elastic.co/t/python-interface-with-elasticsearch-base-put-request/219067/1 "2020-02-12T19:07:42Z")

</div>

Once a base is created when i send a put request with :

r = requests.put("[http://localhost:9200/](http://localhost:9200/)/\_doc/", data=, headers=headers)

I have sometimes exception in elasticsearch :

[2020-02-12T19:09:42,487][INFO][o.e.c.m.MetaDataMappingService] [DESKTOP-D8M21T8] [logbooks\_index\_concepts/MNj\_ZkWZQ2eTGJgxMhNEtw] update\_mapping [\_doc]  
[2020-02-12T19:09:51,490][WARN][o.e.m.j.JvmGcMonitorService] [DESKTOP-D8M21T8] [gc][1662] overhead, spent [824ms] collecting in the last [1.2s]  
[2020-02-12T19:09:55,508][WARN][o.e.m.j.JvmGcMonitorService] [DESKTOP-D8M21T8] [gc][1666] overhead, spent [597ms] collecting in the last [1s]  
[2020-02-12T19:10:01,432][DEBUG][o.e.a.b.TransportShardBulkAction] [DESKTOP-D8M21T8] [logbooks\_index\_concepts][0] failed to execute bulk item (index) index {[logbooks\_index\_concepts][\_doc][219], source[n/a, actual length: [2mb], max length: 2kb]}  
org.elasticsearch.index.mapper.MapperParsingException: failed to parse field [@section.k2] of type [text] in document with id '219'. Preview of field's value: ''  
at org.elasticsearch.index.mapper.FieldMapper.parse(FieldMapper.java:299) ~[elasticsearch-7.5.1.jar:7.5.1]  
at org.elasticsearch.index.mapper.DocumentParser.parseObjectOrField(DocumentParser.java:488) ~[elasticsearch-7.5.1.jar:7.5.1]  
at org.elasticsearch.index.mapper.DocumentParser.parseValue(DocumentParser.java:614) ~[elasticsearch-7.5.1.jar:7.5.1]

How to get the exception from python script :

[logbooks\_index\_concepts][0] failed to execute bulk item (index) index {[logbooks\_index\_concepts][\_doc][219], source[n/a, actual length: [2mb], max length: 2kb]}  
org.elasticsearch.index.mapper.MapperParsingException: failed to parse field [@section.k2] of type [text] in document with id '219'. Preview of field's value: ''  
at org.elasticsearch.index.mapper.FieldMapper.parse(FieldMapper.java:299)

I don't succed to get it from my python script.

---

<div class="post-metadata">

**Author:** ![rameshkr1994](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rameshkr1994/32/59029_2.png) [@rameshkr1994](https://discuss.elastic.co/u/rameshkr1994)\
**Post date:** [February 17, 2020, 9:33am UTC](https://discuss.elastic.co/t/python-interface-with-elasticsearch-base-put-request/219067/2 "2020-02-17T09:33:06Z")

</div>

Hi @cyrduf.  
please try with below code.  
# Import Elasticsearch package  
from elasticsearch import Elasticsearch

# Connect to the elastic cluster

es=Elasticsearch([{'host':'192.XX.XXX.XX','port':9200}])  
print("connection has been done here")

# Let's insert some more documents on index megacorp

e2={  
"first\_name" : "Jane",  
"last\_name" : "Smith",  
"age" : 32,  
"about" : "I like to collect rock albums",  
"interests": ["music"]  
}  
e3={  
"first\_name" : "Douglas",  
"last\_name" : "Fir",  
"age" : 35,  
"about": "I like to build cabinets",  
"interests": ["forestry"]  
}  
res=es.index(index='megacorp',doc\_type='employee',id=2,body=e2)  
print("CREATED",res)  
res=es.index(index='megacorp',doc\_type='employee',id=3,body=e3)  
print ("CREATED",res)  
#print("total no. of records:")  
#False  
#True

---

<div class="post-metadata">

**Author:** ![cyrduf](https://avatars.discourse-cdn.com/v4/letter/c/bbe5ce/32.png) [@cyrduf](https://discuss.elastic.co/u/cyrduf)\
**Post date:** [February 18, 2020, 10:11am UTC](https://discuss.elastic.co/t/python-interface-with-elasticsearch-base-put-request/219067/3 "2020-02-18T10:11:11Z")

</div>

Hi ,

Thank you for your answer but it doesn't answer to my question.

I serach info on elasticsearch exception to be treated by the python API on put requests.  
I give the excpetion I want to treat in my log.

So I search how to manage put request and all the error cases through PYTHON.

Regards

---

<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 17, 2020, 10:11am UTC](https://discuss.elastic.co/t/python-interface-with-elasticsearch-base-put-request/219067/4 "2020-03-17T10:11:17Z")

</div>

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