Skip to content

Commit a1a6666

Browse files
committed
simplify logic
1 parent a9acda2 commit a1a6666

File tree

3 files changed

+15
-29
lines changed

3 files changed

+15
-29
lines changed

readthedocs/search/api.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import itertools
22
import logging
3-
from pprint import pformat
43

54
from rest_framework import generics, serializers
65
from rest_framework.exceptions import ValidationError
@@ -38,7 +37,7 @@ def get_highlight(self, obj):
3837
highlight = getattr(obj.meta, 'highlight', None)
3938
if highlight:
4039
ret = highlight.to_dict()
41-
log.debug('API Search highlight [Page title]: %s', pformat(ret))
40+
log.debug('API Search highlight [Page title]: %s', ret)
4241
return ret
4342

4443
def get_inner_hits(self, obj):
@@ -47,11 +46,13 @@ def get_inner_hits(self, obj):
4746
sections = inner_hits.sections or []
4847
domains = inner_hits.domains or []
4948
all_results = itertools.chain(sections, domains)
50-
sorted_results = list(utils._get_sorted_results(
49+
50+
sorted_results = utils._get_sorted_results(
5151
results=all_results,
5252
source_key='_source',
53-
logging=True,
54-
))
53+
)
54+
55+
log.debug('[API] Sorted Results: %s', sorted_results)
5556
return sorted_results
5657

5758

readthedocs/search/utils.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Utilities related to reading and generating indexable search content."""
22

33
import logging
4-
from pprint import pformat
54
from operator import attrgetter
65

76
from django.shortcuts import get_object_or_404
@@ -155,27 +154,15 @@ def _indexing_helper(html_objs_qs, wipe=False):
155154
delete_objects_in_es.delay(**kwargs)
156155

157156

158-
def _get_inner_hits_highlights(hit, logging=False):
159-
"""Returns highlight dict and does conditional logging of the same."""
160-
if hasattr(hit, 'highlight'):
161-
highlight_dict = hit.highlight.to_dict()
162-
163-
if logging:
164-
log.debug('API Search highlight: %s', pformat(highlight_dict))
165-
166-
return highlight_dict
167-
return {}
168-
169-
170-
def _get_sorted_results(results, source_key='_source', logging=False):
171-
"""Sort results according to their score and return a generator expression."""
172-
sorted_results = (
157+
def _get_sorted_results(results, source_key='_source'):
158+
"""Sort results according to their score and returns results as list."""
159+
sorted_results = [
173160
{
174161
'type': hit._nested.field,
175162
source_key: hit._source.to_dict(),
176-
'highlight': _get_inner_hits_highlights(hit, logging)
163+
'highlight': hit.highlight.to_dict() if hasattr(hit, 'highlight') else {}
177164
}
178165
for hit in sorted(results, key=attrgetter('_score'), reverse=True)
179-
)
166+
]
180167

181168
return sorted_results

readthedocs/search/views.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import itertools
44
import logging
55
from operator import attrgetter
6-
from pprint import pformat
76

87
from django.shortcuts import get_object_or_404, render
98

@@ -117,18 +116,17 @@ def elastic_search(request, project_slug=None):
117116
domains = inner_hits.domains or []
118117
all_results = itertools.chain(sections, domains)
119118

120-
sorted_results = list(utils._get_sorted_results(
119+
sorted_results = utils._get_sorted_results(
121120
results=all_results,
122121
source_key='source',
123-
logging=False,
124-
))
122+
)
125123

126124
result.meta.inner_hits = sorted_results
127125
except Exception:
128126
log.exception('Error while sorting the results (inner_hits).')
129127

130-
log.debug('Search results: %s', pformat(results.to_dict()))
131-
log.debug('Search facets: %s', pformat(results.facets.to_dict()))
128+
log.debug('Search results: %s', results.to_dict())
129+
log.debug('Search facets: %s', results.facets.to_dict())
132130

133131
template_vars = user_input._asdict()
134132
template_vars.update({

0 commit comments

Comments
 (0)