Skip to content

Commit 5857b2a

Browse files
docs updated
1 parent 33dfef8 commit 5857b2a

File tree

5 files changed

+136
-9
lines changed

5 files changed

+136
-9
lines changed

README.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>`.

ROADMAP.rst

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@ Roadmap
22
=======
33
Road-map/upcoming releases.
44

5-
0.7
6-
---
7-
yyyy-mm-dd (future)
8-
9-
- Improved geo-spatial search/filtering/ordering support.
10-
- All filters (mentioned in the ``constants`` module) implemented.
11-
- Aggregations (also for geo-spatial backends).
12-
- Awesome documentation.
5+
See `GitHub issues
6+
<https://github.com/barseghyanartur/django-elasticsearch-dsl-drf/issues>`_
7+
for a complete list of TODOs.

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

setup.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@
5454
'advanced_usage_examples.html#faceted-search'
5555
'>`_'.format(version)
5656
),
57+
(
58+
':doc:`Post-filter filter backend <advanced_usage_examples>`',
59+
'`Post-filter filter backend <'
60+
'http://django-elasticsearch-dsl-drf.readthedocs.io/en/{}/'
61+
'advanced_usage_examples.html#post-filter'
62+
'>`_'.format(version)
63+
),
5764
(
5865
':doc:`Highlight backend <advanced_usage_examples>`',
5966
'`Highlight backend <'

src/django_elasticsearch_dsl_drf/tests/test_filtering_nested.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ def test_schema_fields_with_filter_fields_list(self):
554554
"""Test schema field generator"""
555555
fields = self.backend.get_schema_fields(self.view)
556556
fields = [f.name for f in fields]
557-
self.assertEqual(fields, list(self.view.filter_fields.keys()))
557+
self.assertEqual(fields, list(self.view.nested_filter_fields.keys()))
558558

559559
@unittest.skipIf(not CORE_API_AND_CORE_SCHEMA_ARE_INSTALLED,
560560
CORE_API_AND_CORE_SCHEMA_MISSING_MSG)

0 commit comments

Comments
 (0)