本文介绍 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_sel
和 stop_sel
参数设置为用于在文档中包装突出显示的查询词的字符串值。 PostgreSQL 的默认值为 <b>
和 </b>
。
为 max_words
和 min_words
参数提供整数值以确定最长和最短的标题。 PostgreSQL 的默认值为 35 和 15。
为 short_word
参数提供一个整数值,以丢弃每个标题中此长度或更短的单词。 PostgreSQL 的默认值为 3。
将 highlight_all
参数设置为 True
以使用整个文档代替片段并忽略 max_words
、 min_words
和 short_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
参数的说明,请参阅更改搜索配置。
相关用法
- Python Django SearchVectorField用法及代码示例
- Python Django SearchQuery用法及代码示例
- Python Django SearchRank用法及代码示例
- Python Django SearchVector用法及代码示例
- Python Seaborn.barplot()用法及代码示例
- Python Tableau Server用法及代码示例
- Python Set issubset()用法及代码示例
- Python Set issuperset()用法及代码示例
- Python Pandas Series.cumsum()用法及代码示例
- Python Pandas Series.cov()用法及代码示例
- Python Set difference_update()用法及代码示例
- Python Django SelectDateWidget.months用法及代码示例
- Python Set union()用法及代码示例
- Python Set pop()用法及代码示例
- Python Django SelectDateWidget.empty_label用法及代码示例
- Python Pandas Series.astype()用法及代码示例
- Python Pandas Series.nonzero()用法及代码示例
- Python Pandas Series.cummin()用法及代码示例
- Python Set add()用法及代码示例
- Python Set clear()用法及代码示例
- Python Set isdisjoint()用法及代码示例
- Python Set intersection_update()用法及代码示例
- Python Set symmetric_difference()用法及代码示例
- Python Pandas Series.mad()用法及代码示例
- Python Set symmetric_difference_update()用法及代码示例
注:本文由纯净天空筛选整理自djangoproject.com大神的英文原创作品 django.contrib.postgres.search.SearchHeadline。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。