@@ -1507,6 +1507,123 @@ Phrase
15071507 }
15081508 }
15091509
1510+ Highlighting
1511+ ------------
1512+ Highlighters enable you to get highlighted snippets from one or more fields
1513+ in your search results so you can show users where the query matches are.
1514+
1515+ **ViewSet definition **
1516+
1517+ .. code-block :: python
1518+
1519+ from django_elasticsearch_dsl_drf.views import BaseDocumentViewSet
1520+ from django_elasticsearch_dsl_drf.filter_backends import (
1521+ # ...
1522+ HighlightBackend,
1523+ )
1524+
1525+ from ..documents import BookDocument
1526+ from ..serializers import BookDocumentSimpleSerializer
1527+
1528+
1529+ class BookDocumentViewSet (BaseDocumentViewSet ):
1530+ """ The BookDocument view."""
1531+
1532+ document = BookDocument
1533+ # serializer_class = BookDocumentSerializer
1534+ serializer_class = BookDocumentSimpleSerializer
1535+ lookup_field = ' id'
1536+ filter_backends = [
1537+ # ...
1538+ HighlightBackend,
1539+ ]
1540+
1541+ # ...
1542+
1543+ # Define highlight fields
1544+ highlight_fields = {
1545+ ' title' : {
1546+ ' enabled' : True ,
1547+ ' options' : {
1548+ ' pre_tags' : [" <b>" ],
1549+ ' post_tags' : [" </b>" ],
1550+ }
1551+ },
1552+ ' summary' : {
1553+ ' options' : {
1554+ ' fragment_size' : 50 ,
1555+ ' number_of_fragments' : 3
1556+ }
1557+ },
1558+ ' description' : {},
1559+ }
1560+
1561+ # ...
1562+
1563+ **Request **
1564+
1565+ .. code-block :: text
1566+
1567+ GET http://127.0.0.1:8000/search/books/?search=optimisation&highlight=title&highlight=summary
1568+
1569+ **Response **
1570+
1571+ .. code-block :: javascript
1572+
1573+ {
1574+ " count" : 1 ,
1575+ " next" : null ,
1576+ " previous" : null ,
1577+ " facets" : {
1578+ " _filter_publisher" : {
1579+ " publisher" : {
1580+ " buckets" : [
1581+ {
1582+ " key" : " GWW" ,
1583+ " doc_count" : 1
1584+ }
1585+ ],
1586+ " doc_count_error_upper_bound" : 0 ,
1587+ " sum_other_doc_count" : 0
1588+ },
1589+ " doc_count" : 1
1590+ }
1591+ },
1592+ " results" : [
1593+ {
1594+ " id" : 999999 ,
1595+ " title" : " Performance optimisation" ,
1596+ " description" : null ,
1597+ " summary" : " Ad animi adipisci libero facilis iure totam
1598+ impedit. Facilis maiores quae qui magnam dolores.
1599+ Veritatis quia amet porro voluptates iure quod
1600+ impedit. Dolor voluptatibus maiores at libero
1601+ magnam." ,
1602+ " authors" : [
1603+ " Artur Barseghyan"
1604+ ],
1605+ " publisher" : " Self published" ,
1606+ " publication_date" : " 1981-04-29" ,
1607+ " state" : " cancelled" ,
1608+ " isbn" : " 978-1-7372176-0-2" ,
1609+ " price" : 40.51 ,
1610+ " pages" : 162 ,
1611+ " stock_count" : 30 ,
1612+ " tags" : [
1613+ " Guide" ,
1614+ " Poetry" ,
1615+ " Fantasy"
1616+ ],
1617+ " highlight" : {
1618+ " title" : [
1619+ " Performance <b>optimisation</b>"
1620+ ]
1621+ },
1622+ " null_field" : null
1623+ }
1624+ ]
1625+ }
1626+
15101627 Pagination
15111628----------
15121629
0 commit comments