高亮搜索结果

搜索结果的理想显示是:列出每篇文档中与搜索相关的部分,并标识为什么与查询相关。搜索引擎能够显示标识了搜索词的文档片段。openGauss提供了函数ts_headline支持这部分功能。

ts_headline([ config regconfig, ] document text, query tsquery [, options text ]) returns text

ts_headline的输入是带有查询条件的文档,其返回文档中的摘录,在摘录中查询词是高亮显示的。用来解析文档的分词器由config参数指定。如果省略config,则使用default_text_search_config的值所指定的分词器。

指定options字符串时,需由一个或多个option=value对组成,且必须用逗号分隔。options可以是下面的选项:

  • StartSel,StopSel:分隔文档中出现的查询词,以区别于其他摘录词。当包含有空格或逗号时,必须用双引号将字符串引起来。

  • MaxWords,MinWords:定义摘录的最长和最短值。

  • ShortWord:在摘录的开始和结束会丢弃此长度或更短的词。默认值3会消除常见的英语冠词。

  • HighlightAll:布尔标志。如果为真,整个文档将作为摘录。忽略前面三个参数的值。

  • MaxFragments:要显示的文本摘录或片段的最大数量。默认值0表示选择非片段的摘录生成方法。 大于0的值表示选择基于片段的摘录生成。此方法查找带有尽可能多查询词的文本片段,并显示查询词周围的上下文片段。因此,查询词临近每个片段的中间,且查询词两边都有词。每个片段至多有MaxWords,并且长度为ShortWord或更短的词在每一个片段开始和结束被丢弃。如果在文档中没有找到所有的查询词,则文档中开头将显示MinWords单片段。

  • FragmentDelimiter:当有一个以上的片段时,通过该字符串分隔这些片段。

不声明选项时,采用下面的缺省值:

StartSel=<b>, StopSel=</b>,
MaxWords=35, MinWords=15, ShortWord=3, HighlightAll=FALSE,
MaxFragments=0, FragmentDelimiter=" ... "

例如:

postgres=# SELECT ts_headline('english',
'The most common type of search
is to find all documents containing given query terms
and return them in order of their similarity to the
query.',
to_tsquery('english', 'query & similarity'));
                        ts_headline                         
------------------------------------------------------------
 containing given <b>query</b> terms
 and return them in order of their <b>similarity</b> to the
 <b>query</b>.
(1 row)

postgres=# SELECT ts_headline('english',
'The most common type of search
is to find all documents containing given query terms
and return them in order of their similarity to the
query.',
to_tsquery('english', 'query & similarity'),
'StartSel = <, StopSel = >');
                      ts_headline                      
-------------------------------------------------------
 containing given <query> terms
 and return them in order of their <similarity> to the
 <query>.
(1 row)

ts_headline使用原始文档,而不是tsvector摘录,因此使用起来会慢,应慎重使用。

意见反馈
编组 3备份
openGauss 2024-03-19 00:45:43
取消