Skip to content

Commit

Permalink
fix(api): Applied the suggested changes and tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
albertisfu committed May 14, 2024
1 parent a9993b4 commit 2cbe748
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 74 deletions.
4 changes: 3 additions & 1 deletion cl/api/pagination.py
Expand Up @@ -95,7 +95,9 @@ class ESCursorPagination(BasePagination):
invalid_cursor_message = "Invalid cursor"
request_date = None

def set_page_request(self, request, search_type) -> datetime.date:
def initialize_context_from_request(
self, request, search_type
) -> datetime.date:
self.base_url = request.build_absolute_uri()
self.request = request
self.search_type = search_type
Expand Down
17 changes: 7 additions & 10 deletions cl/lib/elasticsearch_utils.py
Expand Up @@ -759,16 +759,13 @@ def build_custom_function_score_for_date(
:return: The modified QueryString object with applied function score.
"""

midnight_current_date = (
datetime.datetime.combine(default_current_date, datetime.time())
if default_current_date
else None
)
default_current_time = (
int(midnight_current_date.timestamp() * 1000)
if midnight_current_date
else None
)
default_current_time = None
if default_current_date:
midnight_current_date = datetime.datetime.combine(
default_current_date, datetime.time()
)
default_current_time = int(midnight_current_date.timestamp() * 1000)

sort_field, order = order_by
query = Q(
"function_score",
Expand Down
2 changes: 1 addition & 1 deletion cl/search/api_views.py
Expand Up @@ -233,7 +233,7 @@ def list(self, request, *args, **kwargs):
cd = search_form.cleaned_data
search_type = cd["type"]
paginator = ESCursorPagination()
cd["request_date"] = paginator.set_page_request(
cd["request_date"] = paginator.initialize_context_from_request(
request, search_type
)
search_query = DocketDocument.search()
Expand Down
109 changes: 47 additions & 62 deletions cl/search/tests/tests_es_recap.py
Expand Up @@ -3232,6 +3232,7 @@ def test_stable_scores_date_sort(
]
# Generate tests for all the search types.
all_tests = []

for test_param in tests_params:
for search_type in [
SEARCH_TYPES.RECAP,
Expand All @@ -3242,84 +3243,68 @@ def test_stable_scores_date_sort(
test_param_copy["type"] = search_type
all_tests.append(test_param_copy)

original_datetime = now().replace(day=1, hour=5, minute=0)
for search_params in all_tests:
with self.subTest(
search_params=search_params, msg="Test stable scores."
):
original_datetime = now().replace(day=1, hour=5, minute=0)
# Two first-page requests (no cursor) made on the same day will now
# have consistent scores because scores are now computed based on the
# same day's date.
with time_machine.travel(original_datetime, tick=False):
r = self.client.get(
reverse("search-list", kwargs={"version": "v4"}),
search_params,
)
date_score_1 = r.data["results"][0]["date_score"]

with time_machine.travel(
original_datetime + datetime.timedelta(minutes=1),
tick=False,
):
r = self.client.get(
reverse("search-list", kwargs={"version": "v4"}),
search_params,
)
date_score_2 = r.data["results"][0]["date_score"]
), time_machine.travel(original_datetime, tick=False) as traveler:
# Two first-page requests (no cursor) made on the same day will
# now have consistent scores because scores are now computed
# based on the same day's date.
r = self.client.get(
reverse("search-list", kwargs={"version": "v4"}),
search_params,
)
date_score_1 = r.data["results"][0]["date_score"]

traveler.shift(datetime.timedelta(minutes=1))
r = self.client.get(
reverse("search-list", kwargs={"version": "v4"}),
search_params,
)
date_score_2 = r.data["results"][0]["date_score"]
self.assertEqual(date_score_1, date_score_2)

# Two first-page requests (no cursor) made on different days will
# present scores variations, due to it being a different day and
# date_filed and entry_date_filed are day granular.
with time_machine.travel(
original_datetime + datetime.timedelta(days=1), tick=False
):
r = self.client.get(
reverse("search-list", kwargs={"version": "v4"}),
search_params,
)
date_score_1 = r.data["results"][0]["date_score"]

with time_machine.travel(
original_datetime + datetime.timedelta(days=2), tick=False
):
r = self.client.get(
reverse("search-list", kwargs={"version": "v4"}),
search_params,
)
date_score_2 = r.data["results"][0]["date_score"]
# Two first-page requests (no cursor) made on different days
# will present scores variations, due to it being a different
# day and date_filed and entry_date_filed are day granular.
traveler.shift(datetime.timedelta(days=1))
r = self.client.get(
reverse("search-list", kwargs={"version": "v4"}),
search_params,
)
date_score_1 = r.data["results"][0]["date_score"]

traveler.shift(datetime.timedelta(days=1))
r = self.client.get(
reverse("search-list", kwargs={"version": "v4"}),
search_params,
)
date_score_2 = r.data["results"][0]["date_score"]

self.assertNotEqual(date_score_2, date_score_1)

# Two next_page requests preserver the same scores even thought they
# are executed on different days. Since scores are computed base on the
# cursor context date.
# Two next_page requests preserve the same scores even thought
# they are executed on different days. Since scores are computed
# base on the cursor context date.
next_page = r.data["next"]
with time_machine.travel(
original_datetime + datetime.timedelta(days=3), tick=False
):
r = self.client.get(next_page)
traveler.shift(datetime.timedelta(days=1))
r = self.client.get(next_page)
date_score_next_1 = r.data["results"][0]["date_score"]

# Two next_page requests preserve the same scores even though they are
# executed on different days. Since scores are computed based on the
# cursor context date.
with time_machine.travel(
original_datetime + datetime.timedelta(days=4), tick=False
):
r = self.client.get(next_page)
# Two next_page requests preserve the same scores even though
# they are executed on different days. Since scores are computed
# based on the cursor context date.
traveler.shift(datetime.timedelta(days=1))
r = self.client.get(next_page)
date_score_next_2 = r.data["results"][0]["date_score"]
previous_page = r.data["previous"]
self.assertEqual(date_score_next_1, date_score_next_2)

# Now, going to the previous day, the score should be the same as the
# first request that showed the document.
with time_machine.travel(
original_datetime + datetime.timedelta(days=4), tick=False
):
r = self.client.get(previous_page)

# Now, going to the previous page, the score should be the same
# as the first request that showed the document.
traveler.shift(datetime.timedelta(days=1))
r = self.client.get(previous_page)
date_score_previous_2 = r.data["results"][0]["date_score"]
self.assertEqual(date_score_2, date_score_previous_2)

Expand Down

0 comments on commit 2cbe748

Please sign in to comment.