|
| 1 | +Global aggregations |
| 2 | +=================== |
| 3 | +Global aggregations (facets) are regular aggregations, which are not influenced |
| 4 | +by the search query/filter. They deliver results similar to `post_filter`. |
| 5 | + |
| 6 | +Sample view |
| 7 | +----------- |
| 8 | + |
| 9 | +.. code-block:: python |
| 10 | +
|
| 11 | + from django_elasticsearch_dsl_drf.filter_backends import ( |
| 12 | + CompoundSearchFilterBackend, |
| 13 | + DefaultOrderingFilterBackend, |
| 14 | + FacetedSearchFilterBackend, |
| 15 | + OrderingFilterBackend, |
| 16 | + ) |
| 17 | + from django_elasticsearch_dsl_drf.viewsets import DocumentViewSet |
| 18 | +
|
| 19 | + from .documents import BookDocument |
| 20 | + from .serializers import BookDocumentSerializer |
| 21 | +
|
| 22 | + class BookCompoundSearchBackendDocumentViewSet(DocumentViewSet): |
| 23 | +
|
| 24 | + document = BookDocument |
| 25 | + serializer_class = BookDocumentSerializer |
| 26 | + lookup_field = 'id' |
| 27 | +
|
| 28 | + filter_backends = [ |
| 29 | + # ... |
| 30 | + OrderingFilterBackend, |
| 31 | + DefaultOrderingFilterBackend, |
| 32 | + CompoundSearchFilterBackend, |
| 33 | + FacetedSearchFilterBackend, |
| 34 | + # ... |
| 35 | + ] |
| 36 | +
|
| 37 | + faceted_search_fields = { |
| 38 | + 'publisher': { |
| 39 | + 'field': 'publisher.raw', |
| 40 | + 'enabled': True, |
| 41 | + 'global': True, # This makes the aggregation global |
| 42 | + }, |
| 43 | + } |
| 44 | +
|
| 45 | +Sample request |
| 46 | +-------------- |
| 47 | + |
| 48 | +.. code-block:: text |
| 49 | +
|
| 50 | + http://localhost:8000/search/books/?facet=state_global&state=rejected |
| 51 | +
|
| 52 | +Generated query |
| 53 | +--------------- |
| 54 | + |
| 55 | +.. code-block:: javascript |
| 56 | + { |
| 57 | + "from":0, |
| 58 | + "query":{ |
| 59 | + "bool":{ |
| 60 | + "filter":[ |
| 61 | + { |
| 62 | + "terms":{ |
| 63 | + "state.raw":[ |
| 64 | + "rejected" |
| 65 | + ] |
| 66 | + } |
| 67 | + } |
| 68 | + ] |
| 69 | + } |
| 70 | + }, |
| 71 | + "size":25, |
| 72 | + "aggs":{ |
| 73 | + "_filter_state_global":{ |
| 74 | + "aggs":{ |
| 75 | + "state_global":{ |
| 76 | + "terms":{ |
| 77 | + "field":"state.raw" |
| 78 | + } |
| 79 | + } |
| 80 | + }, |
| 81 | + "global":{ |
| 82 | +
|
| 83 | + } |
| 84 | + } |
| 85 | + }, |
| 86 | + "sort":[ |
| 87 | + "id", |
| 88 | + "title", |
| 89 | + "price" |
| 90 | + ] |
| 91 | + } |
| 92 | +
|
| 93 | +Sample response |
| 94 | +--------------- |
| 95 | + |
| 96 | +.. code-block:: javascript |
| 97 | +
|
| 98 | + { |
| 99 | + "count": 25, |
| 100 | + "next": null, |
| 101 | + "previous": null, |
| 102 | + "facets": { |
| 103 | + "_filter_state_global": { |
| 104 | + "state_global": { |
| 105 | + "buckets": [ |
| 106 | + { |
| 107 | + "doc_count": 29, |
| 108 | + "key": "not_published" |
| 109 | + }, |
| 110 | + { |
| 111 | + "doc_count": 25, |
| 112 | + "key": "in_progress" |
| 113 | + }, |
| 114 | + { |
| 115 | + "doc_count": 25, |
| 116 | + "key": "rejected" |
| 117 | + }, |
| 118 | + { |
| 119 | + "doc_count": 21, |
| 120 | + "key": "cancelled" |
| 121 | + }, |
| 122 | + { |
| 123 | + "doc_count": 17, |
| 124 | + "key": "published" |
| 125 | + } |
| 126 | + ], |
| 127 | + "sum_other_doc_count": 0, |
| 128 | + "doc_count_error_upper_bound": 0 |
| 129 | + }, |
| 130 | + "doc_count": 117 |
| 131 | + } |
| 132 | + }, |
| 133 | + "results": [ |
| 134 | + { |
| 135 | + "id": 1007489, |
| 136 | + "title": "Cupiditate qui nulla itaque maxime impedit.", |
| 137 | + "description": null, |
| 138 | + "summary": "Aut recusandae architecto incidunt quaerat odio .", |
| 139 | + "authors": [ |
| 140 | + "Evy Vermeulen", |
| 141 | + "Tycho Weijland", |
| 142 | + "Rik Zeldenrust" |
| 143 | + ], |
| 144 | + "publisher": "Overdijk Inc", |
| 145 | + "publication_date": "2014-02-28", |
| 146 | + "state": "rejected", |
| 147 | + "isbn": "978-0-15-184366-4", |
| 148 | + "price": 6.53, |
| 149 | + "pages": 82, |
| 150 | + "stock_count": 30, |
| 151 | + "tags": [ |
| 152 | + "Trilogy" |
| 153 | + ], |
| 154 | + "highlight": {}, |
| 155 | + "null_field": null, |
| 156 | + "score": null |
| 157 | + }, |
| 158 | + # ... |
| 159 | + ] |
| 160 | + } |
0 commit comments