From 6f004e73fd41f29d26df28479c231289227eac16 Mon Sep 17 00:00:00 2001 From: han0 Date: Fri, 17 Mar 2023 15:06:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=20core.search.es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nc_http/core/search/es/__init__.py | 0 .../core/search/{es.py => es/elastic_base.py} | 0 nc_http/core/search/es/func.py | 25 +++++++++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 nc_http/core/search/es/__init__.py rename nc_http/core/search/{es.py => es/elastic_base.py} (100%) create mode 100644 nc_http/core/search/es/func.py diff --git a/nc_http/core/search/es/__init__.py b/nc_http/core/search/es/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/nc_http/core/search/es.py b/nc_http/core/search/es/elastic_base.py similarity index 100% rename from nc_http/core/search/es.py rename to nc_http/core/search/es/elastic_base.py diff --git a/nc_http/core/search/es/func.py b/nc_http/core/search/es/func.py new file mode 100644 index 0000000..51e31fe --- /dev/null +++ b/nc_http/core/search/es/func.py @@ -0,0 +1,25 @@ +import json + +import requests +from elasticsearch_dsl import connections + + +def clean_es(host, index_name, type_=None): + """ + 清空 es + :param host: + :param index_name: + :param type_: + :return: + """ + connections.create_connection(hosts=[host]) + headers = {'Content-Type': 'application/json'} + # 删除 index + url = '{}/{}/_delete_by_query'.format(host, index_name) + if not type_: + data = {"query": {"match_all": {}}} + else: + data = {"query": {"term": {"type": {"value": type_}}}} + result = requests.post(url, json.dumps(data), headers=headers) + r = json.loads(result.content) + print(r)