コード例 #1
0
ファイル: views.py プロジェクト: silky/courtlistener
def do_search(
    get_params,
    rows=20,
    override_params=None,
    facet=True,
    cache_key=None,
):
    """Do all the difficult solr work.

    :param get_params: The request.GET parameters sent by the user. Note that
    this cannot simply be request.GET since that is immutable and
    override_params needs to be able to change this. Instead generally it's
    best to send request.GET.copy().
    :param rows: The number of solr results to request
    :param override_params: A dict with additional or different GET params to
    be sent to solr.
    :param facet: Whether to complete faceting in the query
    :param cache_key: A cache key with which to save the results. Note that it
    does not do anything clever with the actual query, so if you use this, your
    cache key should *already* have factored in the query. If None, no caching
    is set or used. Results are saved for six hours.
    :return A big dict of variables for use in the search results, homepage, or
    other location.
    """
    query_citation = None
    error = False
    paged_results = None
    cited_cluster = None
    courts = Court.objects.filter(in_use=True)
    related_cluster_pks = None

    # Add additional or overridden GET parameters
    if override_params:
        get_params.update(override_params)
    search_form = SearchForm(get_params)

    if search_form.is_valid():
        cd = search_form.cleaned_data

        # Do the query, hitting the cache if desired
        try:
            si = get_solr_interface(cd)
        except NotImplementedError:
            logger.error(
                "Tried getting solr connection for %s, but it's not "
                "implemented yet",
                cd["type"],
            )
            raise

        try:
            # Is this a `related:<pks>` prefix query?
            related_prefix_match = RELATED_PATTERN.search(cd["q"])
            if related_prefix_match:
                # Seed IDs
                related_cluster_pks = related_prefix_match.group("pks").split(
                    ",")
                results = get_mlt_query(
                    si,
                    cd.copy(),
                    facet,
                    related_cluster_pks,
                    # Original query
                    cd["q"].replace(related_prefix_match.group("pfx"), ""),
                )
            else:
                # Regular search queries
                results = si.query().add_extra(
                    **build_main_query(cd, facet=facet))

            paged_results = paginate_cached_solr_results(
                get_params, cd, results, rows, cache_key)
            cited_cluster = add_depth_counts(
                # Also returns cited cluster if found
                search_data=cd,
                search_results=paged_results,
            )
        except (NotImplementedError, RequestException, SolrError) as e:
            error = True
            logger.warning("Error loading search page with request: %s" %
                           get_params)
            logger.warning("Error was: %s" % e)
            if settings.DEBUG is True:
                traceback.print_exc()

        # A couple special variables for particular search types
        search_form = _clean_form(get_params, cd, courts)
        if cd["type"] in [
                SEARCH_TYPES.OPINION,
                SEARCH_TYPES.RECAP,
                SEARCH_TYPES.DOCKETS,
        ]:
            query_citation = get_query_citation(cd)

        if cd["type"] in [
                SEARCH_TYPES.RECAP,
                SEARCH_TYPES.DOCKETS,
                SEARCH_TYPES.PEOPLE,
        ]:
            panels = Court.FEDERAL_BANKRUPTCY_PANEL
            courts = courts.filter(
                pacer_court_id__isnull=False,
                end_date__isnull=True).exclude(jurisdiction=panels)
    else:
        error = True

    courts, court_count_human, court_count = merge_form_with_courts(
        courts, search_form)
    search_summary_str = search_form.as_text(court_count_human)
    search_summary_dict = search_form.as_display_dict(court_count_human)
    related_cluster = (OpinionCluster.objects.get(
        sub_opinions__pk__in=related_cluster_pks)
                       if related_cluster_pks else None)

    return {
        "results": paged_results,
        "facet_fields": make_stats_variable(search_form, paged_results),
        "search_form": search_form,
        "search_summary_str": search_summary_str,
        "search_summary_dict": search_summary_dict,
        "courts": courts,
        "court_count_human": court_count_human,
        "court_count": court_count,
        "query_citation": query_citation,
        "error": error,
        "cited_cluster": cited_cluster,
        "related_cluster": related_cluster,
    }
