Search code examples
pythonelasticsearchelasticsearch-py

About the `Elasticsearch.index()` method, 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.

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'])

Solution

  • As of elasticsearch-py v8.8.0, the code for the index method contains:

    def index(
        ...
    ) -> ObjectApiResponse[t.Any]:
        """
        Creates or updates a document in an index.
        ...
        """
        ...
        if index not in SKIP_IN_PATH and id not in SKIP_IN_PATH:
            __path = f"/{_quote(index)}/_doc/{_quote(id)}"
            __method = "PUT"
        elif index not in SKIP_IN_PATH:
            __path = f"/{_quote(index)}/_doc"
            __method = "POST"
        ...
        return self.perform_request(  # type: ignore[return-value]
            __method, __path, params=__query, headers=__headers, body=__body
        )
    

    which seems to indicate that:

    That's for the sync version of the Elasticsearch client. The same code appears on the async version of the client AsyncElasticsearch: https://github.com/elastic/elasticsearch-py/blob/v8.8.0/elasticsearch/_async/client/__init__.py#L2214

    As 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.