-
-
Notifications
You must be signed in to change notification settings - Fork 151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(search): implements changes to search to capture user queries #4471
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Generated by Django 5.1.1 on 2024-09-16 20:25 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("search", "0034_add_harvard_pdf_to_opinioncluster"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="SearchQuery", | ||
fields=[ | ||
( | ||
"id", | ||
models.AutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
( | ||
"date_created", | ||
models.DateTimeField(auto_now_add=True), | ||
), | ||
( | ||
"date_modified", | ||
models.DateTimeField(auto_now=True), | ||
), | ||
("get_params", models.CharField(max_length=255)), | ||
("query_time_ms", models.IntegerField()), | ||
("hit_cache", models.BooleanField()), | ||
], | ||
), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CREATE TABLE "search_searchquery" ( | ||
"id" integer NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, | ||
"date_created" timestamp with time zone NOT NULL, | ||
"date_modified" timestamp with time zone NOT NULL, | ||
"get_params" varchar(255) NOT NULL, | ||
"query_time_ms" integer NOT NULL, | ||
"hit_cache" boolean NOT NULL | ||
); | ||
|
||
COMMIT; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CREATE TABLE "search_searchquery" ( | ||
"id" integer NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, | ||
"date_created" timestamp with time zone NOT NULL, | ||
"date_modified" timestamp with time zone NOT NULL, | ||
"get_params" varchar(255) NOT NULL, | ||
"query_time_ms" integer NOT NULL, | ||
"hit_cache" boolean NOT NULL | ||
); | ||
|
||
COMMIT; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -79,7 +79,13 @@ | |||||
UnbalancedQuotesQuery, | ||||||
) | ||||||
from cl.search.forms import SearchForm, _clean_form | ||||||
from cl.search.models import SEARCH_TYPES, Court, Opinion, OpinionCluster | ||||||
from cl.search.models import ( | ||||||
SEARCH_TYPES, | ||||||
Court, | ||||||
Opinion, | ||||||
OpinionCluster, | ||||||
SearchQuery, | ||||||
) | ||||||
from cl.stats.models import Stat | ||||||
from cl.stats.utils import tally_stat | ||||||
from cl.visualizations.models import SCOTUSMap | ||||||
|
@@ -347,7 +353,6 @@ def show_results(request: HttpRequest) -> HttpResponse: | |||||
All of these paths have tests. | ||||||
""" | ||||||
# Create a search string that does not contain the page numbers | ||||||
|
||||||
get_string = make_get_string(request) | ||||||
get_string_sans_alert = make_get_string( | ||||||
request, ["page", "edit_alert", "show_alert_modal"] | ||||||
|
@@ -514,41 +519,59 @@ def show_results(request: HttpRequest) -> HttpResponse: | |||||
if not is_bot(request): | ||||||
async_to_sync(tally_stat)("search.results") | ||||||
|
||||||
# Perform the search | ||||||
search_type = request.GET.get("type", SEARCH_TYPES.OPINION) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason you tweaked the case statement into an if/else, and moved it up here? |
||||||
|
||||||
if search_type == SEARCH_TYPES.PARENTHETICAL: | ||||||
render_dict.update(do_es_search(request.GET.copy())) | ||||||
elif search_type == SEARCH_TYPES.ORAL_ARGUMENT: | ||||||
# Check if waffle flag is active. | ||||||
if waffle.flag_is_active(request, "oa-es-active"): | ||||||
render_dict.update( | ||||||
do_es_search(request.GET.copy()) | ||||||
) | ||||||
else: | ||||||
render_dict.update(do_search(request.GET.copy())) | ||||||
elif search_type == SEARCH_TYPES.PEOPLE: | ||||||
if waffle.flag_is_active(request, "p-es-active"): | ||||||
render_dict.update( | ||||||
do_es_search(request.GET.copy()) | ||||||
) | ||||||
else: | ||||||
render_dict.update(do_search(request.GET.copy())) | ||||||
elif search_type in [ | ||||||
SEARCH_TYPES.RECAP, | ||||||
SEARCH_TYPES.DOCKETS, | ||||||
]: | ||||||
if waffle.flag_is_active(request, "r-es-active"): | ||||||
search_results = do_es_search(request.GET.copy()) | ||||||
else: | ||||||
search_results = do_search(request.GET.copy()) | ||||||
render_dict.update(search_results) | ||||||
elif search_type == SEARCH_TYPES.OPINION: | ||||||
if waffle.flag_is_active(request, "o-es-active"): | ||||||
render_dict.update( | ||||||
do_es_search(request.GET.copy()) | ||||||
) | ||||||
else: | ||||||
render_dict.update(do_search(request.GET.copy())) | ||||||
elif search_type == SEARCH_TYPES.RECAP_DOCUMENT: | ||||||
render_dict.update(do_es_search(request.GET.copy())) | ||||||
else: | ||||||
render_dict.update(do_search(request.GET.copy())) | ||||||
|
||||||
# Create and save the SearchQuery object | ||||||
SearchQuery.objects.create( | ||||||
get_params=request.GET.urlencode(), | ||||||
query_time_ms=render_dict.get("query_time", 0), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I'd set this to null if we don't have the value, or if the called function always returns the value, I'd do:
Suggested change
By doing that, you'd enforce the calling function to always deliver on its promise. |
||||||
hit_cache=render_dict.get("hit_cache", False), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, defauling to False here seems a bit weird if we know the value will always be in |
||||||
) | ||||||
|
||||||
# Create bare-bones alert form. | ||||||
alert_form = CreateAlertForm( | ||||||
initial={"query": get_string, "rate": "dly"}, | ||||||
user=request.user, | ||||||
) | ||||||
search_type = request.GET.get("type", SEARCH_TYPES.OPINION) | ||||||
match search_type: | ||||||
case SEARCH_TYPES.PARENTHETICAL: | ||||||
render_dict.update(do_es_search(request.GET.copy())) | ||||||
case SEARCH_TYPES.ORAL_ARGUMENT: | ||||||
# Check if waffle flag is active. | ||||||
if waffle.flag_is_active(request, "oa-es-active"): | ||||||
render_dict.update(do_es_search(request.GET.copy())) | ||||||
else: | ||||||
render_dict.update(do_search(request.GET.copy())) | ||||||
case SEARCH_TYPES.PEOPLE: | ||||||
if waffle.flag_is_active(request, "p-es-active"): | ||||||
render_dict.update(do_es_search(request.GET.copy())) | ||||||
else: | ||||||
render_dict.update(do_search(request.GET.copy())) | ||||||
case SEARCH_TYPES.RECAP | SEARCH_TYPES.DOCKETS: | ||||||
if waffle.flag_is_active(request, "r-es-active"): | ||||||
search_results = do_es_search(request.GET.copy()) | ||||||
else: | ||||||
search_results = do_search(request.GET.copy()) | ||||||
render_dict.update(search_results) | ||||||
case SEARCH_TYPES.OPINION: | ||||||
if waffle.flag_is_active(request, "o-es-active"): | ||||||
render_dict.update(do_es_search(request.GET.copy())) | ||||||
else: | ||||||
render_dict.update(do_search(request.GET.copy())) | ||||||
case SEARCH_TYPES.RECAP_DOCUMENT: | ||||||
render_dict.update(do_es_search(request.GET.copy())) | ||||||
case _: | ||||||
render_dict.update(do_search(request.GET.copy())) | ||||||
|
||||||
# Set the value to the query as a convenience | ||||||
alert_form.fields["name"].widget.attrs["value"] = render_dict[ | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Getting models exactly right is important because DB migrations are annoying, so I have a few comments here:
AbstractDateTimeModel
to automatically add the date-created and date_modified fields.db_index
is considered the old way of adding an index. These days, they should go into theMeta
class.