Skip to content

Commit 351f5bd

Browse files
more on global aggregations
1 parent 7612461 commit 351f5bd

File tree

4 files changed

+63
-7
lines changed

4 files changed

+63
-7
lines changed

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Main features and highlights
7272
- :doc:`Multi match search filter backend <search_backends>`.
7373
- :doc:`Simple search query search filter backend <search_backends>`.
7474
- :doc:`More-like-this support (detail action) <more_like_this>`.
75+
- :doc:`Global aggregations support <global_aggregations>`.
7576

7677
Installation
7778
============

examples/simple/search_indexes/viewsets/book/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ class BaseBookDocumentViewSet(BaseDocumentViewSet):
190190
'publisher': {
191191
'field': 'publisher.raw',
192192
'enabled': True,
193-
'type': 'global',
193+
'global': True,
194194
},
195195
'publication_date': {
196196
'field': 'publication_date',

global_aggregations.rst

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
Global aggregations
2+
===================
3+
Global aggregations.
4+
5+
Sample view
6+
-----------
7+
8+
.. code-block:: python
9+
10+
from django_elasticsearch_dsl_drf.filter_backends import (
11+
CompoundSearchFilterBackend,
12+
DefaultOrderingFilterBackend,
13+
FacetedSearchFilterBackend,
14+
OrderingFilterBackend,
15+
)
16+
from django_elasticsearch_dsl_drf.viewsets import DocumentViewSet
17+
18+
from .documents import BookDocument
19+
from .serializers import BookDocumentSerializer
20+
21+
class BookCompoundSearchBackendDocumentViewSet(DocumentViewSet):
22+
23+
document = BookDocument
24+
serializer_class = BookDocumentSerializer
25+
lookup_field = 'id'
26+
27+
filter_backends = [
28+
# ...
29+
OrderingFilterBackend,
30+
DefaultOrderingFilterBackend,
31+
CompoundSearchFilterBackend,
32+
FacetedSearchFilterBackend,
33+
# ...
34+
]
35+
36+
faceted_search_fields = {
37+
'publisher': {
38+
'field': 'publisher.raw',
39+
'enabled': True,
40+
'global': True, # This makes the aggregation global
41+
},
42+
}
43+
44+
Sample request
45+
--------------
46+
47+
.. code-block:: text
48+
49+
http://localhost:8000/search/books/?publisher=Egmont
50+
51+
Sample response
52+
---------------
53+
54+
.. code-block:: javascript
55+

src/django_elasticsearch_dsl_drf/filter_backends/faceted_search.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ def prepare_faceted_search_fields(cls, view):
114114
if 'options' not in faceted_search_fields[field]:
115115
faceted_search_fields[field]['options'] = {}
116116

117-
if 'type' not in faceted_search_fields[field]:
118-
faceted_search_fields[field]['type'] = 'filter'
117+
faceted_search_fields[field]['global'] = \
118+
faceted_search_fields[field].get('global', False)
119119

120120
return faceted_search_fields
121121

@@ -175,7 +175,7 @@ def construct_facets(self, request, view):
175175
field=faceted_search_fields[__field]['field'],
176176
**faceted_search_fields[__field]['options']
177177
),
178-
'type': faceted_search_fields[__field]['type'],
178+
'global': faceted_search_fields[__field]['global'],
179179
}
180180
}
181181
)
@@ -200,15 +200,15 @@ def aggregate(self, request, queryset, view):
200200
# continue
201201
# agg_filter &= __filter
202202

203-
if __facet['type'] == 'global':
203+
if __facet['global']:
204204
queryset.aggs.bucket(
205205
'_filter_' + __field,
206-
__facet['type']
206+
'global'
207207
).bucket(__field, agg)
208208
else:
209209
queryset.aggs.bucket(
210210
'_filter_' + __field,
211-
__facet['type'],
211+
'filter',
212212
filter=agg_filter
213213
).bucket(__field, agg)
214214

0 commit comments

Comments
 (0)