File tree Expand file tree Collapse file tree 3 files changed +40
-35
lines changed Expand file tree Collapse file tree 3 files changed +40
-35
lines changed Original file line number Diff line number Diff line change 1
1
import itertools
2
2
import logging
3
- from operator import attrgetter
4
3
from pprint import pformat
5
4
6
5
from rest_framework import generics , serializers
@@ -48,27 +47,13 @@ def get_inner_hits(self, obj):
48
47
sections = inner_hits .sections or []
49
48
domains = inner_hits .domains or []
50
49
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
+ ))
61
55
return sorted_results
62
56
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
-
72
57
73
58
class PageSearchAPIView (generics .ListAPIView ):
74
59
Original file line number Diff line number Diff line change 1
1
"""Utilities related to reading and generating indexable search content."""
2
2
3
3
import logging
4
+ from pprint import pformat
5
+ from operator import attrgetter
4
6
5
7
from django .shortcuts import get_object_or_404
6
8
from django_elasticsearch_dsl .apps import DEDConfig
@@ -174,3 +176,31 @@ def _remove_newlines_from_dict(highlight):
174
176
highlight [k ] = v_new_list
175
177
176
178
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
Original file line number Diff line number Diff line change @@ -117,21 +117,11 @@ def elastic_search(request, project_slug=None):
117
117
domains = inner_hits .domains or []
118
118
all_results = itertools .chain (sections , domains )
119
119
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
+ ))
135
125
136
126
result .meta .inner_hits = sorted_results
137
127
except Exception :
You can’t perform that action at this time.
0 commit comments