@@ -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
0 commit comments