Python interface with elasticsearch base & PUT request

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

r = requests.put("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.

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

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