The `Elasticsearch.index()` method是使用PUT还是POST?

huangapple go评论100阅读模式
英文:

About the `Elasticsearch.index()` method, is it using PUT or POST?

问题

我们正在研究一个名为Elasticsearch Python Client的Python库,官方在线文档中包含了以下用于将数据导入Elasticsearch的示例。

我们希望使摄取操作具有幂等性,因此我们想知道该库的index()方法是使用PUT还是POST

此参考链接 表示:

PUT方法
POST和PUT之间的区别在于PUT请求是幂等的...

官方在线文档中的示例:

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

doc = {
    'author': 'author_name',
    'text': '有趣的内容...',
    'timestamp': datetime.now(),
}
resp = es.index(index="test-index", id=1, document=doc)
print(resp['result'])
英文:

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

答案1

得分: 1

截止到 elasticsearch-py v8.8.0index 方法的代码包含以下内容:

def index(
    ...
) -> ObjectApiResponse[t.Any]:
    """
    在索引中创建或更新文档。
    ...
    """
    ...
    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
    )

这段代码似乎表明:

这适用于 Elasticsearch 客户端的同步版本。同样的代码也出现在异步版本的客户端 AsyncElasticsearch 中:链接

至于 Elasticsearch 中 PUT 和 POST 的行为差异,最好查阅 Elasticsearch 的索引API文档本身而不是通用的 PUT 和 POST 参考:链接

英文:

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.

huangapple
  • 本文由 发表于 2023年6月13日 07:01:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460783.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定