Paginator custom facets code optimization

This commit is contained in:
Kuroshini 2019-11-29 13:46:20 +03:00
parent c769049160
commit 1a57d0edd8
2 changed files with 6 additions and 25 deletions

View File

@ -23,6 +23,7 @@ class CustomFacetedSearchFilterBackend(FacetedSearchFilterBackend):
return cur_facet['facet']._params['field'] != next(iter(x._params)) return cur_facet['facet']._params['field'] != next(iter(x._params))
return myfilter return myfilter
__facets = self.construct_facets(request, view) __facets = self.construct_facets(request, view)
setattr(view.paginator, 'facets_computed', {})
for __field, __facet in iteritems(__facets): for __field, __facet in iteritems(__facets):
agg = __facet['facet'].get_aggregation() agg = __facet['facet'].get_aggregation()
agg_filter = Q('match_all') agg_filter = Q('match_all')
@ -50,7 +51,7 @@ class CustomFacetedSearchFilterBackend(FacetedSearchFilterBackend):
'filter', 'filter',
filter=agg_filter filter=agg_filter
).bucket(__field, agg) ).bucket(__field, agg)
self.facets_computed.update({facet_name: qs.execute().aggregations[facet_name]}) view.paginator.facets_computed.update({facet_name: qs.execute().aggregations[facet_name]})
return queryset return queryset

View File

@ -4,6 +4,7 @@ from django_elasticsearch_dsl_drf import constants
from django_elasticsearch_dsl_drf.filter_backends import ( from django_elasticsearch_dsl_drf.filter_backends import (
FilteringFilterBackend, FilteringFilterBackend,
GeoSpatialFilteringFilterBackend, GeoSpatialFilteringFilterBackend,
GeoSpatialOrderingFilterBackend,
) )
from elasticsearch_dsl import TermsFacet from elasticsearch_dsl import TermsFacet
from django_elasticsearch_dsl_drf.viewsets import BaseDocumentViewSet from django_elasticsearch_dsl_drf.viewsets import BaseDocumentViewSet
@ -26,6 +27,7 @@ class NewsDocumentViewSet(BaseDocumentViewSet):
filters.CustomSearchFilterBackend, filters.CustomSearchFilterBackend,
FilteringFilterBackend, FilteringFilterBackend,
filters.CustomFacetedSearchFilterBackend, filters.CustomFacetedSearchFilterBackend,
GeoSpatialOrderingFilterBackend,
] ]
faceted_search_fields = { faceted_search_fields = {
@ -76,14 +78,6 @@ class NewsDocumentViewSet(BaseDocumentViewSet):
}, },
} }
def filter_queryset(self, queryset):
for backend in list(self.filter_backends):
bc = backend()
queryset = bc.filter_queryset(self.request, queryset, self)
if hasattr(bc, 'facets_computed'):
setattr(self.paginator, 'facets_computed', bc.facets_computed)
return queryset
class MobileNewsDocumentViewSet(NewsDocumentViewSet): class MobileNewsDocumentViewSet(NewsDocumentViewSet):
@ -111,6 +105,7 @@ class EstablishmentDocumentViewSet(BaseDocumentViewSet):
filters.CustomSearchFilterBackend, filters.CustomSearchFilterBackend,
GeoSpatialFilteringFilterBackend, GeoSpatialFilteringFilterBackend,
filters.CustomFacetedSearchFilterBackend, filters.CustomFacetedSearchFilterBackend,
GeoSpatialOrderingFilterBackend,
] ]
faceted_search_fields = { faceted_search_fields = {
@ -292,14 +287,6 @@ class EstablishmentDocumentViewSet(BaseDocumentViewSet):
} }
} }
def filter_queryset(self, queryset):
for backend in list(self.filter_backends):
bc = backend()
queryset = bc.filter_queryset(self.request, queryset, self)
if hasattr(bc, 'facets_computed'):
setattr(self.paginator, 'facets_computed', bc.facets_computed)
return queryset
class MobileEstablishmentDocumentViewSet(EstablishmentDocumentViewSet): class MobileEstablishmentDocumentViewSet(EstablishmentDocumentViewSet):
@ -322,6 +309,7 @@ class ProductDocumentViewSet(BaseDocumentViewSet):
FilteringFilterBackend, FilteringFilterBackend,
filters.CustomSearchFilterBackend, filters.CustomSearchFilterBackend,
filters.CustomFacetedSearchFilterBackend, filters.CustomFacetedSearchFilterBackend,
GeoSpatialOrderingFilterBackend,
] ]
search_fields = { search_fields = {
@ -414,14 +402,6 @@ class ProductDocumentViewSet(BaseDocumentViewSet):
}, },
} }
def filter_queryset(self, queryset):
for backend in list(self.filter_backends):
bc = backend()
queryset = bc.filter_queryset(self.request, queryset, self)
if hasattr(bc, 'facets_computed'):
setattr(self.paginator, 'facets_computed', bc.facets_computed)
return queryset
class MobileProductDocumentViewSet(ProductDocumentViewSet): class MobileProductDocumentViewSet(ProductDocumentViewSet):