hae*_*ney 3 python elasticsearch
我需要像使用 XPOST 一样存储唯一的自动 ID
因为每次启动程序时,数据都会被覆盖。
但是,我找不到在Python中自动生成id的示例
您能告诉我是否有什么好的例子吗?
我的代码:
def saveES(output,es):
bodys=[]
i=0
while i<len(output)-1: #output[len(output)-1] is space
json_doc=json.dumps(output[i])
body = {
"_index":"crawler",
"_type":"typed",
"_id":saveES.counter,
"_source":json_doc
}
i+=1
bodys.append(body)
saveES.counter+=1
helpers.bulk(es,bodys)
Run Code Online (Sandbox Code Playgroud)
小智 5
您不需要在 python 中执行此操作 - 如果您索引没有 id 的文档,Elasticsearch 将自动创建一个唯一的 id。但是,如果由于某种原因你想在 python 中生成 id,你可以使用uuid。
如果您使用ES Python 客户端,es.create(...)没有 ID将无法使用。你应该改为es.index(...)。
两者都调用 Elasticsearch Index API,但将参数es.create设置op_type为create、es.index和index。
如果ID已经存在,该create操作就会失败,因此不会接受没有ID的调用。
| 归档时间: |
|
| 查看次数: |
10446 次 |
| 最近记录: |