英文:
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.0,index
方法的代码包含以下内容:
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
)
这段代码似乎表明:
- 如果指定了
id
,则使用 PUT 方法。 - 如果 未 指定
id
,则使用POST
方法(并且会自动生成文档ID)。
这适用于 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:
- If an
id
is specified, it's a PUT - If an
id
is not specified, then it's aPOST
(and a document ID is auto-generated).
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论