summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorHannes Ljungberg <hannes@5monkeys.se>2019-11-19 14:59:06 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-03-03 15:39:52 +0100
commit65ab4f9f03e70733df6afd9d8454ec3700155111 (patch)
tree4c296df9faab1f796ca22ea10a892d93baa27774 /docs
parentaee0bebc2faf9c6de8211b05d5f1281dc016084f (diff)
Fixed #31147 -- Added SearchHeadline to django.contrib.postgres.
Diffstat (limited to 'docs')
-rw-r--r--docs/ref/contrib/postgres/search.txt54
-rw-r--r--docs/releases/3.1.txt3
2 files changed, 57 insertions, 0 deletions
diff --git a/docs/ref/contrib/postgres/search.txt b/docs/ref/contrib/postgres/search.txt
index 813a3db57a..949d95929e 100644
--- a/docs/ref/contrib/postgres/search.txt
+++ b/docs/ref/contrib/postgres/search.txt
@@ -132,6 +132,60 @@ order by relevancy::
See :ref:`postgresql-fts-weighting-queries` for an explanation of the
``weights`` parameter.
+``SearchHeadline``
+==================
+
+.. versionadded:: 3.1
+
+.. class:: SearchHeadline(expression, query, config=None, start_sel=None, stop_sel=None, max_words=None, min_words=None, short_word=None, highlight_all=None, max_fragments=None, fragment_delimiter=None)
+
+Accepts a single text field or an expression, a query, a config, and a set of
+options. Returns highlighted search results.
+
+Set the ``start_sel`` and ``stop_sel`` parameters to the string values to be
+used to wrap highlighted query terms in the document. PostgreSQL's defaults are
+``<b>`` and ``</b>``.
+
+Provide integer values to the ``max_words`` and ``min_words`` parameters to
+determine the longest and shortest headlines. PostgreSQL's defaults are 35 and
+15.
+
+Provide an integer value to the ``short_word`` parameter to discard words of
+this length or less in each headline. PostgreSQL's default is 3.
+
+Set the ``highlight_all`` parameter to ``True`` to use the whole document in
+place of a fragment and ignore ``max_words``, ``min_words``, and ``short_word``
+parameters. That's disabled by default in PostgreSQL.
+
+Provide a non-zero integer value to the ``max_fragments`` to set the maximum
+number of fragments to display. That's disabled by default in PostgreSQL.
+
+Set the ``fragment_delimiter`` string parameter to configure the delimiter
+between fragments. PostgreSQL's default is ``" ... "``.
+
+The PostgreSQL documentation has more details on `highlighting search
+results`_.
+
+Usage example::
+
+ >>> from django.contrib.postgres.search import SearchHeadline, SearchQuery
+ >>> query = SearchQuery('red tomato')
+ >>> entry = Entry.objects.annotate(
+ ... headline=SearchHeadline(
+ ... 'body_text',
+ ... query,
+ ... start_sel='<span>',
+ ... stop_sel='</span>',
+ ... ),
+ ... ).get()
+ >>> print(entry.headline)
+ Sandwich with <span>tomato</span> and <span>red</span> cheese.
+
+See :ref:`postgresql-fts-search-configuration` for an explanation of the
+``config`` parameter.
+
+.. _highlighting search results: https://www.postgresql.org/docs/current/textsearch-controls.html#TEXTSEARCH-HEADLINE
+
.. _postgresql-fts-search-configuration:
Changing the search configuration
diff --git a/docs/releases/3.1.txt b/docs/releases/3.1.txt
index 669f2ca01e..b1ae1134cb 100644
--- a/docs/releases/3.1.txt
+++ b/docs/releases/3.1.txt
@@ -108,6 +108,9 @@ Minor features
* :class:`~django.contrib.postgres.search.SearchQuery` now supports
``'websearch'`` search type on PostgreSQL 11+.
+* The new :class:`~django.contrib.postgres.search.SearchHeadline` class allows
+ highlighting search results.
+
:mod:`django.contrib.redirects`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~