Skip to content

Commit 5695d09

Browse files
prepare 0.12
1 parent f633a82 commit 5695d09

File tree

19 files changed

+588
-26
lines changed

19 files changed

+588
-26
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ are used for versioning (schema follows below):
1717

1818
0.12
1919
----
20-
yyyy-mm-dd (not released yet)
20+
2018-07-21
2121

2222
- New-style Search Filter Backends. Old style ``SearchFilterBackend`` is
2323
still supported (until at least version 0.16), but is deprecated. Migrate to

TODOS.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ Must haves
1111
==========
1212
.. code-block:: text
1313
14+
- Add tests for ``match_phrase`` and ``match_phrase_prefix`` search query
15+
backends.
16+
- Finish ``more-like-this`` view set integration.
1417
- Full support of all optional params of each native Elasticsearch filter,
1518
such as ``flags`` on ``regexp``.
1619
- Support ``mode`` argument in the ``OrderingFilterBackend``.

docs/changelog.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ are used for versioning (schema follows below):
1717

1818
0.12
1919
----
20-
yyyy-mm-dd (not released yet)
20+
2018-07-21
2121

2222
- New-style Search Filter Backends. Old style ``SearchFilterBackend`` is
2323
still supported (until at least version 0.16), but is deprecated. Migrate to
@@ -30,6 +30,7 @@ yyyy-mm-dd (not released yet)
3030
incorrect in your case, write custom permissions and declare the explicitly
3131
in your view-sets.
3232
- Fixed geo-spatial ``geo_distance`` ordering for Elastic 5.x. and 6.x.
33+
- Fixes occasionally failing tests.
3334

3435
0.11
3536
----

docs/documentation.rst.distrib

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Contents:
1212
installing_elasticsearch
1313
quick_start
1414
filtering_usage_examples
15+
search_backends
1516
basic_usage_examples
1617
advanced_usage_examples
1718
nested_fields_usage_examples

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ Contents:
204204
installing_elasticsearch
205205
quick_start
206206
filtering_usage_examples
207+
search_backends
207208
basic_usage_examples
208209
advanced_usage_examples
209210
nested_fields_usage_examples

docs/search_backends.rst

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
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``.

examples/simple/search_indexes/urls.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
BookFunctionalSuggesterDocumentViewSet,
1010
BookMoreLikeThisDocumentViewSet,
1111
BookMultiMatchSearchFilterBackendDocumentViewSet,
12+
BookMultiMatchOptionsPhasePrefixSearchFilterBackendDocumentViewSet,
1213
BookOrderingByScoreCompoundSearchBackendDocumentViewSet,
1314
BookOrderingByScoreDocumentViewSet,
1415
CityCompoundSearchBackendDocumentViewSet,
@@ -74,13 +75,18 @@
7475
base_name='bookdocument_compound_search_backend_ordered_by_score'
7576
)
7677

