-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.py
836 lines (687 loc) · 29 KB
/
core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
from elasticsearch import AsyncElasticsearch, Elasticsearch, helpers
# import aiohttp
from helpers import functions
from helpers.colors import C
import asyncio
import traceback
import uuid
import json
class Olastic:
elastic: Elasticsearch = None
def __init__(self, hosts, port):
Olastic.hosts = hosts
Olastic.port = port
# Olastic.session = aiohttp.ClientSession()
Olastic.elastic = AsyncElasticsearch(hosts=Olastic.hosts)
Olastic.sync = Elasticsearch(hosts=Olastic.hosts)
async def getConnection(hosts, port):
# Olastic.session = aiohttp.ClientSession()
Olastic.elastic = AsyncElasticsearch(hosts=Olastic.hosts)
Olastic.sync = Elasticsearch(hosts=Olastic.hosts)
return Olastic.elastic
async def close(self):
await Olastic.elastic.close()
def OlasticSchema(cls):
if not hasattr(cls, 'elastic'):
raise Exception("Error: a connection to elasticsearch has to be established before using an Olastic schema")
if cls.elastic is None:
raise Exception("Error: the connection to elasticsearch has to be established before defining an Olastic schema")
cls.subclass_name: str = cls.__name__
cls.index_name = cls.subclass_name.lower()
if cls.index_name[-1] == "y":
cls.index_name = cls.index_name[:-1]
cls.index_name += "ies"
else:
cls.index_name += "s"
instance = cls()
mapping = {
'mappings': {
'properties': {
}
}
}
# mapping["mappings"]["properties"]["_id"] = {"type": "keyword"}
cls.field_names = vars(instance)
for fieldname, value in cls.field_names.items():
if fieldname == "created_at" or fieldname == "updated_at" or fieldname == "_id":
continue
if fieldname in ["$not"]:
raise Exception(f"TODO Error: cannot use {fieldname} as a field name at the moment")
if issubclass(type(value), bool):
mapping["mappings"]["properties"][fieldname] = {"type": "boolean"}
elif issubclass(type(value), int):
mapping["mappings"]["properties"][fieldname] = {"type": "long"}
elif issubclass(type(value), float):
mapping["mappings"]["properties"][fieldname] = {"type": "double"}
elif issubclass(type(value), str):
mapping["mappings"]["properties"][fieldname] = {"type": "keyword"}
else:
mapping["mappings"]["properties"][fieldname] = {"type": "long"}
mapping["mappings"]["properties"]["created_at"] = {"type": "date"}
mapping["mappings"]["properties"]["updated_at"] = {"type": "date"}
doesExists = bool(cls.sync.indices.exists(index=cls.index_name))
if doesExists:
print(f"{C.warn}[Olastic] Warning:{C.end} Index already exists")
else:
cls.sync.indices.create(index=cls.index_name, body=mapping)
if not doesExists:
if cls.sync.indices.exists(index=cls.index_name):
print(f"{C.ok}[Olastic] Info:{C.end} Index {cls.index_name} created successfully !")
else:
raise Exception(f"{C.err}[Olastic] Error:{C.end} Failed to create index '{cls.index_name}'.")
original_init = cls.__init__
def new(self, *args, **kwargs):
original_init(self, *args, **kwargs)
for fieldname, default_val in cls.field_names.items():
val = getattr(self, fieldname)
cast_val = val
if issubclass(type(default_val), bool): # this if HAS to come before the int case, because
cast_val = functions.resolve_bool(val)
elif issubclass(type(default_val), float):
cast_val = float(val)
elif issubclass(type(default_val), int):
cast_val = int(val)
else:
cast_val = str(val)
setattr(self, fieldname, cast_val)
setattr(self, "_id", str(functions.uuid_hex()))
setattr(self, "created_at", functions.get_current_date())
setattr(self, "updated_at", functions.get_current_date())
cls.__init__ = new
def toString(self):
return json.dumps(self.__dict__, indent=4)
cls.__str__ = toString
cls.__repr__ = toString
def create(doc_dict, exists=False):
inst = cls()
for fieldname, default_val in cls.field_names.items():
if fieldname in doc_dict:
val = doc_dict[fieldname]
cast_val = val
if issubclass(type(default_val), bool): # this if HAS to come before the int case, because
cast_val = functions.resolve_bool(val)
elif issubclass(type(default_val), float):
cast_val = float(val)
elif issubclass(type(default_val), int):
cast_val = int(val)
else:
cast_val = str(val)
setattr(inst, fieldname, cast_val)
setattr(inst, "_id", doc_dict["_id"])
if "created_at" in doc_dict:
setattr(inst, "created_at", doc_dict["created_at"])
if "updated_at" in doc_dict:
setattr(inst, "updated_at", doc_dict["updated_at"])
# print(doc_dict["_id"])
if not exists:
setattr(inst, "_id", str(functions.uuid_hex()))
setattr(inst, "created_at", functions.get_current_date())
setattr(inst, "updated_at", functions.get_current_date())
return inst
cls.create = create
def _resolve_value_by_fieldname(field, value, elastic_query, clause="must"):
default_type = type(cls.field_names[field])
if issubclass(default_type, bool):
elastic_query["query"]["bool"][clause].append({
"term": {
f"{field}": functions.resolve_bool(value)
}
})
elif issubclass(default_type, float) or issubclass(default_type, int):
if type(value) == dict:
if "$lt" in value or "$gt" in value or "$lte" in value or "$gte" in value:
range_query = {
"range": {
f"{field}": {}
}
}
if "$lt" in value:
range_query["range"][field]["$lt"] = default_type(value["$lt"])
if "$gt" in value:
range_query["range"][field]["$gt"] = default_type(value["$gt"])
if "$lte" in value:
range_query["range"][field]["$lte"] = default_type(value["$lte"])
if "$gte" in value:
range_query["range"][field]["$gte"] = default_type(value["$gte"])
else:
raise Exception(f"Error: Invalid value ({str(value)}) given to field: '{field}'")
else:
elastic_query["query"]["bool"][clause].append({
"term": {
f"{field}": default_type(value)
}
})
elif issubclass(default_type, str):
if type(value) == list:
elastic_query["query"]["bool"][clause].append({
"terms": {
f"{field}": value
}
})
else:
elastic_query["query"]["bool"][clause].append({
"term": {
f"{field}": str(value)
}
})
else:
if type(value) == list:
elastic_query["query"]["bool"][clause].append({
"terms": {
f"{field}": value
}
})
else:
elastic_query["query"]["bool"][clause].append({
"term": {
f"{field}": str(value)
}
})
return elastic_query
def find(query):
elastic_query = {
"size": 10_000,
"query": {
"bool": {
"must_not": [],
"must": []
}
},
"sort": [
{"created_at": {"order": "desc"}}
]
}
for field, value in query.items():
# =========================================================;
# =========================================================;
# =========================================================;
if field in ["_id", "created_at", "updated_at"]:
clause = "must"
if type(value) == dict:
if "$not" in value:
value = value["$not"]
clause = "must_not"
if type(value) == list:
elastic_query["query"]["bool"][clause].append({
"terms": value
})
else:
elastic_query["query"]["bool"][clause].append({
"term": value
})
elif field in cls.field_names:
clause = "must"
if type(value) == dict:
if "$not" in value:
value = value["$not"]
clause = "must_not"
elastic_query = _resolve_value_by_fieldname(field=field, value=value, elastic_query=elastic_query, clause=clause)
return OlasticQuery(search_query=elastic_query, cls=cls)
cls.find = find
async def findOne(query, sort_by="created_at", asc=False) -> cls:
elastic_query = {
"size": 1,
"query": {
"bool": {
"should": [],
"must_not": [],
"must": []
}
},
"sort": [
{"created_at": {"order": "desc"}}
]
}
for field, value in query.items():
# =========================================================;
# =========================================================;
# =========================================================;
if field in ["_id", "created_at", "updated_at"]:
clause = "must"
if type(value) == dict:
if "$not" in value:
value = value["$not"]
clause = "must_not"
if type(value) == list:
elastic_query["query"]["bool"][clause].append({
"terms": value
})
else:
elastic_query["query"]["bool"][clause].append({
"term": value
})
elif field in cls.field_names:
clause = "must"
if type(value) == dict:
if "$not" in value:
value = value["$not"]
clause = "must_not"
elastic_query = _resolve_value_by_fieldname(field=field, value=value, elastic_query=elastic_query, clause=clause)
if sort_by is not None:
elastic_query["sort"] = [
{
f"{sort_by}": {"order": "desc" if not bool(asc) else "asc"}
}
]
res = await cls.elastic.search(index=cls.index_name, body=elastic_query)
hits = []
if "hits" in res:
if "hits" in res["hits"]:
for hit in res["hits"]["hits"]:
hit["_source"]["_id"] = hit["_id"]
hits.append(hit["_source"])
one = None
for doc in hits:
one = cls.create(doc, exists=True)
return one
cls.findOne = findOne
async def insert(bulk):
print(cls.index_name)
documents = []
for doc in bulk:
try:
new_doc = {}
new_doc['_id'] = str(functions.uuid_hex())
new_doc['_index'] = cls.index_name
new_doc["_source"] = {}
for field in doc:
val = doc[field]
cast_val = val
if field in cls.field_names:
if issubclass(type(cls.field_names[field]), bool): # this if HAS to come before the int case, because
cast_val = functions.resolve_bool(val)
elif issubclass(type(cls.field_names[field]), float):
cast_val = float(val)
elif issubclass(type(cls.field_names[field]), int):
cast_val = int(val)
else:
cast_val = str(val)
new_doc["_source"][field] = cast_val
new_doc["_source"]['created_at'] = functions.get_current_timestamp()
new_doc["_source"]['updated_at'] = functions.get_current_timestamp()
documents.append(new_doc)
except Exception as e:
traceback_info = traceback.format_exc()
print(" +", traceback_info)
print(e)
success, failed = await helpers.async_bulk(cls.elastic, actions=documents)
await asyncio.sleep(0.5)
return success, failed
cls.insert = insert
async def deleteWhere(query):
elastic_query = {
"query": {
"bool": {
"should": [],
"must_not": [],
"must": []
}
},
"sort": [
{"created_at": {"order": "desc"}}
]
}
for field, value in query.items():
# =========================================================;
# =========================================================;
# =========================================================;
if field in ["_id", "created_at", "updated_at"]:
clause = "must"
if type(value) == dict:
if "$not" in value:
value = value["$not"]
clause = "must_not"
if type(value) == list:
elastic_query["query"]["bool"][clause].append({
"terms": value
})
else:
elastic_query["query"]["bool"][clause].append({
"term": value
})
elif field in cls.field_names:
clause = "must"
if type(value) == dict:
if "$not" in value:
value = value["$not"]
clause = "must_not"
elastic_query = _resolve_value_by_fieldname(field=field, value=value, elastic_query=elastic_query, clause=clause)
response = await cls.elastic.delete_by_query(index=cls.index_name, body=elastic_query)
await asyncio.sleep(0.5)
deleted = None
if 'deleted' in response:
print(f"{C.cyan}[Olastic] Info:{C.end} Deleted {response['deleted']} documents.")
deleted = response['deleted']
else:
print(f"{C.err}[Olastic] Error:{C.end} Failed to perform delete-by-query operation.")
return deleted
cls.deleteWhere = deleteWhere
async def updateWhere(values, query):
elastic_query = {
"query": {
"bool": {
"should": [],
"must_not": [],
"must": []
}
}
}
for field, value in query.items():
# =========================================================;
# =========================================================;
# =========================================================;
if field in ["_id", "created_at", "updated_at"]:
clause = "must"
if type(value) == dict:
if "$not" in value:
value = value["$not"]
clause = "must_not"
if type(value) == list:
elastic_query["query"]["bool"][clause].append({
"terms": value
})
else:
elastic_query["query"]["bool"][clause].append({
"term": value
})
elif field in cls.field_names:
clause = "must"
if type(value) == dict:
if "$not" in value:
value = value["$not"]
clause = "must_not"
elastic_query = _resolve_value_by_fieldname(field=field, value=value, elastic_query=elastic_query, clause=clause)
params = {}
source = ""
for field, value in values.items():
if field in cls.field_names:
cast_val = value
if issubclass(type(cls.field_names[field]), bool): # this if HAS to come before the int case, because
cast_val = functions.resolve_bool(value)
elif issubclass(type(cls.field_names[field]), float):
cast_val = float(value)
elif issubclass(type(cls.field_names[field]), int):
cast_val = int(value)
else:
cast_val = str(value)
if field != "_id":
params[field] = cast_val
source += f"ctx._source.{field} = params.{field};"
params["updated_at"] = functions.get_current_timestamp()
source += f"ctx._source.updated_at = params.updated_at;"
elastic_query["script"] = {
"source": source,
"lang": "painless",
"params": params
}
response = await cls.elastic.update_by_query(index=cls.index_name, body=elastic_query)
await asyncio.sleep(0.4)
updated = None
if 'updated' in response:
print(f"{C.ok}[Olastic] Info:{C.end} Updated {response['updated']} documents.")
updated = response['updated']
else:
print(f"{C.err}[Olastic] Error:{C.end} Failed to perform update-by-query operation.")
return updated
cls.updateWhere = updateWhere
async def deleteAll():
if (await cls.elastic.indices.exists(index=cls.index_name)):
response = await cls.elastic.delete_by_query(
index=cls.index_name,
body={
"query": {
"match_all": {}
}
}
)
await asyncio.sleep(0.5)
if response.get('deleted', 0) > 0:
return True
else:
return False
return False
cls.deleteAll = deleteAll
async def termsAgg(fieldname: str, query: dict = {}):
buckets = []
if fieldname in cls.field_names:
elastic_query = {
"query": {
"bool": {
"should": [],
"must_not": [],
"must": []
}
},
"aggs": {
f"{fieldname}_terms_agg": {
"terms": {
"field": fieldname,
"size": 10_000
}
}
}
}
for field, value in query.items():
# =========================================================;
# =========================================================;
# =========================================================;
if field in ["_id", "created_at", "updated_at"]:
clause = "must"
if type(value) == dict:
if "$not" in value:
value = value["$not"]
clause = "must_not"
if type(value) == list:
elastic_query["query"]["bool"][clause].append({
"terms": value
})
else:
elastic_query["query"]["bool"][clause].append({
"term": value
})
elif field in cls.field_names:
clause = "must"
if type(value) == dict:
if "$not" in value:
value = value["$not"]
clause = "must_not"
elastic_query = _resolve_value_by_fieldname(field=field, value=value, elastic_query=elastic_query, clause=clause)
result = await cls.elastic.search(index=cls.index_name, body=elastic_query)
buckets = result['aggregations'][f"{fieldname}_terms_agg"]['buckets']
for i, bucket in enumerate(buckets):
buckets[i] = {
f"{fieldname}": bucket["key"],
"doc_count": bucket["doc_count"]
}
cls.termsAgg = termsAgg
async def save(self):
params = {}
source = ""
for field, value in self.__dict__.items():
if field in cls.field_names:
cast_val = value
if issubclass(type(cls.field_names[field]), bool): # this if HAS to come before the int case, because
cast_val = functions.resolve_bool(value)
elif issubclass(type(cls.field_names[field]), float):
cast_val = float(value)
elif issubclass(type(cls.field_names[field]), int):
cast_val = int(value)
else:
cast_val = str(value)
params[field] = cast_val
source += f"ctx._source.{field} = params.{field};"
# print(f"{C.cyan}[Olastic] Info:{C.end} checking if exists ...")
doc_exists = await self.elastic.exists(index=cls.index_name, id=self._id)
# print(f"{C.cyan}[Olastic] Info:{C.end} check result: [{bool(doc_exists)}]")
if doc_exists:
# print(f"{C.cyan}[Olastic] Info:{C.end} Updating ...")
params["updated_at"] = functions.get_current_timestamp()
source += f"ctx._source.updated_at = params.updated_at;"
if "_id" in params:
del params["_id"]
source = source.replace(f"ctx._source._id = params._id;", "")
# print(f"{C.cyan}[Olastic] Info:{C.end} removed _id override")
update_script = {
"script": {
"source": source,
"lang": "painless",
"params": params
}
}
try:
response = await cls.elastic.update(index=cls.index_name, id=self._id, body=update_script, request_timeout=200)
except Exception as e:
print(f"{C.err}[Olastic] Error:{C.end} Blocked request !!!!")
print(e)
if response['result'] == "updated":
print(f"{C.ok}[Olastic] Info:{C.end} Updated existing document.")
return self
else:
print(f"{C.err}[Olastic] Error:{C.end} Updated existing document.")
return self
else:
response = await cls.elastic.index(index=cls.index_name, id=self._id, body=params)
# Check the response
if response['result'] == 'created':
print(f"{C.ok}[Olastic] Info:{C.end} Document created.")
return self
else:
print(f"{C.err}[Olastic] Error:{C.end} Failed to save document")
return self
cls.save = save
return cls
class OQueryInterface:
def sortBy(self, field, asc=True):
pass
def limit(self, start=0, size=10_000):
pass
async def exec(self, asDicts=False):
pass
async def count(self):
pass
async def update(self, values):
pass
async def delete(self):
pass
async def aggregate(self, aggs):
pass
class Schema(Olastic):
# Schema.__init__ has to be defined in Schema because it has to replace Olastic.__init__
def __init__(self):
# self._id = str(functions.uuid_hex())
pass
def save(self):
pass
@classmethod
def create(cls, doc_dict, exists=False):
pass
@classmethod
def find(cls, query, start=0, end=10_000) -> OQueryInterface:
pass
@classmethod
def findOne(cls, query, sort_by, asc):
pass
@classmethod
def insert(cls, bulk):
pass
@classmethod
def updateWhere(cls, values, query):
pass
@classmethod
def deleteWhere(cls, query):
pass
@classmethod
def deleteAll(cls):
pass
@classmethod
def termsAgg(cls, fieldname: str):
pass
class OlasticQuery(OQueryInterface):
def __init__(self, search_query: dict, cls: Schema):
self.search_query: dict = search_query
self.schema: Schema = cls
def sortBy(self, field, asc=True) -> OQueryInterface:
self.search_query["sort"] = [
{f"{field}": {"order": "asc" if asc else "desc"}}
]
return self
def limit(self, start=0, size=10_000) -> OQueryInterface:
self.search_query["from"] = start
self.search_query["size"] = size
return self
async def exec(self, asDicts=False):
res = await self.schema.elastic.search(index=self.schema.index_name, body=self.search_query)
hits = []
# print(json.dumps(res["hits"]["hits"], indent=4))
if "hits" in res:
if "hits" in res["hits"]:
for hit in res["hits"]["hits"]:
# print(hit["_id"])
# print(json.dumps(hit, indent=4))
hit["_source"]["_id"] = hit["_id"]
hits.append(hit["_source"])
arr = []
if not asDicts:
for doc in hits:
arr.append(self.schema.create(doc, True))
else:
for doc in hits:
arr.append(doc)
return arr
async def count(self):
del self.search_query["sort"]
del self.search_query["size"]
if "aggs" in self.search_query:
del self.search_query["aggs"]
if "from" in self.search_query:
del self.search_query["from"]
result = await self.schema.elastic.count(index=self.schema.index_name, body=self.search_query)
return result['count']
async def update(self, values):
params = {}
source = ""
for field, value in values.items():
if field in self.schema.field_names:
cast_val = value
if issubclass(type(self.schema.field_names[field]), bool): # this if HAS to come before the int case, because int is a superset of bool in python
cast_val = functions.resolve_bool(value)
elif issubclass(type(self.schema.field_names[field]), float):
cast_val = float(value)
elif issubclass(type(self.schema.field_names[field]), int):
cast_val = int(value)
else:
cast_val = str(value)
if field != "_id" and field != "created_at":
params[field] = cast_val
source += f"ctx._source.{field} = params.{field};"
params["updated_at"] = functions.get_current_timestamp()
source += f"ctx._source.updated_at = params.updated_at;"
self.search_query["script"] = {
"source": source,
"lang": "painless",
"params": params
}
response = await self.schema.elastic.update_by_query(index=self.schema.index_name, body=self.search_query)
await asyncio.sleep(0.4)
updated = None
if 'updated' in response:
print(f"{C.ok}[Olastic] Info:{C.end} Updated {response['updated']} documents.")
updated = response['updated']
else:
print(f"{C.err}[Olastic] Error:{C.end} Failed to perform update-by-query operation.")
return updated
async def delete(self):
response = await self.schema.elastic.delete_by_query(index=self.schema.index_name, body=self.search_query)
await asyncio.sleep(0.4)
deleted = None
if 'deleted' in response:
print(f"{C.cyan}[Olastic] Info:{C.end} Deleted {response['deleted']} documents.")
deleted = response['deleted']
else:
print(f"{C.err}[Olastic] Error:{C.end} Failed to perform delete-by-query operation.")
return deleted
async def aggregate(self, aggs):
self.search_query["size"] = 0
self.search_query["aggs"] = aggs
result = await self.schema.elastic.search(index=self.schema.index_name, body=self.search_query)
return result