Skip to content

Commit 648bbac

Browse files
prepare 0.4.3
1 parent 32932a7 commit 648bbac

File tree

17 files changed

+223
-23
lines changed

17 files changed

+223
-23
lines changed

CHANGELOG.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ are used for versioning (schema follows below):
1515
0.3.4 to 0.4).
1616
- All backwards incompatible changes are mentioned in this document.
1717

18+
0.4.3
19+
-----
20+
2017-09-28
21+
22+
- Documentation fixes.
23+
- Fixes in tests.
24+
- Improved factories.
25+
1826
0.4.2
1927
-----
2028
2017-09-28

advanced_usage_examples.rst

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ Sample models
173173
174174
Used in Elasticsearch indexing.
175175
"""
176-
return json.dumps([tag.title for tag in self.tags.all()])
176+
return [tag.title for tag in self.tags.all()]
177177
178178
Sample document
179179
---------------
@@ -309,6 +309,7 @@ Document index
309309
analyzer=html_strip,
310310
fields={
311311
'raw': fields.StringField(analyzer='keyword', multi=True),
312+
'suggest': fields.CompletionField(multi=True),
312313
},
313314
multi=True
314315
)
@@ -380,7 +381,10 @@ Sample serializer
380381
381382
def get_tags(self, obj):
382383
"""Get tags."""
383-
return json.loads(obj.tags)
384+
if obj.tags:
385+
return list(obj.tags)
386+
else:
387+
return []
384388
385389
Sample view
386390
-----------
@@ -865,9 +869,8 @@ To make use of suggestions, you should properly indexed your documents using
865869
After that the ``name.suggest``, ``city.suggest``, ``state_province.suggest``
866870
and ``country.suggest`` fields would be available for suggestions feature.
867871

868-
869872
Serializer definition
870-
---------------------
873+
~~~~~~~~~~~~~~~~~~~~~
871874

872875
This is how publisher serializer would look like.
873876

@@ -1097,6 +1100,60 @@ You can also have multiple suggesters per request.
10971100
]
10981101
}
10991102
1103+
Suggestions on Array/List field
1104+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1105+
Suggestions on Array/List fields (typical use case - tags, where Tag model
1106+
would be a many-to-many relation to a Book model) work almost the
1107+
same.
1108+
1109+
Before checking the `Sample requests/responses`, do have in mind the following:
1110+
1111+
- ``Book`` (see the `Sample models`_)
1112+
- ``BookSerializer`` (see the `Sample serializer`_)
1113+
- ``BookDocumentView`` (see the `Sample view`_)
1114+
1115+
Sample requests/responses
1116+
^^^^^^^^^^^^^^^^^^^^^^^^^
1117+
1118+
Once you have extended your view set with ``SuggesterFilterBackend``
1119+
functionality, you can make use of the ``suggest`` custom action of your
1120+
view set.
1121+
1122+
**Request**
1123+
1124+
.. code-block:: text
1125+
1126+
GET http://127.0.0.1:8000/search/books/suggest/?tag_suggest__completion=bio
1127+
1128+
**Response**
1129+
1130+
.. code-block:: javascript
1131+
1132+
{
1133+
"_shards": {
1134+
"failed": 0,
1135+
"successful": 1,
1136+
"total": 1
1137+
},
1138+
"country_suggest__completion": [
1139+
{
1140+
"options": [
1141+
{
1142+
"score": 1.0,
1143+
"text": "Biography"
1144+
},
1145+
{
1146+
"score": 1.0,
1147+
"text": "Biology"
1148+
}
1149+
],
1150+
"offset": 0,
1151+
"length": 2,
1152+
"text": "bio"
1153+
}
1154+
]
1155+
}
1156+
11001157
Pagination
11011158
----------
11021159

docs/advanced_usage_examples.rst

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ Sample models
173173
174174
Used in Elasticsearch indexing.
175175
"""
176-
return json.dumps([tag.title for tag in self.tags.all()])
176+
return [tag.title for tag in self.tags.all()]
177177
178178
Sample document
179179
---------------
@@ -309,6 +309,7 @@ Document index
309309
analyzer=html_strip,
310310
fields={
311311
'raw': fields.StringField(analyzer='keyword', multi=True),
312+
'suggest': fields.CompletionField(multi=True),
312313
},
313314
multi=True
314315
)
@@ -380,7 +381,10 @@ Sample serializer
380381
381382
def get_tags(self, obj):
382383
"""Get tags."""
383-
return json.loads(obj.tags)
384+
if obj.tags:
385+
return list(obj.tags)
386+
else:
387+
return []
384388
385389
Sample view
386390
-----------
@@ -865,9 +869,8 @@ To make use of suggestions, you should properly indexed your documents using
865869
After that the ``name.suggest``, ``city.suggest``, ``state_province.suggest``
866870
and ``country.suggest`` fields would be available for suggestions feature.
867871

