Skip to content

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

Fix the "search query" in the time took logs (#3313)

Olga Bulat3e4a8710663f
diff --git a/api/api/controllers/elasticsearch/helpers.py b/api/api/controllers/elasticsearch/helpers.py
index 28a9c1cb2..c4bbdf938 100644
--- a/api/api/controllers/elasticsearch/helpers.py
+++ b/api/api/controllers/elasticsearch/helpers.py
@@ -12,7 +12,7 @@ from elasticsearch import BadRequestError, NotFoundError
def log_timing_info(func):
@functools.wraps(func)
- def wrapper(*args, **kwargs):
+ def wrapper(*args, es_query, **kwargs):
start_time = time.time()
# Call the original function
@@ -22,12 +22,12 @@ def log_timing_info(func):
if hasattr(result, "took"):
es_time_in_ms = result.took
else:
- es_time_in_ms = result["took"]
+ es_time_in_ms = result.get("took")
log.info(
{
"response_time": response_time_in_ms,
"es_time": es_time_in_ms,
- "search_query": kwargs.get("search_query"),
+ "es_query": es_query,
}
)
@@ -37,7 +37,7 @@ def log_timing_info(func):
@log_timing_info
-def get_es_response(s, search_query=None):
+def get_es_response(s, *args, **kwargs):
if settings.VERBOSE_ES_RESPONSE:
log.info(pprint.pprint(s.to_dict()))
@@ -53,5 +53,5 @@ def get_es_response(s, search_query=None):
@log_timing_info
-def get_raw_es_response(index, body, search_query=None, **kwargs):
- return settings.ES.search(index=index, body=body, **kwargs)
+def get_raw_es_response(index, body, *args, **kwargs):
+ return settings.ES.search(index=index, body=body, *args, **kwargs)
diff --git a/api/api/controllers/search_controller.py b/api/api/controllers/search_controller.py
index a08e2da3f..8ce00c3ab 100644
--- a/api/api/controllers/search_controller.py
+++ b/api/api/controllers/search_controller.py
@@ -223,7 +223,7 @@ def _post_process_results(
end = search_results.hits.total.value
s = s[start:end]
- search_response = get_es_response(s, "postprocess_search")
+ search_response = get_es_response(s, es_query="postprocess_search")
return _post_process_results(
s, start, end, page_size, search_response, filter_dead
@@ -448,7 +448,7 @@ def search(
# Paginate
start, end = _get_query_slice(s, page_size, page, filter_dead)
s = s[start:end]
- search_response = get_es_response(s, "search")
+ search_response = get_es_response(s, es_query="search")
results = _post_process_results(
s, start, end, page_size, search_response, filter_dead
@@ -545,7 +545,7 @@ def related_media(uuid: str, index: str, filter_dead: bool) -> list[Hit]:
start, end = _get_query_slice(s, page_size, page, filter_dead)
s = s[start:end]
- response = get_es_response(s, "related_media")
+ response = get_es_response(s, es_query="related_media")
results = _post_process_results(s, start, end, page_size, response, filter_dead)
return results or []
@@ -586,7 +586,10 @@ def get_sources(index):
}
try:
results = get_raw_es_response(
- index=index, body=body, search_query="sources", request_cache=True
+ index=index,
+ body=body,
+ request_cache=True,
+ es_query="sources",
)
buckets = results["aggregations"]["unique_sources"]["buckets"]
except NotFoundError:
diff --git a/api/api/utils/search_context.py b/api/api/utils/search_context.py
index 4c26811d0..0cddaaa81 100644
--- a/api/api/utils/search_context.py
+++ b/api/api/utils/search_context.py
@@ -44,7 +44,7 @@ class SearchContext:
# results.
filtered_index_slice = filtered_index_search[: len(all_result_identifiers)]
results_in_filtered_index = get_es_response(
- filtered_index_slice, "filtered_index_context"
+ filtered_index_slice, es_query="filtered_index_context"
)
filtered_index_identifiers = {
result.identifier for result in results_in_filtered_index