コード例 #2
0
def do_search(request,
              rows=20,
              order_by=None,
              type=None,
              facet=True,
              cache_key=None):
    """Do all the difficult solr work.

    :param request: The request made by the user
    :param rows: The number of solr results to request
    :param order_by: An opportunity to override the ordering of the search
    results
    :param type: An opportunity to override the type
    :param facet: Whether to complete faceting in the query
    :param cache_key: A cache key with which to save the results. Note that it
    does not do anything clever with the actual query, so if you use this, your
    cache key should *already* have factored in the query. If None, no caching
    is set or used. Results are saved for six hours.
    :return A big dict of variables for use in the search results, homepage, or
    other location.
    """
    query_citation = None
    error = False
    paged_results = None
    search_form = SearchForm(request.GET)
    courts = Court.objects.filter(in_use=True)

    if search_form.is_valid():
        cd = search_form.cleaned_data
        # Allows an override by calling methods.
        if order_by is not None:
            cd['order_by'] = order_by
        if type is not None:
            cd['type'] = type

        # Do the query, hitting the cache if desired
        try:
            results = get_solr_result_objects(cd, facet)
            paged_results = paginate_cached_solr_results(
                request, cd, results, rows, cache_key)
        except (NotImplementedError, RequestException, SolrError) as e:
            error = True
            logger.warning("Error loading search page with "
                           "request: %s" % request.GET)
            logger.warning("Error was: %s" % e)
            if settings.DEBUG is True:
                traceback.print_exc()

        # A couple special variables for particular search types
        search_form = _clean_form(request, cd, courts)
        if cd['type'] == 'o':
            query_citation = get_query_citation(cd)
        elif cd['type'] == 'r':
            panels = Court.FEDERAL_BANKRUPTCY_PANEL
            courts = (courts.filter(
                pacer_court_id__isnull=False,
                end_date__isnull=True).exclude(jurisdiction=panels))
    else:
        error = True

    courts, court_count_human, court_count = merge_form_with_courts(
        courts, search_form)
    search_summary_str = search_form.as_text(court_count, court_count_human)

    return {
        'results': paged_results,
        'facet_fields': make_stats_variable(search_form, paged_results),
        'search_form': search_form,
        'search_summary_str': search_summary_str,
        'courts': courts,
        'court_count_human': court_count_human,
        'court_count': court_count,
        'query_citation': query_citation,
        'error': error,
    }
コード例 #3
0
ファイル: views.py プロジェクト: paulyc/courtlistener
def do_search(
    get_params,
    rows=20,
    override_params=None,
    facet=True,
    cache_key=None,
):
    """Do all the difficult solr work.

    :param get_params: The request.GET parameters sent by the user. Note that
    this cannot simply be request.GET since that is immutable and
    override_params needs to be able to change this. Instead generally it's
    best to send request.GET.copy().
    :param rows: The number of solr results to request
    :param override_params: A dict with additional or different GET params to
    be sent to solr.
    :param facet: Whether to complete faceting in the query
    :param cache_key: A cache key with which to save the results. Note that it
    does not do anything clever with the actual query, so if you use this, your
    cache key should *already* have factored in the query. If None, no caching
    is set or used. Results are saved for six hours.
    :return A big dict of variables for use in the search results, homepage, or
    other location.
    """
    query_citation = None
    error = False
    paged_results = None
    courts = Court.objects.filter(in_use=True)

    # Add additional or overridden GET parameters
    if override_params:
        get_params.update(override_params)
    search_form = SearchForm(get_params)

    if search_form.is_valid():
        cd = search_form.cleaned_data

        # Do the query, hitting the cache if desired
        try:
            results = get_solr_result_objects(cd, facet)
            paged_results = paginate_cached_solr_results(
                get_params, cd, results, rows, cache_key)
        except (NotImplementedError, RequestException, SolrError) as e:
            error = True
            logger.warning("Error loading search page with request: %s" %
                           get_params)
            logger.warning("Error was: %s" % e)
            if settings.DEBUG is True:
                traceback.print_exc()

        # A couple special variables for particular search types
        search_form = _clean_form(get_params, cd, courts)
        if cd["type"] in [SEARCH_TYPES.OPINION, SEARCH_TYPES.RECAP]:
            query_citation = get_query_citation(cd)

        if cd["type"] == SEARCH_TYPES.RECAP:
            panels = Court.FEDERAL_BANKRUPTCY_PANEL
            courts = courts.filter(
                pacer_court_id__isnull=False,
                end_date__isnull=True).exclude(jurisdiction=panels)
    else:
        error = True

    courts, court_count_human, court_count = merge_form_with_courts(
        courts, search_form)
    search_summary_str = search_form.as_text(court_count_human)
    search_summary_dict = search_form.as_display_dict(court_count_human)
    cited_cluster = add_depth_counts(  # Also returns cited cluster if found
        search_data=cd,
        search_results=paged_results,
    )

    return {
        "results": paged_results,
        "facet_fields": make_stats_variable(search_form, paged_results),
        "search_form": search_form,
        "search_summary_str": search_summary_str,
        "search_summary_dict": search_summary_dict,
        "courts": courts,
        "court_count_human": court_count_human,
        "court_count": court_count,
        "query_citation": query_citation,
        "error": error,
        "cited_cluster": cited_cluster,
    }