868-
869872
Serializer definition
870-
---------------------
873+
~~~~~~~~~~~~~~~~~~~~~
871874

872875
This is how publisher serializer would look like.
873876

@@ -1097,6 +1100,60 @@ You can also have multiple suggesters per request.
10971100
]
10981101
}
10991102
1103+
Suggestions on Array/List field
1104+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1105+
Suggestions on Array/List fields (typical use case - tags, where Tag model
1106+
would be a many-to-many relation to a Book model) work almost the
1107+
same.
1108+
1109+
Before checking the `Sample requests/responses`, do have in mind the following:
1110+
1111+
- ``Book`` (see the `Sample models`_)
1112+
- ``BookSerializer`` (see the `Sample serializer`_)
1113+
- ``BookDocumentView`` (see the `Sample view`_)
1114+
1115+
Sample requests/responses
1116+
^^^^^^^^^^^^^^^^^^^^^^^^^
1117+
1118+
Once you have extended your view set with ``SuggesterFilterBackend``
1119+
functionality, you can make use of the ``suggest`` custom action of your
1120+
view set.
1121+
1122+
**Request**
1123+
1124+
.. code-block:: text
1125+
1126+
GET http://127.0.0.1:8000/search/books/suggest/?tag_suggest__completion=bio
1127+
1128+
**Response**
1129+
1130+
.. code-block:: javascript
1131+
1132+
{
1133+
"_shards": {
1134+
"failed": 0,
1135+
"successful": 1,
1136+
"total": 1
1137+
},
1138+
"country_suggest__completion": [
1139+
{
1140+
"options": [
1141+
{
1142+
"score": 1.0,
1143+
"text": "Biography"
1144+
},
1145+
{
1146+
"score": 1.0,
1147+
"text": "Biology"
1148+
}
1149+
],
1150+
"offset": 0,
1151+
"length": 2,
1152+
"text": "bio"
1153+
}
1154+
]
1155+
}
1156+
11001157
Pagination
11011158
----------
11021159

docs/changelog.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ are used for versioning (schema follows below):
1515
0.3.4 to 0.4).
1616
- All backwards incompatible changes are mentioned in this document.
1717

18+
0.4.3
19+
-----
20+
2017-09-28
21+
22+
- Documentation fixes.
23+
- Fixes in tests.
24+
1825
0.4.2
1926
-----
2027
2017-09-28

