About the `index()` method of `Elasticsearch Python Client` library, is it using PUT or POST?

We are looking into a Python library Elasticsearch Python Client, and its official online document contains the below example for ingesting data into Elastic.

We hope to make the ingest action idempotent, so we wonder whether the library's index() method uses PUT or POST.

Please also point out if we should investigate something else for the idempotent feature. We are new to Elasticsearch and its Python libraries, so we highly appreciate any hints and suggestions.

Details:

This reference says:

The PUT Method
The difference between POST and PUT is that PUT requests are idempotent. ...

The example in the official online document:

from datetime import datetime
from elasticsearch import Elasticsearch
es = Elasticsearch('https://localhost:9200')

doc = {
    'author': 'author_name',
    'text': 'Interensting content...',
    'timestamp': datetime.now(),
}
resp = es.index(index="test-index", id=1, document=doc)
print(resp['result'])

See Index API | Elasticsearch Guide [8.11] | Elastic;

PUT /<target>/_doc/<_id>
POST /<target>/_doc/

PUT /<target>/_create/<_id>
POST /<target>/_create/<_id>

You can see that a PUT is when you want to specify an _id, and a POST is when you want to auto create one, which follows the W3 reference you linked to.

That means your quoted example will be a PUT when it sends the request.

1 Like

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