Skip to content

Commit

Permalink
Fix the "search query" in the time took logs (#3313)
Browse files Browse the repository at this point in the history
* Fix the "search query" in the time took logs

Signed-off-by: Olga Bulat <[email protected]>

* Add a keyword-only arg with no default

Signed-off-by: Olga Bulat <[email protected]>

---------

Signed-off-by: Olga Bulat <[email protected]>
obulat authored Nov 7, 2023
1 parent 3d6e8b5 commit 3e4a871
Showing 3 changed files with 14 additions and 11 deletions.
12 changes: 6 additions & 6 deletions api/api/controllers/elasticsearch/helpers.py
Original file line number Diff line number Diff line change
@@ -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
@@ -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": 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)
11 changes: 7 additions & 4 deletions api/api/controllers/search_controller.py
Original file line number Diff line number Diff line change
@@ -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:
2 changes: 1 addition & 1 deletion api/api/utils/search_context.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3e4a871

Please sign in to comment.