|
| 1 | +=============== |
| 2 | +Search backends |
| 3 | +=============== |
| 4 | +Compound search filter backend |
| 5 | +------------------------------ |
| 6 | +Compound search filter backend aims to replace old style `SearchFilterBackend`. |
| 7 | + |
| 8 | +Sample view |
| 9 | +~~~~~~~~~~~ |
| 10 | + |
| 11 | +.. code-block:: python |
| 12 | +
|
| 13 | + from django_elasticsearch_dsl_drf.filter_backends import ( |
| 14 | + DefaultOrderingFilterBackend, |
| 15 | + CompoundSearchFilterBackend, |
| 16 | + OrderingFilterBackend, |
| 17 | + ) |
| 18 | + from django_elasticsearch_dsl_drf.viewsets import DocumentViewSet |
| 19 | +
|
| 20 | + from .documents import BookDocument |
| 21 | + from .serializers import BookDocumentSerializer |
| 22 | +
|
| 23 | + class BookCompoundSearchBackendDocumentViewSet(DocumentViewSet): |
| 24 | +
|
| 25 | + document = BookDocument |
| 26 | + serializer_class = BookDocumentSerializer |
| 27 | + lookup_field = 'id' |
| 28 | +
|
| 29 | + filter_backends = [ |
| 30 | + # ... |
| 31 | + OrderingFilterBackend, |
| 32 | + DefaultOrderingFilterBackend, |
| 33 | + CompoundSearchFilterBackend, |
| 34 | + # ... |
| 35 | + ] |
| 36 | +
|
| 37 | + search_fields = ( |
| 38 | + 'title', |
| 39 | + 'description', |
| 40 | + 'summary', |
| 41 | + ) |
| 42 | +
|
| 43 | + ordering = ('_score', 'id', 'title', 'price',) |
| 44 | +
|
| 45 | +Sample request |
| 46 | +~~~~~~~~~~~~~~ |
| 47 | + |
| 48 | +.. code-block:: text |
| 49 | +
|
| 50 | + http://localhost:8000/search/books-compound-search-backend/?search=enim |
| 51 | +
|
| 52 | +Generated query |
| 53 | +~~~~~~~~~~~~~~~ |
| 54 | + |
| 55 | +.. code-block:: javascript |
| 56 | +
|
| 57 | + { |
| 58 | + "from": 0, |
| 59 | + "sort": [ |
| 60 | + "id", |
| 61 | + "title", |
| 62 | + "price" |
| 63 | + ], |
| 64 | + "size": 23, |
| 65 | + "query": { |
| 66 | + "bool": { |
| 67 | + "should": [ |
| 68 | + { |
| 69 | + "match": { |
| 70 | + "title": { |
| 71 | + "query": "enim" |
| 72 | + } |
| 73 | + } |
| 74 | + }, |
| 75 | + { |
| 76 | + "match": { |
| 77 | + "description": { |
| 78 | + "query": "enim" |
| 79 | + } |
| 80 | + } |
| 81 | + }, |
| 82 | + { |
| 83 | + "match": { |
| 84 | + "summary": { |
| 85 | + "query": "enim" |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + ] |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +
|
| 94 | +Multi match search filter backend |
| 95 | +--------------------------------- |
| 96 | +Document and serializer definition are trivial (there are lots of examples |
| 97 | +in other sections). |
| 98 | + |
| 99 | +Sample view |
| 100 | +~~~~~~~~~~~ |
| 101 | + |
| 102 | +.. code-block:: python |
| 103 | +
|
| 104 | + from django_elasticsearch_dsl_drf.filter_backends import ( |
| 105 | + DefaultOrderingFilterBackend, |
| 106 | + MultiMatchSearchFilterBackend, |
| 107 | + OrderingFilterBackend, |
| 108 | + ) |
| 109 | + from django_elasticsearch_dsl_drf.viewsets import DocumentViewSet |
| 110 | +
|
| 111 | + from .documents import BookDocument |
| 112 | + from .serializers import BookDocumentSerializer |
| 113 | +
|
| 114 | +
|
| 115 | + class BookMultiMatchSearchFilterBackendDocumentViewSet(DocumentViewSet): |
| 116 | +
|
| 117 | + document = BookDocument |
| 118 | + serializer_class = BookDocumentSerializer |
| 119 | + lookup_field = 'id' |
| 120 | +
|
| 121 | + filter_backends = [ |
| 122 | + # ... |
| 123 | + OrderingFilterBackend, |
| 124 | + DefaultOrderingFilterBackend, |
| 125 | + MultiMatchSearchFilterBackend, |
| 126 | + # ... |
| 127 | + ] |
| 128 | +
|
| 129 | + search_fields = { |
| 130 | + 'title': {'boost': 4}, |
| 131 | + 'summary': {'boost': 2}, |
| 132 | + 'description': None, |
| 133 | + } |
| 134 | +
|
| 135 | + ordering = ('_score', 'id', 'title', 'price',) |
| 136 | +
|
| 137 | +Sample request |
| 138 | +~~~~~~~~~~~~~~ |
| 139 | +.. note:: |
| 140 | + |
| 141 | + Multiple search params (`search_multi_match`) are not supported. Even if |
| 142 | + you provide multiple search params, the first one would be picked, having |
| 143 | + the rest simply ignored. |
| 144 | + |
| 145 | +.. code-block:: text |
| 146 | +
|
| 147 | + http://localhost:8000/search/books-multi-match-search-backend/?search_multi_match=debitis%20enim |
| 148 | +
|
| 149 | +Generated query |
| 150 | +~~~~~~~~~~~~~~~ |
| 151 | + |
| 152 | +.. code-block:: javascript |
| 153 | +
|
| 154 | + { |
| 155 | + "from": 0, |
| 156 | + "query": { |
| 157 | + "multi_match": { |
| 158 | + "query": "debitis enim", |
| 159 | + "fields": [ |
| 160 | + "summary^2", |
| 161 | + "description", |
| 162 | + "title^4" |
| 163 | + ] |
| 164 | + } |
| 165 | + }, |
| 166 | + "size": 38, |
| 167 | + "sort": [ |
| 168 | + "_score", |
| 169 | + "id", |
| 170 | + "title", |
| 171 | + "price" |
| 172 | + ] |
| 173 | + } |
| 174 | +
|
| 175 | +Options |
| 176 | +~~~~~~~ |
| 177 | +All standard multi match query options are available/tunable with help of |
| 178 | +``multi_match_options`` view property. |
| 179 | + |
| 180 | +Selective list of available options: |
| 181 | + |
| 182 | +- operator |
| 183 | +- type |
| 184 | +- analyzer |
| 185 | +- tie_breaker |
| 186 | + |
| 187 | +Type options |
| 188 | +^^^^^^^^^^^^ |
| 189 | + |
| 190 | +See the `Elasticsearch docs |
| 191 | +<https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html#type-phrase>`_ |
| 192 | +for detailed explanation. |
| 193 | + |
| 194 | +- best_fields |
| 195 | +- most_fields |
| 196 | +- cross_fields |
| 197 | +- phrase |
| 198 | +- phrase_prefix |
| 199 | + |
| 200 | +**Example** |
| 201 | + |
| 202 | +.. code-block:: python |
| 203 | +
|
| 204 | + class BookMultiMatchSearchFilterBackendDocumentViewSet(DocumentViewSet): |
| 205 | +
|
| 206 | + # ... |
| 207 | +
|
| 208 | + multi_match_options = { |
| 209 | + 'type': 'phrase' |
| 210 | + } |
| 211 | +
|
| 212 | +Operator options |
| 213 | +^^^^^^^^^^^^^^^^ |
| 214 | +Can be either ``and`` or ``or``. |
0 commit comments