refactor
This commit is contained in:
+59
-11
@@ -2,7 +2,6 @@ import zipfile
|
||||
import sys
|
||||
import os
|
||||
import json
|
||||
from copy import deepcopy
|
||||
from time import sleep
|
||||
from elasticsearch import Elasticsearch,helpers
|
||||
|
||||
@@ -93,7 +92,6 @@ class ElasticHelper():
|
||||
for item in s_res['hits']['hits']:
|
||||
|
||||
if fields :
|
||||
#item2 = deepcopy(item)
|
||||
item2={}
|
||||
item2['id']=item['_id']
|
||||
for kf in fields :
|
||||
@@ -153,7 +151,6 @@ class ElasticHelper():
|
||||
out_json = []
|
||||
for item in s_res['hits']['hits']:
|
||||
if fields :
|
||||
#item2 = deepcopy(item)
|
||||
item2={}
|
||||
item2['id']=item['_id']
|
||||
for kf in fields :
|
||||
@@ -232,8 +229,6 @@ class ElasticHelper():
|
||||
print( '----', index_name_o, ' complated')
|
||||
|
||||
|
||||
|
||||
|
||||
def deleteIndex(self, index_name) :
|
||||
if not self.es.indices.exists(index=index_name) :
|
||||
print(' ' * 10, " for delete NOT exist index :", index_name )
|
||||
@@ -376,8 +371,7 @@ class ElasticHelper():
|
||||
}
|
||||
]
|
||||
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):
|
||||
if not os.path.exists(file_path):
|
||||
@@ -454,10 +448,7 @@ class ElasticHelper():
|
||||
# yield from (hit for hit in data)
|
||||
#return data
|
||||
yield from ({"source": hit["_source"], "id": hit["_id"]} for hit in data)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def es_iterate_all_documents(self, index, body="", pagesize=250, scroll_timeout="25m", **kwargs):
|
||||
"""
|
||||
@@ -571,3 +562,60 @@ class ElasticHelper():
|
||||
print(e)
|
||||
|
||||
# save_error(id, e)
|
||||
|
||||
def mappingIndex(self, index_name_i):
|
||||
# فقط از طریق کیبانا میشه تغییر مپ داد
|
||||
|
||||
# با پایتون نمیشه
|
||||
# باید ایندکس جدیدی با مپ مطلوب ایجاد کرد و رایندکس کرد
|
||||
pass
|
||||
|
||||
def updateByQueryIndex(self, index_name_i, body):
|
||||
## sample
|
||||
# body = {
|
||||
# "script": {
|
||||
# "inline": "ctx._source.Device='Test'",
|
||||
# "lang": "painless"
|
||||
# },
|
||||
# "query": {
|
||||
# "match": {
|
||||
# "Device": "Boiler"
|
||||
# }
|
||||
# }
|
||||
# }
|
||||
try:
|
||||
self.es.update_by_query(body=body, index=index_name_i)
|
||||
|
||||
except Exception as e:
|
||||
print(e)
|
||||
# save_error(id, e)
|
||||
|
||||
|
||||
def deleteByQueryIndex(self, index_name_i, body):
|
||||
## sample
|
||||
# body = {
|
||||
# "query": {
|
||||
# "match": {
|
||||
# "Device": "Boiler"
|
||||
# }
|
||||
# }
|
||||
# }
|
||||
try:
|
||||
self.es.delete_by_query(index=index_name_i, body=body )
|
||||
|
||||
except Exception as e:
|
||||
print(e)
|
||||
# save_error(id, e)
|
||||
|
||||
def delete_by_ids(self, index_name_i, ids):
|
||||
try:
|
||||
# ids = ['test1', 'test2', 'test3']
|
||||
|
||||
query = {"query": {"terms": {"_id": ids}}}
|
||||
res = self.es.delete_by_query(index=index_name_i, body=query)
|
||||
print(res)
|
||||
|
||||
except Exception as e:
|
||||
print(e)
|
||||
# save_error(id, e)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user