我们正在研究Python库Elasticsearch Python Client
,其官方在线文档包含以下将数据摄取到Elastic中的示例。
我们希望使摄取动作幂等,所以我们想知道库的index()
方法使用的是PUT
还是POST
。
This reference说:
PUT方法
POST和PUT的区别在于PUT请求是幂等的。...
官网文档示例:
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条答案
按热度按时间xyhw6mcr1#
从elasticsearch-py v8.8.0开始,
index
方法的代码包含:这似乎表明:
id
,则为PUTid
* not* 指定,则它是POST
(并且自动生成文档ID)。这是
Elasticsearch
客户端的同步版本。同样的代码出现在客户端AsyncElasticsearch
的异步版本上:https://github.com/elastic/elasticsearch-py/blob/v8.8.0/elasticsearch/_async/client/init.py#L2214至于Elasticsearch上PUT和POST的行为差异,而不是PUT和POST的通用参考,最好查阅Elasticsearch的Index API文档本身:https://www.elastic.co/guide/en/elasticsearch/reference/8.8/docs-index_.html。