一、新建index
设置副本数和自定义分词器等操作
mappings = {
"settings": {
# 副本数
"number_of_replicas": 0,
# 分片数
"number_of_shards": 2,
"analysis": {
"analyzer": {
"comma": {
"type": "pattern",
"pattern": ","
}
}
}
},
"mappings": {
"type_video": {
"properties": {
"video_id": {
"type": "keyword"
},
"category": {
"type": "text",
"analyzer": "comma",
"search_analyzer": "comma"
}
}
}
}
}
后面动态修改index副本数
curl -XPUT 'host:9200/target_index/_settings' -H 'content-Type:application/json' -d '{
"index": {
"number_of_replicas": "0"
}
}'
二、写入
1.使用helpers批量写入,代替index()方法
from elasticsearch import helpers
action = {
"_index": config["index"],
"_type": "type_video",
"_id":item[0],
"_source": body
}
actions.append(action)
helpers.bulk(es, actions)
2.设置别名实现数据全量覆盖
# 已有数据:video_info_final, 别名 video_info
# 1、remove video_info_final的别名video_info,建立 video_info_temp 别名为 video_info
es.indices.update_aliases(body={
"actions": [
{"add": {"index": "video_info_temp", "alias": "video_info"}}
]
})
# 2、删除 video_info_final
try:
es.indices.delete('video_info_final')
print("已经删除video_info_final")
except:
print("no video_info_final")
# 3、数据从video_info_temp 保存到 video_info_final
es.reindex(body={
"source": {
"index": "video_info_temp"
},
"dest": {
"index": "video_info_final"
}
})
#4、remove video_info_temp的别名video_info,建立 video_info_final 别名为 video_info
es.indices.update_aliases(body={
"actions": [
{"add": {"index": "video_info_final", "alias": "video_info"}}
]
})
# 5、删除es临时表 video_info_temp
es.indices.delete('video_info_temp')
三、删除
清空index
def delete_all_document():
"""
清空index
"""
body = {
"query": {
"match_all": {}
}
}
es = Elasticsearch(hosts="xxx:9200")
es.delete_by_query(index=config["index"], body=body)
标签:info,index,body,video,final,es
From: https://www.cnblogs.com/leimu/p/16833797.html