|
| 1 | +======================== |
| 2 | +Source filtering backend |
| 3 | +======================== |
| 4 | +Allows to control how the `_source` field is returned with every hit. |
| 5 | + |
| 6 | +By default operations return the contents of the _source field unless you have |
| 7 | +used the `stored_fields` parameter or if the `_source` field is disabled. |
| 8 | + |
| 9 | +You can turn off `_source` retrieval by using the `source` parameter: |
| 10 | + |
| 11 | +.. code-block:: python |
| 12 | +
|
| 13 | + from django_elasticsearch_dsl_drf.filter_backends import ( |
| 14 | + SourceBackend |
| 15 | + ) |
| 16 | + from django_elasticsearch_dsl_drf.viewsets import ( |
| 17 | + BaseDocumentViewSet, |
| 18 | + ) |
| 19 | +
|
| 20 | + # Local article document definition |
| 21 | + from .documents import ArticleDocument |
| 22 | +
|
| 23 | + # Local article document serializer |
| 24 | + from .serializers import ArticleDocumentSerializer |
| 25 | +
|
| 26 | + class ArticleDocumentView(BaseDocumentViewSet): |
| 27 | +
|
| 28 | + document = ArticleDocument |
| 29 | + serializer_class = ArticleDocumentSerializer |
| 30 | + filter_backends = [SourceBackend,] |
| 31 | + source = ["title"] |
| 32 | +
|
| 33 | +
|
| 34 | +To disable `_source` retrieval set to False: |
| 35 | + |
| 36 | +.. code-block:: python |
| 37 | +
|
| 38 | + # ... |
| 39 | + source = False |
| 40 | + # ... |
| 41 | +
|
| 42 | +
|
| 43 | +The `source` also accepts one or more wildcard patterns to control what parts |
| 44 | +of the `_source` should be returned: |
| 45 | + |
| 46 | +.. code-block:: python |
| 47 | +
|
| 48 | + # ... |
| 49 | + source = ["title", "author.*"] |
| 50 | + # ... |
| 51 | +
|
| 52 | +Finally, for complete control, you can specify both `includes` and `excludes` |
| 53 | +patterns: |
| 54 | + |
| 55 | +.. code-block:: python |
| 56 | +
|
| 57 | + # ... |
| 58 | + source = { |
| 59 | + "includes": ["title", "author.*"], |
| 60 | + "excludes": [ "*.description" ] |
| 61 | + } |
| 62 | + # ... |
| 63 | +
|
| 64 | +.. note:: |
| 65 | + |
| 66 | + Source can make queries lighter. However, it can break current |
| 67 | + functionality. Use it with caution. |
0 commit comments