Skip to content

Commit f9ce78d

Browse files
docs updated
1 parent d320032 commit f9ce78d

File tree

3 files changed

+132
-3
lines changed

3 files changed

+132
-3
lines changed

docs/advanced_usage_examples.rst

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,130 @@ In the example below, we show results with faceted ``state`` and
759759
760760
http://127.0.0.1:8000/search/books/?facet=state&facet=pages_count
761761
762+
Post-filter
763+
-----------
764+
The `post_filter` is very similar to the common filter. The only difference
765+
is that it doesn't affect facets. So, whatever post-filters applied, the
766+
numbers in facets will remain intact.
767+
768+
Sample view
769+
~~~~~~~~~~~
770+
.. note::
771+
772+
Note the ``PostFilterFilteringFilterBackend`` and ``post_filter_fields``
773+
usage.
774+
775+
*search_indexes/viewsets/book.py*
776+
777+
.. code-block:: python
778+
779+
# ...
780+
781+
from django_elasticsearch_dsl_drf.filter_backends import (
782+
# ...
783+
PostFilterFilteringFilterBackend,
784+
)
785+
786+
# ...
787+
788+
class BookDocumentView(DocumentViewSet):
789+
"""The BookDocument view."""
790+
791+
document = BookDocument
792+
serializer_class = BookDocumentSerializer
793+
lookup_field = 'id'
794+
filter_backends = [
795+
FilteringFilterBackend,
796+
OrderingFilterBackend,
797+
DefaultOrderingFilterBackend,
798+
SearchFilterBackend,
799+
PostFilterFilteringFilterBackend,
800+
]
801+
# Define search fields
802+
search_fields = (
803+
'title',
804+
'summary',
805+
'description',
806+
)
807+
# Define filtering fields
808+
filter_fields = {
809+
'id': {
810+
'field': '_id',
811+
'lookups': [
812+
LOOKUP_FILTER_RANGE,
813+
LOOKUP_QUERY_IN,
814+
],
815+
},
816+
'publisher': 'publisher.raw',
817+
'publication_date': 'publication_date',
818+
'isbn': 'isbn.raw',
819+
'tags': {
820+
'field': 'tags',
821+
'lookups': [
822+
LOOKUP_FILTER_TERMS,
823+
LOOKUP_FILTER_PREFIX,
824+
LOOKUP_FILTER_WILDCARD,
825+
LOOKUP_QUERY_IN,
826+
LOOKUP_QUERY_EXCLUDE,
827+
],
828+
},
829+
'tags.raw': {
830+
'field': 'tags.raw',
831+
'lookups': [
832+
LOOKUP_FILTER_TERMS,
833+
LOOKUP_FILTER_PREFIX,
834+
LOOKUP_FILTER_WILDCARD,
835+
LOOKUP_QUERY_IN,
836+
LOOKUP_QUERY_EXCLUDE,
837+
],
838+
},
839+
}
840+
# Define post-filter filtering fields
841+
post_filter_fields = {
842+
'publisher_pf': 'publisher.raw',
843+
'isbn_pf': 'isbn.raw',
844+
'state_pf': 'state.raw',
845+
'tags_pf': {
846+
'field': 'tags',
847+
'lookups': [
848+
LOOKUP_FILTER_TERMS,
849+
LOOKUP_FILTER_PREFIX,
850+
LOOKUP_FILTER_WILDCARD,
851+
LOOKUP_QUERY_IN,
852+
LOOKUP_QUERY_EXCLUDE,
853+
],
854+
},
855+
}
856+
# Define ordering fields
857+
ordering_fields = {
858+
'id': 'id',
859+
'title': 'title.raw',
860+
'price': 'price.raw',
861+
'state': 'state.raw',
862+
'publication_date': 'publication_date',
863+
}
864+
# Specify default ordering
865+
ordering = ('id', 'title',)
866+
867+
Sample queries
868+
~~~~~~~~~~~~~~
869+
870+
**Filter documents by field**
871+
872+
Filter documents by field (``state``) "published".
873+
874+
.. code-block:: text
875+
876+
http://127.0.0.1:8080/search/books/?state_pf=published
877+
878+
**Filter documents by multiple fields**
879+
880+
Filter documents by field (``states``) "published" and "in_progress".
881+
882+
.. code-block:: text
883+
884+
http://127.0.0.1:8080/search/books/?state_pf__in=published|in_progress
885+
762886
Geo-spatial features
763887
--------------------
764888

docs/changelog.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ are used for versioning (schema follows below):
1515
0.3.4 to 0.4).
1616
- All backwards incompatible changes are mentioned in this document.
1717

18-
0.8.5
19-
-----
20-
yyyy-mm-dd (not released yet)
18+
0.9
19+
---
20+
2018-07-04
2121

22+
- Introduced ``post_filter`` support.
23+
- Generalised the ``FilteringFilterBackend`` backend. Both
24+
``PostFilterFilteringFilterBackend`` and ``NestedFilteringFilterBackend``
25+
backends are now primarily based on it.
2226
- Reduced Elastic queries from 3 to 2 when using ``LimitOffsetPagination``.
2327

2428
0.8.4

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Main features and highlights
4949
- :doc:`Geo-spatial ordering filter backend <advanced_usage_examples>` (the
5050
following filters implemented: ``geo_distance``).
5151
- :doc:`Faceted search filter backend <advanced_usage_examples>`.
52+
- :doc:`Post-filter filter backend <advanced_usage_examples>`.
5253
- :doc:`Nested filtering filter backend <nested_fields_usage_examples>`.
5354
- :doc:`Highlight backend <advanced_usage_examples>`.
5455
- :doc:`Suggester filter backend <advanced_usage_examples>`.

0 commit comments

Comments
 (0)