def get_result_count(request, version, day_count): """Get the count of results for the past `day_count` number of days GET parameters will be a complete search string :param request: The Django request object :param version: The API version number (ignored for now, but there for later) :param day_count: The number of days to average across. More is slower. :return: A JSON object with the number of hits during the last day_range period. """ search_form = SearchForm(request.GET.copy()) if not search_form.is_valid(): return JsonResponse( {"error": "Invalid SearchForm"}, safe=True, status=HTTP_400_BAD_REQUEST, ) cd = search_form.cleaned_data 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 response = (si.query().add_extra( **build_alert_estimation_query(cd, int(day_count))).execute()) si.conn.http_connection.close() return JsonResponse({"count": response.result.numFound}, safe=True)
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, }