#!/usr/bin/python
#coding=utf-8
#op type: index, create , delete, update
#www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html
from datetime import datetime
from elasticsearch import Elasticsearch
from elasticsearch import helpers
es = Elasticsearch()
es.indices.put_settings(index="index_0",body={"refresh_interval":-1,"number_of_replicas":0}) # for single node
file ="/home/allen/bbb.txt"
f=open(file,'r')
actions = []
for i in f:
if i=="" or i=="\n" or i=="\r\n":
continue
action={
"_op_type":"index",
"_index":"index_0",
"_type":"type_0",
"_source":{
"content":i
}
}
actions.append(action)
helpers.bulk(es,actions)
f.close()
es.indices.put_settings(index="index_0",body={"refresh_interval":"1s"}) # change back to default
使用bulk
之前最好设置一下某些参数,可以使用put_settings()
函数
参考资料