-
Notifications
You must be signed in to change notification settings - Fork 112
/
nested_continents.py
63 lines (54 loc) · 2.2 KB
/
nested_continents.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
from django_elasticsearch_dsl_drf.filter_backends.mixins import (
FilterBackendMixin,
)
from rest_framework.filters import BaseFilterBackend
__all__ = ('NestedContinentsBackend',)
class NestedContinentsBackend(BaseFilterBackend, FilterBackendMixin):
"""Adds nesting to continents."""
faceted_search_param = 'nested_facet'
def get_faceted_search_query_params(self, request):
"""Get faceted search query params.
:param request: Django REST framework request.
:type request: rest_framework.request.Request
:return: List of search query params.
:rtype: list
"""
query_params = request.query_params.copy()
return query_params.getlist(self.faceted_search_param, [])
def filter_queryset(self, request, queryset, view):
"""Filter the queryset.
:param request: Django REST framework request.
:param queryset: Base queryset.
:param view: View.
:type request: rest_framework.request.Request
:type queryset: elasticsearch_dsl.search.Search
:type view: rest_framework.viewsets.ReadOnlyModelViewSet
:return: Updated queryset.
:rtype: elasticsearch_dsl.search.Search
"""
facets = self.get_faceted_search_query_params(request)
if 'continent' in facets:
queryset \
.aggs\
.bucket('continents',
'nested',
path='continent') \
.bucket('continent_name',
'terms',
field='continent.name.raw',
size=10) \
.bucket('counties',
'nested',
path='continent.country') \
.bucket('country_name',
'terms',
field='continent.country.name.raw',
size=10) \
.bucket('city',
'nested',
path='continent.country.city') \
.bucket('city_name',
'terms',
field='continent.country.city.name.raw',
size=10)
return queryset