Skip to content

Commit

Permalink
OPEN-3260: Refinements to the data captured in search term logging
Browse files Browse the repository at this point in the history
  • Loading branch information
thriuin committed May 3, 2024
1 parent 24ad0a0 commit 1b50690
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
2 changes: 1 addition & 1 deletion oc_search/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ def __call__(self, request):
# Code to be executed for each request/response after
# the view is called.

return response
return response
9 changes: 4 additions & 5 deletions search/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ def iter_namespace(ns_pkg):
return pkgutil.iter_modules(ns_pkg.__path__, ns_pkg.__name__ + ".")


def log_search_results(request: HttpRequest, search_logger: logging.Logger, doc_count: int = 0):
def log_search_results(request: HttpRequest, search_logger: logging.Logger, search_type: str = "", format: str = "html", page_type: str = "search", search_text: str = '', doc_count: int = 0):
qurl = parse.urlparse(request.get_full_path())

query_values = parse.parse_qs(request.META["QUERY_STRING"])
page = '1'
sort = 'default'
search_text = ""
facets = ''
search_format = 'default'
if 'page' in query_values:
Expand All @@ -54,8 +53,7 @@ def log_search_results(request: HttpRequest, search_logger: logging.Logger, doc_
for q in query_values:
if q not in ['page', 'sort', 'search_text', 'search_format']:
facets += f"{q}:{parse.quote_plus(query_values[q][0])},"
search_type = qurl.path.rstrip('/').split('/')[-1]
log_message = f'{qurl.hostname},{search_type},{request.session.session_key},{page},{sort},"{search_text}","{facets}",{search_format},{doc_count}'
log_message = f'{qurl.hostname},{search_type},{page_type},{format},{request.session.session_key},{page},{sort},"{search_text}","{facets}",{doc_count}'
search_logger.info(log_message)


Expand Down Expand Up @@ -541,7 +539,7 @@ def get(self, request: HttpRequest, lang='en', search_type=''):
else:
json_link = json_link + "&search_format=json"
context["json_format_url"] = json_link
log_search_results(request, self.search_logger, doc_count=solr_response.num_found)
log_search_results(request, self.search_logger, search_type=search_type, format=search_format, page_type='search', doc_count=solr_response.num_found)
return render(request, self.searches[search_type].page_template, context)
except (ConnectionError, SolrError) as ce:
return render(request, 'error.html', get_error_context(search_type, lang, ce.args[0]))
Expand Down Expand Up @@ -664,6 +662,7 @@ def get(self, request: HttpRequest, lang='en', search_type='', record_id=''):
self.searches[search_type],
self.fields[search_type],
self.codes_fr[search_type] if lang == 'fr' else self.codes_en[search_type])
log_search_results(request, self.search_logger, search_type=search_type, format='html', page_type='record', search_text=record_id, doc_count=solr_response.num_found)
return render(request, self.searches[search_type].record_template, context)

else:
Expand Down

0 comments on commit 1b50690

Please sign in to comment.