項目上線之後,由于疊代更新,需要重建索引,但是為了保證重建索引之後出現異常時可以及時滾回上一版本索引,是以需要給目前版本索引進行備份。
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