search.py 文件源码

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

项目:jamdb 作者: CenterForOpenScience 项目源码 文件源码
def get(self, handler):
        # Rip the search object out of the elasticsearch backend
        sort = handler.sort
        search = self.collection._state._backend.raw_backend().search

        if handler.request.query_arguments.get('q'):
            search = search.query(elasticsearch_dsl.Q('query_string', query=handler.request.query_arguments['q'][-1].decode('utf-8')))
        else:
            # This should technically be elsewhere but the search object
            # does not provide a nice way to figure out if there is a query or not.
            search = search.sort({'ref': {
                'order': 'asc',
                'unmapped_type': 'string'
            }})

        if handler.request.query_arguments.get('sort'):
            search = search.sort({sort.key: {
                'order': 'asc' if sort.order == 1 else 'desc',
                'unmapped_type': 'string'
            }})

        # Hacking into the serializer
        handler._serializer = self.get_serializer()
        handler._view.parents = handler._view.parents + (self.collection,)

        start = handler.page * handler.page_size
        wrapper = SearchResultWrapper(search[start:start + handler.page_size])
        return handler.write({
            'meta': {
                'total': wrapper.count(),
                'perPage': handler.page_size
            },
            # TODO
            'links': {},
            'data': [handler.serialize(resource) for resource in wrapper]
        })
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号