项目上线之后,由于迭代更新,需要重建索引,但是为了保证重建索引之后出现异常时可以及时滚回上一版本索引,所以需要给目前版本索引进行备份。
from elasticsearch import Elasticsearch
from elasticsearch import helpers
from settings import config
import json
import time
def get_search_result(es_client, es_search_options, scroll='5m', index=config.APPNAME):
es_result = helpers.scan(
client=es_client,
query=es_search_options,
scroll=scroll,
index=index
)
return es_result
def search(es_client):
# 检索选项
es_search_options = {
"query": {
"match_all": {}
}
}
es_result = get_search_result(es_client, es_search_options)
final_result = []
for item in es_result:
final_result.append(item['_source'])
return final_result
if __name__ == '__main__':
es = Elasticsearch([SEARCH_SERVER])
st1 = time.time()
final_results = search(es)
st2 = time.time()
with open('zkh_backup.json', 'wr') as outfile:
json.dump(final_results, outfile)
print len(final_results)
print st2 - st1