Skip to content

Commit 3aa1e5f

Browse files
Update elasticsearch_helpers.py
1 parent 658da68 commit 3aa1e5f

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,43 @@
1+
from django.conf import settings
12
from elasticsearch import Elasticsearch
23

4+
__all__ = (
5+
"get_all_indices",
6+
"delete_all_indices",
7+
)
8+
39

410
def get_all_indices(with_protected=False):
511
"""Get all indices.
6-
712
Args:
813
with_protected (bool):
9-
1014
Returns:
1115
list: List of indices.
1216
"""
13-
es = Elasticsearch()
14-
_indices = es.indices.get_alias('*').items()
17+
es = Elasticsearch(**settings.ELASTICSEARCH_DSL["default"])
18+
_indices = es.indices.get_alias(index="*").items()
1519
if with_protected:
1620
return [_i for _i, _o in _indices]
1721
else:
18-
return [_i for _i, _o in _indices if not _i.startswith('.')]
22+
return [_i for _i, _o in _indices if not _i.startswith(".")]
1923

2024

2125
def delete_all_indices(with_protected=False):
2226
"""Delete all indices.
23-
2427
Args:
2528
with_protected (bool):
26-
2729
Returns:
2830
tuple: Tuple of two lists with removed and errored indices.
2931
"""
30-
es = Elasticsearch()
32+
es = Elasticsearch(**settings.ELASTICSEARCH_DSL["default"])
3133
_indices = get_all_indices(with_protected=with_protected)
3234
_ok = []
3335
_fail = []
3436
for _i in _indices:
3537
try:
36-
_res = es.indices.delete(_i)
38+
_res = es.indices.delete(index=_i)
3739
except Exception as err:
3840
_fail.append(_i)
39-
if _res and isinstance(_res, dict) and _res.get('acknowledged', False):
41+
if _res and isinstance(_res, dict) and _res.get("acknowledged", False):
4042
_ok.append(_i)
4143
return _ok, _fail

0 commit comments

Comments
 (0)