feat: 新增 search
This commit is contained in:
0
nc_http/core/search/__init__.py
Normal file
0
nc_http/core/search/__init__.py
Normal file
31
nc_http/core/search/es.py
Normal file
31
nc_http/core/search/es.py
Normal file
@@ -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
|
Reference in New Issue
Block a user