Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Tally top 80 results exactly and fix "monday" timestamp calculation (#1113)

sarayourfriend4dcb80e468a5
diff --git a/api/catalog/api/controllers/search_controller.py b/api/catalog/api/controllers/search_controller.py
index ee1291702..20920b0d5 100644
--- a/api/catalog/api/controllers/search_controller.py
+++ b/api/catalog/api/controllers/search_controller.py
@@ -413,10 +413,33 @@ def search(
search_response, results, page_size, page
)
- if results and page <= 4:
- # We ignore tallies for deep pages because they're not likely to
+ max_result_depth = page * page_size
+ if max_result_depth <= 80:
+ # Applies when `page_size * page` could land "evenly" on 80
+ should_tally = True
+ results_to_tally = results
+ elif max_result_depth - page_size < 80:
+ # Applies when `page_size * page` could land beyond 80, but still
+ # encompass some results on _this page_ that are at or below the 80th
+ # position. For example: page=7 page_size=12 result depth=84.
+ # While max_result_depth exceeds 80, we still want to count
+ # the first eight results in `results` that are below or at the 80th
+ # position for the query.
+ should_tally = True
+ results_to_tally = results[: 80 - (max_result_depth - page_size)]
+ else:
+ should_tally = False
+
+ if results and should_tally:
+ # We ignore tallies for deep results because they're not likely to
# be as important for search relevancy for most users at this point
- tallies.count_provider_occurrences(results, index)
+ # 80 is chosen because it represents the first four pages of the
+ # default page count of 20 (20 * 4) which is how our own frontend
+ # makes requests and displays results. Because that is the only
+ # place we can actually conceivably measure relevancy down the
+ # line, it is the only sensible, controlled space we can use to
+ # check things like provider density for a set of queries.
+ tallies.count_provider_occurrences(results_to_tally, index)
return results or [], page_count, result_count
diff --git a/api/catalog/api/utils/tallies.py b/api/catalog/api/utils/tallies.py
index c792272f5..b683d473e 100644
--- a/api/catalog/api/utils/tallies.py
+++ b/api/catalog/api/utils/tallies.py
@@ -1,5 +1,5 @@
from collections import defaultdict
-from datetime import datetime
+from datetime import datetime, timedelta
import django_redis
from django_redis.client.default import Redis
@@ -8,12 +8,8 @@ from django_redis.client.default import Redis
def _get_weekly_timestamp() -> str:
"""Get a timestamp for the Monday of any given week."""
now = datetime.now()
- return datetime(
- now.year,
- now.month,
- # Set the day to Monday of the current week
- now.day - now.weekday(),
- ).strftime("%Y-%m-%d")
+ monday = now - timedelta(days=now.weekday())
+ return monday.strftime("%Y-%m-%d")
def count_provider_occurrences(results: list[dict], index: str) -> None:
diff --git a/api/test/unit/controllers/test_search_controller.py b/api/test/unit/controllers/test_search_controller.py
index e346f5688..bc81da56d 100644
--- a/api/test/unit/controllers/test_search_controller.py
+++ b/api/test/unit/controllers/test_search_controller.py
@@ -381,9 +381,6 @@ def test_paginate_with_dead_link_mask_query_mask_overlaps_query_window(
), f"expected {expected_range} but got {actual_range}"
-MOCK_RESULTS = [{"provider": "a provider", "identifier": i} for i in range(20)]
-
-
@pytest.mark.parametrize(
"index",
(
@@ -392,13 +389,35 @@ MOCK_RESULTS = [{"provider": "a provider", "identifier": i} for i in range(20)]
),
)
@pytest.mark.parametrize(
- ("page", "does_tally"),
+ ("page", "page_size", "does_tally", "number_of_results_passed"),
(
- (1, True),
- (2, True),
- (3, True),
- (4, True),
- (5, False),
+ (1, 20, True, 20),
+ (2, 20, True, 20),
+ (3, 20, True, 20),
+ (4, 20, True, 20),
+ (5, 20, False, 0),
+ (1, 40, True, 40),
+ (2, 40, True, 40),
+ (3, 40, False, 0),
+ (4, 40, False, 0),
+ (5, 40, False, 0),
+ (1, 10, True, 10),
+ (2, 10, True, 10),
+ (3, 10, True, 10),
+ (4, 10, True, 10),
+ (5, 10, True, 10),
+ (6, 10, True, 10),
+ (7, 10, True, 10),
+ (8, 10, True, 10),
+ (9, 10, False, 0),
+ (1, 12, True, 12),
+ (2, 12, True, 12),
+ (3, 12, True, 12),
+ (4, 12, True, 12),
+ (5, 12, True, 12),
+ (6, 12, True, 12),
+ (7, 12, True, 8),
+ (8, 12, False, 0),
),
)
@mock.patch.object(
@@ -406,17 +425,22 @@ MOCK_RESULTS = [{"provider": "a provider", "identifier": i} for i in range(20)]
)
@mock.patch(
"catalog.api.controllers.search_controller._post_process_results",
- return_value=MOCK_RESULTS,
)
@pytest.mark.django_db
def test_search_tallies_pages_less_than_5(
- _,
+ mock_post_process_results,
count_provider_occurrences_mock: mock.MagicMock,
page,
+ page_size,
does_tally,
+ number_of_results_passed,
index,
request_factory,
):
+ mock_post_process_results.return_value = [
+ {"provider": "a provider", "identifier": i} for i in range(page_size)
+ ]
+
serializer = MediaSearchRequestSerializer(data={"q": "dogs"})
serializer.is_valid()
@@ -425,7 +449,7 @@ def test_search_tallies_pages_less_than_5(
ip=0,
index=index,
page=page,
- page_size=20,
+ page_size=page_size,
request=request_factory.get("/"),
filter_dead=False,
)
@@ -435,5 +459,7 @@ def test_search_tallies_pages_less_than_5(
mock.ANY,
index,
)
+ passed_results = count_provider_occurrences_mock.call_args_list[0][0][0]
+ assert len(passed_results) == number_of_results_passed
else:
count_provider_occurrences_mock.assert_not_called()