def query(kind, userid, size=50, **kw):
kind_params = ENABLED_SEARCHES.get(kind)
try:
# Arguments received from a network request come in kw, as a mapping
# between param_name and a list of received values.
# If size was provided by the user, it will be a list, so we take its
# first item.
if type(size) is list:
size = size[0]
if int(size) > 100:
size = 100
from_ = int(kw.pop('from', [0])[0])
api_params = dict([
('index', kind_params['index']),
('doc_type', kind_params['doc_type']),
('size', size),
('from_', from_)
])
body = build_dsl(kind_params, userid, kw)
api_params['body'] = json.dumps(body)
ret = _get_engine().search(**api_params)
logging.info('Performing query %r', kind_params)
logging.info('api_params %r', api_params)
logging.info('ret %r', ret)
if ret.get('hits') is not None:
results = [hit['_source'] for hit in ret['hits']['hits']]
total = ret['hits']['total']
total_bytes = ret.get('aggregations')['total_bytes']['value']
else:
results = []
total = 0
total_bytes = 0
return {
'results': results,
'summary': {
"total": total,
"totalBytes": total_bytes
}
}
except (NotFoundError, json.decoder.JSONDecodeError, ValueError) as e:
logging.error("query: %r" % e)
return {
'results': [],
'summary': {
"total": 0,
"totalBytes": 0
},
'error': str(e)
}
评论列表
文章目录