This commit is contained in:
hsafaei
2025-02-09 16:45:52 +03:30
parent d2b56991a0
commit b54683638a
11 changed files with 765 additions and 216 deletions
+74 -4
View File
@@ -2,6 +2,7 @@ import zipfile
import sys
import os
import json
from copy import deepcopy
from time import sleep
from elasticsearch import Elasticsearch,helpers
@@ -59,7 +60,65 @@ class ElasticHelper():
return resp
def backupIndexToZipfile(self, path_back, index_name, out_name= '', query={}, byzip = True) :
def exportToJsonForAI(self, path_back, index_name, out_name= '', body={}, fields=[]) :
print('*' * 50, ' start backup -->', index_name)
self.counter = 0
out = out_name
if out_name == '' :
out = index_name
fout = open( path_back + "/"+ out + '.json', 'a+' , encoding='utf-8')
s_res = self.es.search(
index=index_name,
scroll='5m',
size=1000,
body=body
)
self.total = s_res["hits"]["total"]['value']
print('start index = %s' % index_name)
print('total = %d' % self.total)
sid = s_res['_scroll_id']
scroll_size = len(s_res['hits']['hits'])
file_count = 1
out_json = []
while scroll_size > 0:
"Scrolling..."
self.counter += scroll_size
print("progress -> %.2f %%" % ((self.counter / self.total)*100))
#############################
for item in s_res['hits']['hits']:
if fields :
#item2 = deepcopy(item)
item2={}
item2['id']=item['_id']
for kf in fields :
#print(kf)
if kf in item['_source'] :
# print(item['_source'][kf])
item2[kf] = item['_source'][kf]
#exit()
else :
item2=item
out_json.append(item2)
s_res = self.es.scroll(scroll_id=sid, scroll='2m', request_timeout=100000)
sid = s_res['_scroll_id']
scroll_size = len(s_res['hits']['hits'])
text = json.dumps(out_json, ensure_ascii=False)
fout.write(text)
##############################
def backupIndexToZipfile(self, path_back, index_name, out_name= '', body={}, byzip = True, fields=[]) :
print('*' * 50, ' start backup -->', index_name)
self.counter = 0
@@ -76,7 +135,7 @@ class ElasticHelper():
index=index_name,
scroll='5m',
size=1000,
body=query
body=body
)
self.total = s_res["hits"]["total"]['value']
@@ -92,11 +151,22 @@ class ElasticHelper():
print("progress -> %.2f %%" % ((self.counter / self.total)*100))
#############################
out_json = []
for item in s_res['hits']['hits']:
out_json.append(item)
for item in s_res['hits']['hits']:
if fields :
#item2 = deepcopy(item)
item2={}
item2['id']=item['_id']
for kf in fields :
if kf in item['_source'] :
item2['_source'][kf] = item['_source'][kf]
else :
item2=item
out_json.append(item2)
text = json.dumps(out_json, ensure_ascii=False)
out_json = []
if byzip:
filename = out + str(file_count) + '.json'
file_count +=1