コード例 #4
0
ファイル: views.py プロジェクト: umeboshi2/courtlistener
def do_search(request, rows=20, order_by=None, type=None, facet=True):

    query_citation = None
    error = False
    paged_results = None
    search_form = SearchForm(request.GET)
    courts = Court.objects.filter(in_use=True)

    if search_form.is_valid():
        cd = search_form.cleaned_data
        # Allows an override by calling methods.
        if order_by is not None:
            cd['order_by'] = order_by
        if type is not None:
            cd['type'] = type
        search_form = _clean_form(request, cd, courts)

        if cd['type'] == 'o':
            si = ExtraSolrInterface(settings.SOLR_OPINION_URL, mode='r')
            results = si.query().add_extra(**build_main_query(cd, facet=facet))
            query_citation = get_query_citation(cd)
        elif cd['type'] == 'r':
            si = ExtraSolrInterface(settings.SOLR_RECAP_URL, mode='r')
            results = si.query().add_extra(**build_main_query(cd, facet=facet))
        elif cd['type'] == 'oa':
            si = ExtraSolrInterface(settings.SOLR_AUDIO_URL, mode='r')
            results = si.query().add_extra(**build_main_query(cd, facet=facet))
        elif cd['type'] == 'p':
            si = ExtraSolrInterface(settings.SOLR_PEOPLE_URL, mode='r')
            results = si.query().add_extra(**build_main_query(cd, facet=facet))

        # Set up pagination
        try:
            if cd['type'] == 'r':
                rows = 10
            paginator = Paginator(results, rows)
            page = request.GET.get('page', 1)
            try:
                paged_results = paginator.page(page)
            except PageNotAnInteger:
                paged_results = paginator.page(1)
            except EmptyPage:
                # Page is out of range (e.g. 9999), deliver last page.
                paged_results = paginator.page(paginator.num_pages)
        except Exception as e:
            # Catches any Solr errors, and aborts.
            logger.warning("Error loading pagination on search page with "
                           "request: %s" % request.GET)
            logger.warning("Error was: %s" % e)
            if settings.DEBUG is True:
                traceback.print_exc()
            error = True

        # Post processing of the results
        regroup_snippets(paged_results)

    else:
        error = True

    courts, court_count_human, court_count = merge_form_with_courts(
        courts, search_form)
    search_summary_str = search_form.as_text(court_count, court_count_human)
    return {
        'results': paged_results,
        'search_form': search_form,
        'search_summary_str': search_summary_str,
        'courts': courts,
        'court_count_human': court_count_human,
        'court_count': court_count,
        'query_citation': query_citation,
        'facet_fields': make_stats_variable(search_form, paged_results),
        'error': error,
    }
コード例 #5
0
ファイル: views.py プロジェクト: freelawproject/courtlistener
def do_search(request, rows=20, order_by=None, type=None, facet=True,
              cache_key=None):
    """Do all the difficult solr work.

    :param request: The request made by the user
    :param rows: The number of solr results to request
    :param order_by: An opportunity to override the ordering of the search
    results
    :param type: An opportunity to override the type
    :param facet: Whether to complete faceting in the query
    :param cache_key: A cache key with which to save the results. Note that it
    does not do anything clever with the actual query, so if you use this, your
    cache key should *already* have factored in the query. If None, no caching
    is set or used. Results are saved for six hours.
    :return A big dict of variables for use in the search results, homepage, or
    other location.
    """
    query_citation = None
    error = False
    paged_results = None
    search_form = SearchForm(request.GET)
    courts = Court.objects.filter(in_use=True)

    if search_form.is_valid():
        cd = search_form.cleaned_data
        # Allows an override by calling methods.
        if order_by is not None:
            cd['order_by'] = order_by
        if type is not None:
            cd['type'] = type

        # Do the query, hitting the cache if desired
        # noinspection PyBroadException
        try:
            results = get_solr_result_objects(cd, facet)
            paged_results = paginate_cached_solr_results(request, cd, results,
                                                         rows, cache_key)
        except (NotImplementedError, RequestException, SolrError) as e:
            error = True
            logger.warning("Error loading search page with "
                           "request: %s" % request.GET)
            logger.warning("Error was: %s" % e)
            if settings.DEBUG is True:
                traceback.print_exc()

        # A couple special variables for particular search types
        search_form = _clean_form(request, cd, courts)
        if cd['type'] == 'o':
            query_citation = get_query_citation(cd)
        elif cd['type'] == 'r':
            panels = Court.FEDERAL_BANKRUPTCY_PANEL
            courts = (courts.filter(pacer_court_id__isnull=False,
                                    end_date__isnull=True)
                            .exclude(jurisdiction=panels))
    else:
        error = True

    courts, court_count_human, court_count = merge_form_with_courts(
        courts, search_form)
    search_summary_str = search_form.as_text(court_count, court_count_human)

    return {
        'results': paged_results,
        'facet_fields': make_stats_variable(search_form, paged_results),
        'search_form': search_form,
        'search_summary_str': search_summary_str,
        'courts': courts,
        'court_count_human': court_count_human,
        'court_count': court_count,
        'query_citation': query_citation,
        'error': error,
    }