77-
78-
books_compound_search_backend_ordered_by_score = router.register(
78+
books_multi_match_backend_ordered_by_score = router.register(
7979
r'books-multi-match-search-backend',
8080
BookMultiMatchSearchFilterBackendDocumentViewSet,
8181
base_name='bookdocument_multi_match_search_backend'
8282
)
8383

84+
books_multi_match_phrase_prefix_backend_ordered_by_score = router.register(
85+
r'books-multi-match-phrase-prefix-search-backend',
86+
BookMultiMatchOptionsPhasePrefixSearchFilterBackendDocumentViewSet,
87+
base_name='bookdocument_multi_match_phrase_prefix_search_backend'
88+
)
89+
8490
cities = router.register(
8591
r'cities',
8692
CityDocumentViewSet,

examples/simple/search_indexes/viewsets/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
BookFunctionalSuggesterDocumentViewSet,
88
BookMoreLikeThisDocumentViewSet,
99
BookMultiMatchSearchFilterBackendDocumentViewSet,
10+
BookMultiMatchOptionsPhasePrefixSearchFilterBackendDocumentViewSet,
1011
BookOrderingByScoreCompoundSearchBackendDocumentViewSet,
1112
BookOrderingByScoreDocumentViewSet,
1213
)
@@ -22,6 +23,7 @@
2223
'BookFunctionalSuggesterDocumentViewSet',
2324
'BookMoreLikeThisDocumentViewSet',
2425
'BookMultiMatchSearchFilterBackendDocumentViewSet',
26+
'BookMultiMatchOptionsPhasePrefixSearchFilterBackendDocumentViewSet',
2527
'BookOrderingByScoreCompoundSearchBackendDocumentViewSet',
2628
'BookOrderingByScoreDocumentViewSet',
2729
'CityCompoundSearchBackendDocumentViewSet',

examples/simple/search_indexes/viewsets/book.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,15 @@
4444
from ..serializers import BookDocumentSimpleSerializer
4545

4646
__all__ = (
47+
'BookCompoundSearchBackendDocumentViewSet',
48+
'BookDefaultFilterLookupDocumentViewSet',
4749
'BookDocumentViewSet',
48-
'BookOrderingByScoreDocumentViewSet',
4950
'BookFunctionalSuggesterDocumentViewSet',
5051
'BookMoreLikeThisDocumentViewSet',
51-
'BookDefaultFilterLookupDocumentViewSet',
52-
'BookCompoundSearchBackendDocumentViewSet',
53-
'BookOrderingByScoreCompoundSearchBackendDocumentViewSet',
52+
'BookMultiMatchOptionsPhasePrefixSearchFilterBackendDocumentViewSet',
5453
'BookMultiMatchSearchFilterBackendDocumentViewSet',
54+
'BookOrderingByScoreCompoundSearchBackendDocumentViewSet',
55+
'BookOrderingByScoreDocumentViewSet',
5556
)
5657

5758

@@ -442,12 +443,44 @@ class BookMultiMatchSearchFilterBackendDocumentViewSet(
442443
]
443444

444445
search_fields = {
446+
'title': None,
447+
'summary': None,
448+
'description': None,
449+
}
450+
ordering = ('_score', 'id', 'title', 'price',)
451+
452+
multi_match_options = {
453+
'type': 'phrase',
454+
}
455+
456+
457+
class BookMultiMatchOptionsPhasePrefixSearchFilterBackendDocumentViewSet(
458+
BookMultiMatchSearchFilterBackendDocumentViewSet
459+
):
460+
"""Same as parent, but uses `multi_match_search_fields` declarations.
461+
462+
Additionally, uses `phase_prefix`.
463+
"""
464+
465+
filter_backends = [
466+
FilteringFilterBackend,
467+
PostFilterFilteringFilterBackend,
468+
IdsFilterBackend,
469+
OrderingFilterBackend,
470+
DefaultOrderingFilterBackend,
471+
MultiMatchSearchFilterBackend,
472+
FacetedSearchFilterBackend,
473+
HighlightBackend,
474+
SuggesterFilterBackend,
475+
]
476+
477+
multi_match_search_fields = {
445478
'title': {'boost': 4},
446479
'summary': {'boost': 2},
447480
'description': None,
448481
}
449482
ordering = ('_score', 'id', 'title', 'price',)
450483

451484
multi_match_options = {
452-
'type': 'phrase'
485+
'type': 'phrase_prefix',
453486
}

scripts/prepare_docs.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
cat README.rst docs/documentation.rst.distrib > docs/index.rst
33
cat CHANGELOG.rst > docs/changelog.rst
44
cat installing_elasticsearch.rst > docs/installing_elasticsearch.rst
5+
cat search_backends.rst > docs/search_backends.rst
56
cat quick_start.rst > docs/quick_start.rst
67
cat basic_usage_examples.rst > docs/basic_usage_examples.rst
78
cat advanced_usage_examples.rst > docs/advanced_usage_examples.rst

0 commit comments

Comments
 (0)