From 008ef90ee941193739511e7c3adba72dfc73ce7c Mon Sep 17 00:00:00 2001 From: Olga Bulat Date: Thu, 2 Nov 2023 08:12:17 +0300 Subject: [PATCH 1/2] Fix the "search query" in the time took logs Signed-off-by: Olga Bulat --- api/api/controllers/elasticsearch/helpers.py | 10 +++++----- api/api/controllers/search_controller.py | 6 +++--- api/api/utils/search_context.py | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/api/api/controllers/elasticsearch/helpers.py b/api/api/controllers/elasticsearch/helpers.py index 28a9c1cb23a..c89f7b07631 100644 --- a/api/api/controllers/elasticsearch/helpers.py +++ b/api/api/controllers/elasticsearch/helpers.py @@ -22,12 +22,12 @@ def wrapper(*args, **kwargs): 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": kwargs.get("es_query"), } ) @@ -37,7 +37,7 @@ def wrapper(*args, **kwargs): @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 a08e2da3f4c..8893eec9e7d 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 [] diff --git a/api/api/utils/search_context.py b/api/api/utils/search_context.py index 4c26811d087..0cddaaa81ff 100644 --- a/api/api/utils/search_context.py +++ b/api/api/utils/search_context.py @@ -44,7 +44,7 @@ def build( # 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 From 21d8a0591dd4e72943e0f182d56bbe117fffd97f Mon Sep 17 00:00:00 2001 From: Olga Bulat Date: Fri, 3 Nov 2023 18:24:53 +0300 Subject: [PATCH 2/2] Add a keyword-only arg with no default Signed-off-by: Olga Bulat --- api/api/controllers/elasticsearch/helpers.py | 4 ++-- api/api/controllers/search_controller.py | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/api/api/controllers/elasticsearch/helpers.py b/api/api/controllers/elasticsearch/helpers.py index c89f7b07631..c4bbdf9386e 100644 --- a/api/api/controllers/elasticsearch/helpers.py +++ b/api/api/controllers/elasticsearch/helpers.py @@ -12,7 +12,7 @@ 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 @@ -27,7 +27,7 @@ def wrapper(*args, **kwargs): { "response_time": response_time_in_ms, "es_time": es_time_in_ms, - "es_query": kwargs.get("es_query"), + "es_query": es_query, } ) diff --git a/api/api/controllers/search_controller.py b/api/api/controllers/search_controller.py index 8893eec9e7d..8ce00c3ab90 100644 --- a/api/api/controllers/search_controller.py +++ b/api/api/controllers/search_controller.py @@ -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: