@@ -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 ``
866870and ``country.suggest `` fields would be available for suggestions feature.
867871
868-
869872Serializer definition
870- ---------------------
873+ ~~~~~~~~~~~~~~~~~~~~~
871874
872875This 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
0 commit comments