|
2 | 2 | Faceted search backend. |
3 | 3 | """ |
4 | 4 | import copy |
| 5 | +from collections import defaultdict |
| 6 | + |
5 | 7 | from elasticsearch_dsl import TermsFacet |
6 | 8 | from elasticsearch_dsl.query import Q |
7 | 9 |
|
|
13 | 15 | __author__ = 'Artur Barseghyan <[email protected]>' |
14 | 16 | __copyright__ = '2017-2020 Artur Barseghyan' |
15 | 17 | __license__ = 'GPL 2.0/LGPL 2.1' |
16 | | -__all__ = ('FacetedSearchFilterBackend',) |
| 18 | +__all__ = ('FacetedSearchFilterBackend', 'FacetedFilterSearchFilterBackend') |
| 19 | + |
| 20 | +from django_elasticsearch_dsl_drf.filter_backends.filtering import FilteringFilterBackend |
17 | 21 |
|
18 | 22 |
|
19 | 23 | class FacetedSearchFilterBackend(BaseFilterBackend): |
@@ -229,3 +233,91 @@ def filter_queryset(self, request, queryset, view): |
229 | 233 | :rtype: elasticsearch_dsl.search.Search |
230 | 234 | """ |
231 | 235 | return self.aggregate(request, queryset, view) |
| 236 | + |
| 237 | + |
| 238 | +class FacetedFilterSearchFilterBackend(FilteringFilterBackend, FacetedSearchFilterBackend): |
| 239 | + """ Combined faceting and filtering backend similar to elasticsearch-dsl's FacetedSearch class. |
| 240 | + It combines the functionality of FilteringFilterBackend and FacetedSearchFilterBackend to take filters into |
| 241 | + account when creating facets. |
| 242 | +
|
| 243 | + This backend uses the same configuration fields as FilteringFilterBackend and FacetedSearchFilterBackend. |
| 244 | + This backend replaces their functionality and should not be used together with either of those backends. |
| 245 | +
|
| 246 | + Note that to work correctly, the actual elasticsearch field must be the same for a facet and its matching filter. |
| 247 | + For example, if a facet will aggregate on field `state.raw`, then the filter must also filter on `state.raw`, |
| 248 | + and not just `state`. |
| 249 | +
|
| 250 | + When creating a facet, filters for faceted fields other than for the current facet are applied. Filters |
| 251 | + for faceted fields are then applied as post_filters. Filters on non-faceted fields are applied as normal filters. |
| 252 | + """ |
| 253 | + def filter_queryset(self, request, queryset, view): |
| 254 | + # the fact that apply_filter is a classmethod means we can't store state on self, |
| 255 | + # so we hitch it onto queryset |
| 256 | + queryset._facets = self.construct_facets(request, view) |
| 257 | + queryset._faceted_fields = set(f['facet']._params['field'] for f in queryset._facets.values()) |
| 258 | + queryset._filters = defaultdict(list) |
| 259 | + |
| 260 | + # apply filters |
| 261 | + queryset = FilteringFilterBackend.filter_queryset(self, request, queryset, view) |
| 262 | + |
| 263 | + # apply aggregations |
| 264 | + return self.aggregate(request, queryset, view) |
| 265 | + |
| 266 | + @classmethod |
| 267 | + def apply_filter(cls, queryset, options=None, args=None, kwargs=None): |
| 268 | + if args is None: |
| 269 | + args = [] |
| 270 | + if kwargs is None: |
| 271 | + kwargs = {} |
| 272 | + |
| 273 | + facets = queryset._facets |
| 274 | + faceted_fields = queryset._faceted_fields |
| 275 | + filters = queryset._filters |
| 276 | + |
| 277 | + # if this field is faceted, then apply it as a post-filter |
| 278 | + if options['field'] in faceted_fields: |
| 279 | + queryset = queryset.post_filter(*args, **kwargs) |
| 280 | + else: |
| 281 | + queryset = queryset.filter(*args, **kwargs) |
| 282 | + |
| 283 | + filters[options['field']].append(Q(*args, **kwargs)) |
| 284 | + |
| 285 | + # ensure the new queryset object retains the helper variables |
| 286 | + queryset._facets = facets |
| 287 | + queryset._faceted_fields = faceted_fields |
| 288 | + queryset._filters = filters |
| 289 | + return queryset |
| 290 | + |
| 291 | + def aggregate(self, request, queryset, view): |
| 292 | + facets = queryset._facets |
| 293 | + faceted_fields = queryset._faceted_fields |
| 294 | + filters = queryset._filters |
| 295 | + |
| 296 | + for field, facet in facets.items(): |
| 297 | + agg = facet['facet'].get_aggregation() |
| 298 | + |
| 299 | + if facet['global']: |
| 300 | + queryset.aggs.bucket( |
| 301 | + '_filter_' + field, |
| 302 | + 'global' |
| 303 | + ).bucket(field, agg) |
| 304 | + continue |
| 305 | + |
| 306 | + agg_filter = Q('match_all') |
| 307 | + for f, _filter in filters.items(): |
| 308 | + # apply filters for that are applicable for facets other than this one |
| 309 | + if agg.field == f or f not in faceted_fields: |
| 310 | + continue |
| 311 | + # combine with or |
| 312 | + q = _filter[0] |
| 313 | + for x in _filter[1:]: |
| 314 | + q = q | x |
| 315 | + agg_filter &= q |
| 316 | + |
| 317 | + queryset.aggs.bucket( |
| 318 | + '_filter_' + field, |
| 319 | + 'filter', |
| 320 | + filter=agg_filter |
| 321 | + ).bucket(field, agg) |
| 322 | + |
| 323 | + return queryset |
0 commit comments