Debian Codegraph @

docs/04_highlighting.rst

raw
1 .. _highlighting-label:
2
3 Highlighting
4 ============
5
6 Haystack supports two kinds of `Highlighting <https://django-haystack.readthedocs.io/en/latest/highlighting.html>`_,
7 and we support them both.
8
9 #. SearchQuerySet highlighting. This kind of highlighting requires a search backend which has support for
10 highlighting, such as Elasticsearch or Solr.
11 #. Pure python highlighting. This implementation is somewhat slower, but enables highlighting support
12 even if your search backend does not support it.
13
14
15 .. note::
16
17 The highlighter will always use the ``document=True`` field on your index to hightlight on.
18 See examples below.
19
20 SearchQuerySet Highlighting
21 ---------------------------
22
23 In order to add support for ``SearchQuerySet().highlight()``, all you have to do is to add the
24 :class:`drf_haystack.filters.HaystackHighlightFilter` to the ``filter_backends`` in your view. The ``HaystackSerializer`` will
25 check if your queryset has highlighting enabled, and render an additional ``highlighted`` field to
26 your result. The highlighted words will be encapsulated in an ``<em>words go here</em>`` html tag.
27
28
29 **Example view with highlighting enabled**
30
31 .. code-block:: python
32
33 from drf_haystack.viewsets import HaystackViewSet
34 from drf_haystack.filters import HaystackHighlightFilter
35
36 from .models import Person
37 from .serializers import PersonSerializer
38
39
40 class SearchViewSet(HaystackViewSet):
41 index_models = [Person]
42 serializer_class = PersonSerializer
43 filter_backends = [HaystackHighlightFilter]
44
45
46 Given a query like below
47
48 .. code-block:: none
49
50 /api/v1/search/?firstname=jeremy
51
52
53 We would get a result like this
54
55 .. code-block:: json
56
57 [
58 {
59 "lastname": "Rowland",
60 "full_name": "Jeremy Rowland",
61 "firstname": "Jeremy",
62 "highlighted": "<em>Jeremy</em> Rowland\nCreated: May 19, 2015, 10:48 a.m.\nLast modified: May 19, 2015, 10:48 a.m.\n"
63 },
64 {
65 "lastname": "Fowler",
66 "full_name": "Jeremy Fowler",
67 "firstname": "Jeremy",
68 "highlighted": "<em>Jeremy</em> Fowler\nCreated: May 19, 2015, 10:48 a.m.\nLast modified: May 19, 2015, 10:48 a.m.\n"
69 }
70 ]
71
72
73
74 Pure Python Highlighting
75 ------------------------
76
77 This implementation make use of the haystack ``Highlighter()`` class.
78 It is implemented as :class:`drf_haystack.serializers.HighlighterMixin` mixin class, and must be applied on the ``Serializer``.
79 This is somewhat slower, but more configurable than the :class:`drf_haystack.filters.HaystackHighlightFilter` filter class.
80
81 The Highlighter class will be initialized with the following default options, but can be overridden by
82 changing any of the following class attributes.
83
84 .. code-block:: python
85
86 highlighter_class = Highlighter
87 highlighter_css_class = "highlighted"
88 highlighter_html_tag = "span"
89 highlighter_max_length = 200
90 highlighter_field = None
91
92 The Highlighter class will usually highlight the ``document_field`` (the field marked ``document=True`` on your
93 search index class), but this may be overridden by changing the ``highlighter_field``.
94
95 You can of course also use your own ``Highlighter`` class by overriding the ``highlighter_class = MyFancyHighLighter``
96 class attribute.
97
98
99 **Example serializer with highlighter support**
100
101 .. code-block:: python
102
103 from drf_haystack.serializers import HighlighterMixin, HaystackSerializer
104
105 class PersonSerializer(HighlighterMixin, HaystackSerializer):
106
107 highlighter_css_class = "my-highlighter-class"
108 highlighter_html_tag = "em"
109
110 class Meta:
111 index_classes = [PersonIndex]
112 fields = ["firstname", "lastname", "full_name"]
113
114
115 Response
116
117 .. code-block:: json
118
119 [
120 {
121 "full_name": "Jeremy Rowland",
122 "lastname": "Rowland",
123 "firstname": "Jeremy",
124 "highlighted": "<em class=\"my-highlighter-class\">Jeremy</em> Rowland\nCreated: May 19, 2015, 10:48 a.m.\nLast modified: May 19, 2015, 10:48 a.m.\n"
125 },
126 {
127 "full_name": "Jeremy Fowler",
128 "lastname": "Fowler",
129 "firstname": "Jeremy",
130 "highlighted": "<em class=\"my-highlighter-class\">Jeremy</em> Fowler\nCreated: May 19, 2015, 10:48 a.m.\nLast modified: May 19, 2015, 10:48 a.m.\n"
131 }
132 ]