Skip to content

Commit 4857a80

Browse files
committed
return empty dict when no highlight
1 parent 0f47f05 commit 4857a80

File tree

3 files changed

+40
-35
lines changed

3 files changed

+40
-35
lines changed

readthedocs/search/api.py

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

65
from rest_framework import generics, serializers
@@ -48,27 +47,13 @@ def get_inner_hits(self, obj):
4847
sections = inner_hits.sections or []
4948
domains = inner_hits.domains or []
5049
all_results = itertools.chain(sections, domains)
51-
52-
sorted_results = [
53-
{
54-
'type': hit._nested.field,
55-
'_source': hit._source.to_dict(),
56-
'highlight': self._get_inner_hits_highlights(hit),
57-
}
58-
for hit in sorted(all_results, key=attrgetter('_score'), reverse=True)
59-
]
60-
50+
sorted_results = list(utils._get_sorted_results(
51+
results=all_results,
52+
source_key='_source',
53+
logging=True,
54+
))
6155
return sorted_results
6256

63-
def _get_inner_hits_highlights(self, hit):
64-
"""Removes new lines from highlight and log it."""
65-
highlight_dict = utils._remove_newlines_from_dict(
66-
hit.highlight.to_dict()
67-
)
68-
69-
log.debug('API Search highlight: %s', pformat(highlight_dict))
70-
return highlight_dict
71-
7257

7358
class PageSearchAPIView(generics.ListAPIView):
7459

readthedocs/search/utils.py

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

33
import logging
4+
from pprint import pformat
5+
from operator import attrgetter
46

57
from django.shortcuts import get_object_or_404
68
from django_elasticsearch_dsl.apps import DEDConfig
@@ -174,3 +176,31 @@ def _remove_newlines_from_dict(highlight):
174176
highlight[k] = v_new_list
175177

176178
return highlight
179+
180+
181+
def _get_inner_hits_highlights(hit, logging=False):
182+
"""Removes new lines from highlight dict"""
183+
if hasattr(hit, 'highlight'):
184+
highlight_dict = _remove_newlines_from_dict(
185+
hit.highlight.to_dict()
186+
)
187+
188+
if logging:
189+
log.debug('API Search highlight: %s', pformat(highlight_dict))
190+
191+
return highlight_dict
192+
return {}
193+
194+
195+
def _get_sorted_results(results, source_key='_source', logging=False):
196+
"""Sort results according to their score and return a generator expression."""
197+
sorted_results = (
198+
{
199+
'type': hit._nested.field,
200+
source_key: hit._source.to_dict(),
201+
'highlight': _get_inner_hits_highlights(hit, logging)
202+
}
203+
for hit in sorted(results, key=attrgetter('_score'), reverse=True)
204+
)
205+
206+
return sorted_results

readthedocs/search/views.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,11 @@ def elastic_search(request, project_slug=None):
117117
domains = inner_hits.domains or []
118118
all_results = itertools.chain(sections, domains)
119119

120-
sorted_results = [
121-
{
122-
'type': hit._nested.field,
123-
124-
# here _source term is not used because
125-
# django gives error if the names of the
126-
# variables start with underscore
127-
'source': hit._source.to_dict(),
128-
129-
'highlight': utils._remove_newlines_from_dict(
130-
hit.highlight.to_dict()
131-
),
132-
}
133-
for hit in sorted(all_results, key=attrgetter('_score'), reverse=True)
134-
]
120+
sorted_results = list(utils._get_sorted_results(
121+
results=all_results,
122+
source_key='source',
123+
logging=False,
124+
))
135125

136126
result.meta.inner_hits = sorted_results
137127
except Exception:

0 commit comments

Comments
 (0)