models.py 文件源码

python
阅读 18 收藏 0 点赞 0 评论 0

项目:metastore 作者: datahq 项目源码 文件源码
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)
        }
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号