Elasticsearch document update and insertio using update api

I want to ingest data using Python code, and if data is already available, I just want to update it with the given document for that ID. So far, I have tried this.
Using Elasticsearch 8.6.1


import elasticsearch 
from elasticsearch import Elasticsearch
###############################
Initialization
###############################
es = Elasticsearch("http://localhost:9200")
print(Elasticsearch.info(es))

## I have created an index with two fields one is name & class just to test

## already inserted data is :-1: 
1.name:"murli",class:"abc"
2.name:"omi",class:"yup"
3.name:"abhi"

# defining a dictionary with key value pair and inserting it into es if data id exist it will update it but if not found it ill create it .
#So i just want to know is this approach is write or not. 
a = {"name":"shanks"}
body =  {
    "doc":{ a,},"doc_as_upsert" :True
  }

status = es.update(index="image_text",body=body,id=4)
print(status)

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