@@ -1303,6 +1303,192 @@ view set.
13031303 ]
13041304 }
13051305
1306+ Context suggesters
1307+ ^^^^^^^^^^^^^^^^^^
1308+ Note, that context suggesters only work for `completion ` (thus, not for `term `
1309+ or `phrase `).
1310+
1311+ `category ` context
1312+ ++++++++++++++++++
1313+ The completion suggester considers all documents in the index, but it is often
1314+ desirable to serve suggestions filtered and/or boosted by some criteria. For
1315+ example, you want to suggest song titles filtered by certain artists or you
1316+ want to boost song titles based on their genre.
1317+
1318+ In that case, the document definition should be altered as follows:
1319+
1320+ **Document definition **
1321+
1322+ .. code-block :: python
1323+
1324+ class BookDocument (DocType ):
1325+
1326+ # ...
1327+
1328+ title = StringField(
1329+ analyzer = html_strip,
1330+ fields = {
1331+ ' raw' : KeywordField(),
1332+ ' suggest' : fields.CompletionField(),
1333+ ' suggest_context' : fields.CompletionField(
1334+ contexts = [
1335+ {
1336+ " name" : " tag" ,
1337+ " type" : " category" ,
1338+ # The `path` value shall be pointing to an
1339+ # existing field of current document, which shall
1340+ # be used for filtering.
1341+ " path" : " tags.raw" ,
1342+ },
1343+ ]
1344+ ),
1345+ }
1346+ )
1347+
1348+ # Tags
1349+ tags = StringField(
1350+ attr = ' tags_indexing' ,
1351+ analyzer = html_strip,
1352+ fields = {
1353+ ' raw' : KeywordField(multi = True ),
1354+ ' suggest' : fields.CompletionField(multi = True ),
1355+ },
1356+ multi = True
1357+ )
1358+
1359+ # ...
1360+
1361+ ViewSet should altered as follows:
1362+
1363+ **ViewSet definition **
1364+
1365+ .. code-block :: python
1366+
1367+ class BookFrontendDocumentViewSet (DocumentViewSet ):
1368+
1369+ # ...
1370+
1371+ # Suggester fields
1372+ suggester_fields = {
1373+ ' title_suggest_context' : {
1374+ ' field' : ' title.suggest_context' ,
1375+ ' default_suggester' : SUGGESTER_COMPLETION ,
1376+ # We want to be able to filter the completion filter
1377+ # results on the following params: tag, state and publisher.
1378+ # We also want to provide the size value.
1379+ # See the "https://www.elastic.co/guide/en/elasticsearch/
1380+ # reference/6.1/suggester-context.html" for the reference.
1381+ ' completion_options' : {
1382+ ' filters' : {
1383+ # The `tag` has been defined as `name` value in the
1384+ # `suggest_context` of the `BookDocument`.
1385+ ' title_suggest_tag' : ' tag' ,
1386+ },
1387+ ' size' : 10 ,
1388+ }
1389+ },
1390+ }
1391+
1392+ # ...
1393+
1394+ And finally we can narrow our suggestions as follows:
1395+
1396+ **Sample request **
1397+
1398+ In the example below we have filtered suggestions by tags "Art" and "Comics"
1399+ having boosted "Comics" by 2.0.
1400+
1401+ .. code-block :: text
1402+
1403+ GET http://localhost:8000/search/books-frontend/suggest/?title_suggest_context=M&title_suggest_tag=Art&title_suggest_tag=Comics__2.0
1404+
1405+ `geo ` context
1406+ +++++++++++++
1407+ Geo context allows to get suggestions within a certain distance from a
1408+ specified geo location.
1409+
1410+ In that case, the document definition should be altered as follows:
1411+
1412+ **Document definition **
1413+
1414+ .. code-block :: python
1415+
1416+ class AddressDocument (DocType ):
1417+
1418+ # ...
1419+
1420+ street = StringField(
1421+ analyzer = html_strip,
1422+ fields = {
1423+ ' raw' : KeywordField(),
1424+ ' suggest' : fields.CompletionField(),
1425+ ' suggest_context' : fields.CompletionField(
1426+ contexts = [
1427+ {
1428+ " name" : " loc" ,
1429+ " type" : " geo" ,
1430+ " path" : " location" ,
1431+ " precision" : " 100km" ,
1432+ },
1433+ ]
1434+ ),
1435+ }
1436+ )
1437+
1438+ location = fields.GeoPointField(
1439+ attr = ' location_field_indexing' ,
1440+ )
1441+
1442+ # ...
1443+
1444+ ViewSet should altered as follows:
1445+
1446+ **ViewSet definition **
1447+
1448+ .. code-block :: python
1449+
1450+ class BookFrontendDocumentViewSet (DocumentViewSet ):
1451+
1452+ # ...
1453+
1454+ # Suggester fields
1455+ suggester_fields = {
1456+ ' street_suggest_context' : {
1457+ ' field' : ' street.suggest_context' ,
1458+ ' default_suggester' : SUGGESTER_COMPLETION ,
1459+ # We want to be able to filter the completion filter
1460+ # results on the following params: tag, state and publisher.
1461+ # We also want to provide the size value.
1462+ # See the "https://www.elastic.co/guide/en/elasticsearch/
1463+ # reference/6.1/suggester-context.html" for the reference.
1464+ ' completion_options' : {
1465+ ' geo_filters' : {
1466+ ' title_suggest_loc' : ' loc' ,
1467+ },
1468+ ' size' : 10 ,
1469+ }
1470+ },
1471+ }
1472+
1473+ # ...
1474+
1475+ And finally we can narrow our suggestions as follows:
1476+
1477+ **Sample request **
1478+
1479+ In the example below we have filtered suggestions within 8000km distance
1480+ from geo-point (-30, -100).
1481+
1482+ .. code-block :: text
1483+
1484+ GET http://localhost:8000/search/addresses-frontend/suggest/?street_suggest_context=L&title_suggest_loc=-30__-100__8000km
1485+
1486+ Same query with boosting as well (boost value 2.0):
1487+
1488+ .. code-block :: text
1489+
1490+ GET http://localhost:8000/search/addresses-frontend/suggest/?street_suggest_context=L&title_suggest_loc=-30__-100__8000km__2.0
1491+
13061492 Term and Phrase suggestions
13071493~~~~~~~~~~~~~~~~~~~~~~~~~~~
13081494While for the ``completion `` suggesters to work the ``CompletionField `` shall
0 commit comments