需求:Elasticsearch索引,如果存在,删除索引下的数据,若不存在,则新建索引
代码:
from elasticsearch import Elasticsearch import elasticsearch.helpers
es = Elasticsearch('127.0.0.1:7676')
mappings = { "mappings": { "dl_view_type": { "properties": { "dl_view_data": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 # 最大可被term索引到的最大值 } } }, "dl_view_res": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } } } }
list = [] # 存放目前es数据库里所有的索引 indices = es.indices.get_alias() print(indices) for k, v in indices.items(): list.append(k)
if 'dl_view' in list: es.delete_by_query(index='dl_view',body={'query':{'match_all':{}}}) # 若存在,则删除索引下全部数据 else: es.indices.create(index='dl_view', body=mappings) # 不存在,新建索引
es_data = [] # 需要存放的数据 for each in view_data: each_data = dict() dic = {} dic["basic_str"] = {"age": each["age_years"]} dic["basic_str"] = {"gender": each["gender_name"]} dic["basic_str"] = {"height": each["height"][0]} dic["basic_str"] = {"weight": each["weight"][0]} # each_data['name'] = name each_data['data'] = each each_data['res'] = dic es_data.append(each_data)
actions = [ { '_op_type': 'index', '_index': "dl_view", # index '_type': "dl_view_type", # type '_source': one } for one in es_data ] # 将数据和索引绑定
elasticsearch.helpers.bulk(es, actions, index="dl_view", raise_on_error=True) # 往es里导入数据
此时,如果不报错,那就是导入成功了,如果报错,一般都是mapping和你要导入的数据不匹配导致的,例如下面的错误信息:
按照提示信息,对你的mappings进行修改即可。