From 3deb269adc70cafff31b2fb069c4880824ee648f Mon Sep 17 00:00:00 2001 From: han0 Date: Thu, 23 Feb 2023 16:19:53 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=20search?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nc_http/core/search/__init__.py | 0 nc_http/core/search/es.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 nc_http/core/search/__init__.py create mode 100644 nc_http/core/search/es.py diff --git a/nc_http/core/search/__init__.py b/nc_http/core/search/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/nc_http/core/search/es.py b/nc_http/core/search/es.py new file mode 100644 index 0000000..aaa2671 --- /dev/null +++ b/nc_http/core/search/es.py @@ -0,0 +1,31 @@ +class ElasticBase: + + index = None # 索引名称 + query = {} # dsl 查询参数 + es = None # es 实例 + + @classmethod + def search(cls, query=None, **params): + query = query or cls.query.copy() + for param in params: + print(param) # 根据参数构建查询 dsl + return query + + @classmethod + def get_list(cls, query, paging=None): + params = {} + if paging: + params['size'] = paging['limit'] + params['from'] = (paging['page'] - 1) * paging['limit'] + + result = cls.es.search(index=cls.index, body=query, params=params) + data = [] + for row in result['hits']['hits']: + source = row['_source'] + source['_score'] = row['_score'] + source['_sort'] = row.get('sort') + data.append(source) + + paging['total'] = result['hits']['total']['value'] + + return data, paging