how to use python Elasticsearch client upsert api

Question:

I’m using Elasticsearch python client as http://elasticsearch-py.readthedocs.org/
I tried hard but still could not find the update api with upsert. Could anyone give me an example with ES python client upsert api please.

Asked By: Jack

||

Answers:

The example code is following.

from elasticsearch import Elasticsearch
es = Elasticsearch("localhost:9200")
es.update(
    index="test",
    doc_type="test1",
    id="1",
    body={
        "doc": {"username": "Tom"}, 
        "doc_as_upsert": True
    }
)

If body is without doc_as_upsert=true the code would throw an exception when the id is not existing. Additionally, make sure your data were wrapped in doc.

Answered By: user5314006

Method index(*args, **kwargs) adds or updates a typed JSON document in a specific index, making it searchable.

As pointed out in Python Elasticsearch Client -> API Documentation.

Answered By: no_igor
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.