当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python Django SearchHeadline用法及代码示例


本文介绍 django.contrib.postgres.search.SearchHeadline 的用法。

声明

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)

接受单个文本字段或表达式、查询、配置和一组选项。返回突出显示的搜索结果。

start_selstop_sel 参数设置为用于在文档中包装突出显示的查询词的字符串值。 PostgreSQL 的默认值为 <b></b>

max_wordsmin_words 参数提供整数值以确定最长和最短的标题。 PostgreSQL 的默认值为 35 和 15。

short_word 参数提供一个整数值,以丢弃每个标题中此长度或更短的单词。 PostgreSQL 的默认值为 3。

highlight_all 参数设置为 True 以使用整个文档代替片段并忽略 max_wordsmin_wordsshort_word 参数。这在 PostgreSQL 中默认是禁用的。

max_fragments 提供一个非零整数值以设置要显示的最大片段数。这在 PostgreSQL 中默认是禁用的。

设置fragment_delimiter 字符串参数以配置片段之间的分隔符。 PostgreSQL 的默认值为 " ... "

PostgreSQL 文档有更多关于 highlighting search results 的详细信息。

使用示例:

>>> 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.

有关 config 参数的说明,请参阅更改搜索配置。

相关用法


注:本文由纯净天空筛选整理自djangoproject.com大神的英文原创作品 django.contrib.postgres.search.SearchHeadline。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。