docs/quick_start.rst

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ Book model
284284
285285
Used in Elasticsearch indexing.
286286
"""
287-
return json.dumps([tag.title for tag in self.tags.all()])
287+
return [tag.title for tag in self.tags.all()]
288288
289289
Admin classes
290290
-------------
@@ -534,6 +534,7 @@ Document definition
534534
analyzer=html_strip,
535535
fields={
536536
'raw': fields.StringField(analyzer='keyword', multi=True),
537+
'suggest': fields.CompletionField(multi=True),
537538
},
538539
multi=True
539540
)
@@ -737,7 +738,10 @@ needed to be serialized and leave it further to the dynamic serializer.
737738
738739
def get_tags(self, obj):
739740
"""Get tags."""
740-
return json.loads(obj.tags)
741+
if obj.tags:
742+
return list(obj.tags)
743+
else:
744+
return []
741745
742746
However, if dynamic serializer doesn't work for your or you want to customize
743747
too many things, you are free to use standard ``Serializer`` class of the
@@ -787,7 +791,10 @@ Django REST framework.
787791
788792
def get_tags(self, obj):
789793
"""Get tags."""
790-
return json.loads(obj.tags)
794+
if obj.tags:
795+
return list(obj.tags)
796+
else:
797+
return []
791798
792799
ViewSet definition
793800
------------------

examples/simple/books/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def tags_indexing(self):
152152
153153
Used in Elasticsearch indexing.
154154
"""
155-
return json.dumps([tag.title for tag in self.tags.all()])
155+
return [tag.title for tag in self.tags.all()]
156156

157157
@property
158158
def null_field_indexing(self):

examples/simple/factories/books_book.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
)
2020
from .books_order import OrderFactory
2121
from .books_orderline import OrderLineFactory
22-
from .books_tag import LimitedTagFactory
22+
from .books_tag import LimitedTagFactory, TagGenreFactory
2323

2424
__all__ = (
2525
'BookFactory',
@@ -57,7 +57,7 @@ def tags(obj, created, extracted, **kwargs):
5757
if created:
5858
# Create from 1 to 7 ``Tag`` objects.
5959
amount = random.randint(1, 7)
60-
tags = LimitedTagFactory.create_batch(amount, **kwargs)
60+
tags = TagGenreFactory.create_batch(amount, **kwargs)
6161
obj.tags.add(*tags)
6262

6363
@post_generation

examples/simple/factories/books_tag.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import random
22

33
from factory import DjangoModelFactory, LazyAttribute
4+
from factory.fuzzy import FuzzyChoice
45

56
from books.models import Tag
67

8+
from .constants import BOOK_GENRES
79
from .factory_faker import Faker
810

911
__all__ = (
1012
'TagFactory',
13+
'TagGenreFactory',
1114
'LimitedTagFactory',
1215
)
1316

@@ -31,6 +34,17 @@ class TagFactory(BaseTagFactory):
3134
"""Tag factory."""
3235

3336

37+
class TagGenreFactory(BaseTagFactory):
38+
"""Tag factory consisting of genres."""
39+
40+
title = FuzzyChoice(BOOK_GENRES)
41+
42+
class Meta(object):
43+
"""Meta class."""
44+
45+
django_get_or_create = ('title',)
46+
47+
3448
class LimitedTagFactory(BaseTagFactory):
3549
"""Tag factory, but limited to 20 tags."""
3650

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
__all__ = (
2+
'BOOK_GENRES',
3+
)
4+
5+
BOOK_GENRES = [
6+
"Children's",
7+
'Action and Adventure',
8+
'Anthology',
9+
'Art',
10+
'Autobiographies',
11+
'Biographies',
12+
'Comics',
13+
'Cookbooks',
14+
'Diaries',
15+
'Dictionaries',
16+
'Drama',
17+
'Encyclopedias',
18+
'Fantasy',
19+
'Guide',
20+
'Health',
21+
'History',
22+
'Horror',
23+
'Journals',
24+
'Math',
25+
'Mystery',
26+
'Poetry',
27+
'Prayer books',
28+
'Religion, Spirituality & New Age',
29+
'Romance',
30+
'Satire',
31+
'Science fiction',
32+
'Science',
33+
'Self help',
34+
'Series',
35+
'Travel',
36+
'Trilogy',
37+
]

examples/simple/search_indexes/documents/book.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class BookDocument(DocType):
105105
analyzer=html_strip,
106106
fields={
107107
'raw': KeywordField(multi=True),
108+
'suggest': fields.CompletionField(multi=True),
108109
},
109110
multi=True
110111
)

0 commit comments

Comments
 (0)