def search(request, query): params = search_string_to_params(query) normalized = params_to_search_string(params) main_search = MainappSearch(params, limit=settings.SEARCH_PAGINATION_LENGTH) for error in main_search.errors: messages.error(request, error) try: handle_subscribe_requests( request, params, _("You will now receive notifications about new search results."), _("You will no longer receive notifications."), _("You have already subscribed to this search."), ) except NeedsLoginError as err: return redirect(err.redirect_url) executed = main_search.execute() results = [parse_hit(hit) for hit in executed.hits] context = _search_to_context(normalized, main_search, executed, results, request) context["new_facets"] = aggs_to_context(executed) return render(request, "mainapp/search/search.html", context)
def search_results_only(request, query): """Returns only the result list items. Used for the endless scrolling""" params = search_string_to_params(query) normalized = params_to_search_string(params) after = int(request.GET.get("after", 0)) main_search = MainappSearch(params, offset=after, limit=settings.SEARCH_PAGINATION_LENGTH) executed = main_search.execute() # The mocked results don't have a took value logger.debug("Elasticsearch query took {}ms".format( executed.to_dict().get("took"))) results = [parse_hit(hit) for hit in executed.hits] context = _search_to_context(normalized, main_search, executed, results, request) assert executed.hits.total["relation"] in ["eq", "gte"] total_results = executed.hits.total if not isinstance(total_results, dict): total_results = total_results.to_dict() result = { "results": loader.render_to_string("mainapp/search/results_section.html", context, request), "total_results": total_results, "subscribe_widget": loader.render_to_string("partials/subscribe_widget.html", context, request), "more_link": reverse(search_results_only, args=[normalized]), # TOOD: Currently we need both because the js for the dropdown facet # and document type facet hasn't been unified "facets": executed.facets.to_dict(), "new_facets": aggs_to_context(executed), "query": normalized, } return JsonResponse(result, safe=False)
def test_params_to_search_string(self): expected = "document-type:file,committee radius:50 sort:date_newest word radius anotherword" search_string = params_to_search_string(self.params) self.assertEqual(search_string, expected)
def set_search_params(self, params: dict): self.search_string = params_to_search_string(params)
def params_are_equal(params1: dict, params2: dict): # Comparison should be case-insensitive, as you usually don't subscribe to "school" and "School" at the same time return (params_to_search_string(params1).lower() == params_to_search_string(params2).lower())