Esempio n. 1
0
def search(collection, p, of, ot, so, sf, sp, rm, rg, jrec):
    """Render search page."""
    if 'action_browse' in request.args \
            or request.args.get('action', '') == 'browse':
        return browse()

    if 'c' in request.args and len(request.args) == 1 \
            and len(request.args.getlist('c')) == 1:
        return redirect(url_for('.collection', name=request.args.get('c')))

    if 'f' in request.args:
        args = request.args.copy()
        args['p'] = "{0}:{1}".format(args['f'], args['p'])
        del args['f']
        return redirect(url_for('.search', **args))

    # fix for queries like `/search?p=+ellis`
    p = p.strip().encode('utf-8')

    response = Query(p).search(collection=collection.name)
    response.body.update({
        'size': int(rg),
        'from': jrec-1,
        'aggs': cfg['SEARCH_ELASTIC_AGGREGATIONS'].get(
            collection.name.lower(), {}
        )
    })

    if sf in cfg['SEARCH_ELASTIC_SORT_FIELDS']:
        so = so if so in ('asc', 'desc') else ''
        sorting = {
            'sort': {
                sf: {
                    'order': so
                }
            }
        }
        response.body.update(sorting)

    # FIXME refactor to separate search hook
    filtered_facets = ''
    from invenio_search.walkers.elasticsearch import ElasticSearchDSL
    if 'post_filter' in request.values and request.values['post_filter']:
        parsed_post_filter = Query(request.values.get('post_filter'))
        post_filter = parsed_post_filter.query.accept(
            ElasticSearchDSL()
        )
        response.body['query'] = {
            "filtered": {
                'query': response.body['query'],
                'filter': post_filter
            }
        }
        # extracting the facet filtering
        from invenio_search.walkers.facets import FacetsVisitor
        filtered_facets = parsed_post_filter.query.accept(
            FacetsVisitor()
        )
        # sets cannot be converted to json. use facetsVisitor to convert them
        # to lists
        filtered_facets = FacetsVisitor.jsonable(filtered_facets)
    else:
        # Save current query and number of hits in the user session
        session_key = 'last-query' + p + collection.name
        if not session.get(session_key):
            session[session_key] = {}

        session[session_key] = {
            "p": p,
            "collection": collection.name,
            "number_of_hits": len(response),
            "timestamp": datetime.datetime.utcnow()
        }

    number_of_hits = len(response)

    if number_of_hits and jrec > number_of_hits:
        args = request.args.copy()
        args['jrec'] = 1
        return redirect(url_for('.search', **args))

    pagination = Pagination((jrec-1) // rg + 1, rg, number_of_hits)

    ctx = dict(
        facets={},  # facets.get_facets_config(collection, qid),
        filtered_facets=filtered_facets,
        response=response,
        rg=rg,
        ot=ot,
        pagination=pagination,
        collection=collection,
    )

    # TODO add search services
    # TODO add external collection search

    return response_formated_records(response.records(), of, **ctx)
def search(collection, p, of, ot, so, sf, sp, rm, rg, jrec):
    """Render search page."""
    if 'action_browse' in request.args \
            or request.args.get('action', '') == 'browse':
        return browse()

    if 'c' in request.args and len(request.args) == 1 \
            and len(request.args.getlist('c')) == 1:
        return redirect(url_for('.collection', name=request.args.get('c')))

    if 'f' in request.args:
        args = request.args.copy()
        args['p'] = "{0}:{1}".format(args['f'], args['p'])
        del args['f']
        return redirect(url_for('.search', **args))

    # fix for queries like `/search?p=+ellis`
    p = p.strip().encode('utf-8')

    collection_breadcrumbs(collection)

    response = Query(p).search(collection=collection.name)
    response.body.update({
        'size': int(rg),
        'from': jrec-1,
        'aggs': {
            "collection": {"terms": {"field": "_collections"}},
            "author": {"terms": {"field": "authors.raw"}},
        },
    })

    # FIXME refactor to separate search hook
    filtered_facets = ''
    from invenio_search.walkers.elasticsearch import ElasticSearchDSL
    if 'post_filter' in request.values:
        parsed_post_filter = Query(request.values.get('post_filter'))
        post_filter = parsed_post_filter.query.accept(
            ElasticSearchDSL()
        )
        response.body['post_filter'] = post_filter
        # extracting the facet filtering
        from invenio_search.walkers.facets import FacetsVisitor
        filtered_facets = parsed_post_filter.query.accept(
            FacetsVisitor()
        )
        # sets cannot be converted to json. use facetsVisitor to convert them to
        # lists
        filtered_facets = FacetsVisitor.jsonable(filtered_facets)

    if len(response) and jrec > len(response):
        args = request.args.copy()
        args['jrec'] = 1
        return redirect(url_for('.search', **args))

    pagination = Pagination((jrec-1) // rg + 1, rg, len(response))

    ctx = dict(
        facets={},  # facets.get_facets_config(collection, qid),
        filtered_facets=filtered_facets,
        response=response,
        rg=rg,
        create_nearest_terms_box=lambda: _("Try to modify the query."),
        easy_search_form=EasySearchForm(csrf_enabled=False),
        ot=ot,
        pagination=pagination,
        collection=collection,
    )

    # TODO add search services
    # TODO add external collection search

    return response_formated_records(response.records(), of, **ctx)
Esempio n. 3
0
def search(collection, p, of, ot, so, sf, sp, rm, rg, jrec):
    """Render search page."""
    if 'action_browse' in request.args \
            or request.args.get('action', '') == 'browse':
        return browse()

    if 'c' in request.args and len(request.args) == 1 \
            and len(request.args.getlist('c')) == 1:
        return redirect(url_for('.collection', name=request.args.get('c')))

    if 'f' in request.args:
        args = request.args.copy()
        args['p'] = "{0}:{1}".format(args['f'], args['p'])
        del args['f']
        return redirect(url_for('.search', **args))

    # fix for queries like `/search?p=+ellis`
    p = p.strip().encode('utf-8')

    collection_breadcrumbs(collection)

    response = Query(p).search(collection=collection.name)
    response.body.update({
        'size': int(rg),
        'from': jrec-1,
        'aggs': {
            "collection": {"terms": {"field": "_collections"}},
            "author": {"terms": {"field": "authors.raw"}},
        },
    })

    # FIXME refactor to separate search hook
    filtered_facets = ''
    from invenio_search.walkers.elasticsearch import ElasticSearchDSL
    if 'post_filter' in request.values:
        parsed_post_filter = Query(request.values.get('post_filter'))
        post_filter = parsed_post_filter.query.accept(
            ElasticSearchDSL()
        )
        response.body['post_filter'] = post_filter
        # extracting the facet filtering
        from invenio_search.walkers.facets import FacetsVisitor
        filtered_facets = parsed_post_filter.query.accept(
            FacetsVisitor()
        )
        # sets cannot be converted to json. use facetsVisitor to convert them to
        # lists
        filtered_facets = FacetsVisitor.jsonable(filtered_facets)

    if len(response) and jrec > len(response):
        args = request.args.copy()
        args['jrec'] = 1
        return redirect(url_for('.search', **args))

    pagination = Pagination((jrec-1) // rg + 1, rg, len(response))

    ctx = dict(
        facets={},  # facets.get_facets_config(collection, qid),
        filtered_facets=filtered_facets,
        response=response,
        rg=rg,
        create_nearest_terms_box=lambda: _("Try to modify the query."),
        easy_search_form=EasySearchForm(csrf_enabled=False),
        ot=ot,
        pagination=pagination,
        collection=collection,
    )

    # TODO add search services
    # TODO add external collection search

    return response_formated_records(response.records(), of, **ctx)