stemmer
This commit is contained in:
+54
-25
@@ -32,7 +32,8 @@ class ElasticHelper():
|
||||
for a in range(0,10):
|
||||
try :
|
||||
if not self.es.ping():
|
||||
sleep(30)
|
||||
print('elastic not ping, sleep 30 s : ', a)
|
||||
sleep(5)
|
||||
continue
|
||||
else:
|
||||
self.success_connect = True
|
||||
@@ -62,6 +63,7 @@ class ElasticHelper():
|
||||
def exportToJsonForAI(self, path_back, index_name, out_name= '', body={}, fields=[]) :
|
||||
print('*' * 50, ' start backup -->', index_name)
|
||||
self.counter = 0
|
||||
sid = None
|
||||
|
||||
out = out_name
|
||||
if out_name == '' :
|
||||
@@ -110,7 +112,7 @@ class ElasticHelper():
|
||||
sid = s_res['_scroll_id']
|
||||
scroll_size = len(s_res['hits']['hits'])
|
||||
|
||||
|
||||
sid = None
|
||||
text = json.dumps(out_json, ensure_ascii=False)
|
||||
fout.write(text)
|
||||
|
||||
@@ -119,23 +121,37 @@ class ElasticHelper():
|
||||
def backupIndexToZipfile(self, path_back, index_name, out_name= '', body={}, byzip = True, fields=[], noFields=[]) :
|
||||
print('*' * 50, ' start backup -->', index_name)
|
||||
self.counter = 0
|
||||
sid = None
|
||||
|
||||
out = out_name
|
||||
if out_name == '' :
|
||||
out = index_name
|
||||
|
||||
|
||||
if body == {} :
|
||||
s_res = self.es.search(
|
||||
index=index_name,
|
||||
scroll='5m',
|
||||
size=1000
|
||||
)
|
||||
else:
|
||||
s_res = self.es.search(
|
||||
index=index_name,
|
||||
scroll='5m',
|
||||
size=1000,
|
||||
body=body
|
||||
)
|
||||
|
||||
self.total = s_res["hits"]["total"]['value']
|
||||
if self.total == 0 :
|
||||
print('total index_name by query = %d' % self.total)
|
||||
return False
|
||||
|
||||
if byzip:
|
||||
fout = zipfile.ZipFile(path_back + "/"+ out + '.zip', 'w')
|
||||
else:
|
||||
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)
|
||||
@@ -181,6 +197,8 @@ class ElasticHelper():
|
||||
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'])
|
||||
sid = None
|
||||
fout.close()
|
||||
|
||||
|
||||
def restorFileToElastic(self, path_back, index_name, app_key = '', queryDelete = True, map_name='') :
|
||||
@@ -321,21 +339,22 @@ class ElasticHelper():
|
||||
|
||||
|
||||
def updateBulkList(self, listData, index_name):
|
||||
chunk_size=100000
|
||||
chunk_size=1000
|
||||
raise_on_error=False
|
||||
raise_on_exception=False
|
||||
stats_only=True
|
||||
yield_ok = False
|
||||
|
||||
actions=[]
|
||||
for item in listData:
|
||||
actions = [{
|
||||
actions.append({
|
||||
"_op_type": "update",
|
||||
"_index": index_name,
|
||||
"_id" : item['_id'],
|
||||
"doc": item['_source']
|
||||
}
|
||||
]
|
||||
helpers.bulk(self.es, actions, chunk_size, raise_on_error, raise_on_exception, stats_only, yield_ok )
|
||||
)
|
||||
helpers.bulk(self.es, actions, chunk_size, raise_on_error, raise_on_exception, stats_only, yield_ok )
|
||||
|
||||
def importBulkList(self, listData, index_name):
|
||||
chunk_size=100000
|
||||
@@ -355,25 +374,35 @@ class ElasticHelper():
|
||||
helpers.bulk(self.es, actions, chunk_size, raise_on_error, raise_on_exception, stats_only, yield_ok )
|
||||
|
||||
|
||||
def importJsonDataToElastic(self, jsonData, index_name):
|
||||
chunk_size=100000
|
||||
def importJsonDataToElastic(self, jsonData, index_name, fields=[]):
|
||||
chunk_size=1000
|
||||
raise_on_error=False
|
||||
raise_on_exception=False
|
||||
stats_only=True
|
||||
yield_ok = False
|
||||
|
||||
actions=[]
|
||||
|
||||
for item in jsonData:
|
||||
actions = [{
|
||||
id = item['_id'] if item['_id'] else item['id']
|
||||
source = item['_source']
|
||||
if fields :
|
||||
source = {}
|
||||
for col in fields :
|
||||
if col in item['_source'] :
|
||||
source[col] = item['_source']
|
||||
|
||||
|
||||
actions.append({
|
||||
"_op_type": "index",
|
||||
"_index": index_name,
|
||||
"_id" : item['_id'],
|
||||
"_source": item['_source']
|
||||
}
|
||||
]
|
||||
helpers.bulk(self.es, actions, chunk_size, raise_on_error, raise_on_exception, stats_only, yield_ok )
|
||||
"_id" : id,
|
||||
"_source": source
|
||||
})
|
||||
helpers.bulk(self.es, actions, chunk_size, raise_on_error, raise_on_exception, stats_only, yield_ok )
|
||||
|
||||
|
||||
def fileToElastic(self, file_path, index_name, limit_pack = -1):
|
||||
def fileToElastic(self, file_path, index_name, limit_pack = -1, fields=[]):
|
||||
if not os.path.exists(file_path):
|
||||
print("file zip:" , file_path , " not exist")
|
||||
return
|
||||
@@ -381,12 +410,12 @@ class ElasticHelper():
|
||||
self.counter = 0
|
||||
with open(file_path) as file:
|
||||
data = json.loads(file.read())
|
||||
self.importJsonDataToElastic(data, index_name)
|
||||
self.importJsonDataToElastic(data, index_name, fields)
|
||||
|
||||
self.es.indices.refresh(index=index_name)
|
||||
print(self.es.cat.count(index=index_name, format="json"))
|
||||
|
||||
def zipFileToElastic(self, file_path, index_name, limit_pack = -1):
|
||||
def zipFileToElastic(self, file_path, index_name, limit_pack = -1, fields=[]):
|
||||
if not os.path.exists(file_path):
|
||||
print("file zip:" , file_path , " not exist for imort to elastic : ", index_name )
|
||||
return
|
||||
@@ -408,7 +437,7 @@ class ElasticHelper():
|
||||
print("index:" , index_name , '=>' , filename )
|
||||
with zObject.open(filename) as file:
|
||||
data = json.loads(file.read())
|
||||
self.importJsonDataToElastic(data, index_name)
|
||||
self.importJsonDataToElastic(data, index_name, fields)
|
||||
|
||||
self.es.indices.refresh(index=index_name)
|
||||
print(self.es.cat.count(index=index_name, format="json"))
|
||||
|
||||
Reference in New Issue
Block a user