-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathfrontend.py
379 lines (361 loc) · 10.9 KB
/
frontend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
from django.utils.decorators import classonlymethod
from django_elasticsearch_dsl_drf.constants import (
LOOKUP_FILTER_PREFIX,
LOOKUP_FILTER_RANGE,
LOOKUP_FILTER_TERMS,
LOOKUP_FILTER_WILDCARD,
LOOKUP_QUERY_EXCLUDE,
LOOKUP_QUERY_GT,
LOOKUP_QUERY_GTE,
LOOKUP_QUERY_IN,
LOOKUP_QUERY_ISNULL,
LOOKUP_QUERY_LT,
LOOKUP_QUERY_LTE,
SUGGESTER_COMPLETION,
SUGGESTER_PHRASE,
SUGGESTER_TERM,
)
from django_elasticsearch_dsl_drf.filter_backends import (
DefaultOrderingFilterBackend,
FacetedSearchFilterBackend,
FilteringFilterBackend,
HighlightBackend,
IdsFilterBackend,
OrderingFilterBackend,
PostFilterFilteringFilterBackend,
SearchFilterBackend,
SuggesterFilterBackend,
FunctionalSuggesterFilterBackend,
)
from django_elasticsearch_dsl_drf.viewsets import (
DocumentViewSet,
)
from django_elasticsearch_dsl_drf.pagination import PageNumberPagination
from elasticsearch_dsl import DateHistogramFacet, RangeFacet
from rest_framework.decorators import action
from ...documents import BookDocument
from ...serializers import BookDocumentSimpleSerializer
__all__ = (
'BookFrontendDocumentViewSet',
'BookCustomDocumentViewSet',
)
class CustomPageNumberPagination(PageNumberPagination):
"""This is needed in order to make page size customisation possible."""
page_size_query_param = 'page_size'
class BookFrontendDocumentViewSet(DocumentViewSet):
"""Frontend BookDocument ViewSet.
From the name you can guess that it's all about React frontend demo.
"""
document = BookDocument
serializer_class = BookDocumentSimpleSerializer
pagination_class = CustomPageNumberPagination
lookup_field = 'id'
filter_backends = [
FilteringFilterBackend,
IdsFilterBackend,
OrderingFilterBackend,
DefaultOrderingFilterBackend,
SearchFilterBackend,
FacetedSearchFilterBackend,
PostFilterFilteringFilterBackend,
HighlightBackend,
SuggesterFilterBackend,
FunctionalSuggesterFilterBackend,
]
# Define search fields
search_fields = (
'title',
'description',
'summary',
)
# Define highlight fields
highlight_fields = {
'title': {
'enabled': True,
'options': {
'pre_tags': ["<b>"],
'post_tags': ["</b>"],
}
},
'summary': {
'options': {
'fragment_size': 50,
'number_of_fragments': 3
}
},
'description': {},
}
# Define filter fields
filter_fields = {
'id': {
'field': 'id',
'lookups': [
LOOKUP_FILTER_RANGE,
LOOKUP_QUERY_IN,
LOOKUP_QUERY_GT,
LOOKUP_QUERY_GTE,
LOOKUP_QUERY_LT,
LOOKUP_QUERY_LTE,
LOOKUP_FILTER_TERMS,
],
},
'title': 'title.raw',
'summary': 'summary',
'publisher': 'publisher.raw',
'publication_date_f': 'publication_date',
'status': 'state.raw',
'isbn': 'isbn.raw',
'price_f': {
'field': 'price.raw',
'lookups': [
LOOKUP_FILTER_RANGE,
],
},
'pages': {
'field': 'pages',
'lookups': [
LOOKUP_FILTER_RANGE,
LOOKUP_QUERY_GT,
LOOKUP_QUERY_GTE,
LOOKUP_QUERY_LT,
LOOKUP_QUERY_LTE,
],
},
'stock_count': {
# 'field': 'stock_count',
'lookups': [
LOOKUP_FILTER_RANGE,
LOOKUP_QUERY_GT,
LOOKUP_QUERY_GTE,
LOOKUP_QUERY_LT,
LOOKUP_QUERY_LTE,
],
},
'tags': {
'field': 'tags',
'lookups': [
LOOKUP_FILTER_TERMS,
LOOKUP_FILTER_PREFIX,
LOOKUP_FILTER_WILDCARD,
LOOKUP_QUERY_IN,
LOOKUP_QUERY_EXCLUDE,
LOOKUP_QUERY_ISNULL,
],
},
'tags.raw': {
'field': 'tags.raw',
'lookups': [
LOOKUP_FILTER_TERMS,
LOOKUP_FILTER_PREFIX,
LOOKUP_FILTER_WILDCARD,
LOOKUP_QUERY_IN,
LOOKUP_QUERY_EXCLUDE,
],
},
}
# Post filter fields, copy filters as they are valid
post_filter_fields = {
'publisher_pf': 'publisher.raw',
'status_pf': 'state.raw',
'price': 'price',
'publication_date': {
'field': 'publication_date',
},
'tags_pf': {
'field': 'tags',
'lookups': [
LOOKUP_FILTER_TERMS,
LOOKUP_FILTER_PREFIX,
LOOKUP_FILTER_WILDCARD,
LOOKUP_QUERY_IN,
LOOKUP_QUERY_EXCLUDE,
LOOKUP_QUERY_ISNULL,
],
},
'tags.raw_pf': {
'field': 'tags.raw',
'lookups': [
LOOKUP_FILTER_TERMS,
LOOKUP_FILTER_PREFIX,
LOOKUP_FILTER_WILDCARD,
LOOKUP_QUERY_IN,
LOOKUP_QUERY_EXCLUDE,
],
},
}
# Define ordering fields
ordering_fields = {
'id': 'id',
'title': 'title.raw',
'price': 'price.raw',
'state': 'state.raw',
'publication_date': 'publication_date',
}
# Specify default ordering
ordering = ('_score', 'id', 'title', 'price',)
faceted_search_fields = {
'status': {
'field': 'state.raw',
'enabled': True,
# 'global': True,
'options': {
"min_doc_count": 0,
"size": 20000,
"order": {
"_term": "asc"
},
},
},
'publisher': {
'field': 'publisher.raw',
'enabled': True,
# 'global': True,
'options': {
"min_doc_count": 0,
"size": 20000,
"order": {
"_term": "asc"
},
},
},
'publication_date': {
'field': 'publication_date',
'facet': DateHistogramFacet,
# 'global': True,
'enabled': True,
'options': {
'interval': 'year',
},
},
'pages_count': {
'field': 'pages',
'facet': RangeFacet,
'options': {
'ranges': [
("0__10", (0, 10)),
("11__20", (11, 20)),
("20__50", (20, 50)),
("50__999999", (50, 999999)),
],
}
},
'price': {
# 'field': 'price',
'facet': RangeFacet,
'enabled': True,
'options': {
'ranges': [
("0__9.99", (0, 9.99)),
("10__19.99", (10, 19.99)),
("20__49.99", (20, 49.99)),
("50__999999", (50, 999999)),
],
},
},
}
# Suggester fields
suggester_fields = {
'title_suggest': {
'field': 'title.suggest',
'default_suggester': SUGGESTER_COMPLETION,
'options': {
'size': 20,
# 'd': True,
},
},
'title_suggest_context': {
'field': 'title.suggest_context',
'suggesters': [
SUGGESTER_COMPLETION,
],
'default_suggester': SUGGESTER_COMPLETION,
# We want to be able to filter the completion filter
# results on the following params: tag, state and publisher.
# We also want to provide the size value.
# See the "https://www.elastic.co/guide/en/elasticsearch/
# reference/6.1/suggester-context.html" for the reference.
'completion_options': {
'category_filters': {
'title_suggest_tag': 'tag',
'title_suggest_state': 'state',
'title_suggest_publisher': 'publisher',
},
},
'options': {
'size': 20,
},
},
'title_suggest_edge_ngram': {
'field': 'title.edge_ngram_completion',
'default_suggester': SUGGESTER_TERM,
'suggesters': [
SUGGESTER_PHRASE,
SUGGESTER_TERM,
],
},
'title_suggest_mlt': {
'field': 'title.mlt',
'default_suggester': SUGGESTER_TERM,
'suggesters': [
SUGGESTER_PHRASE,
SUGGESTER_TERM,
],
},
'publisher_suggest': 'publisher.suggest',
'tag_suggest': 'tags.suggest',
'summary_suggest': 'summary',
}
class BookCustomDocumentViewSet(BookFrontendDocumentViewSet):
@classonlymethod
def as_view(cls, actions=None, **initkwargs):
# No request object is available here
print('as_view')
print('actions: ', actions)
print('initkwargs: ', initkwargs)
return super(BookCustomDocumentViewSet, cls).as_view(
actions,
**initkwargs
)
def retrieve(self, request, *args, **kwargs):
# Used for detail routes, like
# http://localhost:8000/search/books-custom/999999/
print('retrieve')
print('request: ', request)
print('args: ', args)
print('kwargs: ', kwargs)
return super(BookCustomDocumentViewSet, self).retrieve(
request,
*args,
**kwargs
)
def list(self, request, *args, **kwargs):
# Used for list routes, like
# http://localhost:8000/search/books-custom/
print('list')
print('request: ', request)
print('args: ', args)
print('kwargs: ', kwargs)
return super(BookCustomDocumentViewSet, self).list(
request,
*args,
**kwargs
)
@action(detail=False)
def suggest(self, request):
# Used for suggest routes, like
# http://localhost:8000/search/books-custom/suggest/?title_suggest=A
print('suggest')
print('request: ', request)
return super(BookCustomDocumentViewSet, self).suggest(
request
)
@action(detail=False)
def functional_suggest(self, request):
# Used for functional suggest routes, like
# http://localhost:8000/search/books-custom/functional_suggest/
# ?title_suggest=A
print('functional_suggest')
print('request: ', request)
return super(BookCustomDocumentViewSet, self).suggest(
request
)