elasticsearch批量index,update,delete——Bulk Helpers

来源:互联网 发布:贪心算法的基本思想 编辑:程序博客网 时间:2024/05/22 23:29

1.  批量建索引

for i in range(0,1000):

newDic = {"key":"value"}

action = {"_index": IndexName, "_type": TypeName, "_id": _id, "_source": newDic}
actions.append(action)


#--------------bukl index actions
 if len(actions)>0:
            finished = False
            while not finished:
                try:
                    # bulk create index
                    helpers.bulk(es, actions, chunk_size=1000, request_timeout=30)
                    finished = True
                except ConnectionTimeout:
                    s = traceback.format_exc()
                    print s
            del actions[:]
            

2. 批量删除

action = {
    '_op_type': 'delete',
    '_index': 'index-name',
    '_type': 'document',
    '_id': 42,
}

actions.append(action)


3. 批量更新
action = {
    '_op_type': 'update',
    '_index': 'index-name',
    '_type': 'document',
    '_id': 42,
    'doc': {'question': 'The life, universe and everything.'}
}

actions.append(action)

0 0
原创粉丝点击