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.
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'])
As of elasticsearch-py v8.8.0, the code for the
indexmethod contains:which seems to indicate that:
idis specified, it's a PUTidis not specified, then it's aPOST(and a document ID is auto-generated).That's for the sync version of the
Elasticsearchclient. The same code appears on the async version of the clientAsyncElasticsearch: https://github.com/elastic/elasticsearch-py/blob/v8.8.0/elasticsearch/_async/client/__init__.py#L2214As to the difference in behavior of the PUT and POST on Elasticsearch, rather than a generic reference on PUT and POST, it is best to consult Elasticsearch's Index API documentation itself: https://www.elastic.co/guide/en/elasticsearch/reference/8.8/docs-index_.html.