今天要講的是對ES批量操作數據的方法bulk
在講bulk這個方法之前,我想先說說action結構,其實就是bulk使用時需要使用這個結構
我們直接看action結構的範例
新增 index
{
"_op_type": "index" #執行的動作
"_index": "school_members", #指定index
"_id": 1, #指定id,不指定會自動生成
"_source": { #文檔內容
"sid": "s1090101",
"name": "王小明",
"age": 18,
"class": "資工一1"
}
}
刪除 delete
{
"_index": "school_members",
"_op_type": "delete",
"_id": "qsYuynQBjyvRpz1FfbP9" #要刪除的文檔id
}
更新 update
{
"_index": "school_members",
"_op_type": "update",
"_id": "qcYuynQBjyvRpz1FfbP9", #要更新的文檔id
"doc": {"age": 20} #指定更新內容
}
以這次匯入學生資料為例,下面是bulk的完整程式碼,更新刪除大家可以自己試試看
from elasticsearch import Elasticsearch
from elasticsearch import helpers
import json
def load_datas():
actions = list()
with open('student.csv', 'r') as f:
for data in f.readlines():
sid, name, age, class_ = data.replace('\n', '').split(',')
actions.append({
"_index": "school_members",
"_op_type": "index",
"_source": {
"sid": sid,
"name": name,
"age": int(age),
"class": class_
}
})
return actions
if __name__ == "__main__":
es = Elasticsearch(hosts='192.168.1.59', port=9200)
data = load_datas()
print(json.dumps(data, ensure_ascii=False))
helpers.bulk(es, data)
ES匯入的部分就到這邊告一段落,下篇開始就來告訴大家ES搜尋這件事吧!