首页 > 解决方案 > 是否有一个 ts(文本搜索)函数会返回找到的字符串而不是布尔值?

问题描述

我正在使用 PostgreSQL 通过使用tsvectorand来查找文章中匹配的字符串tsquery

我阅读了 PostgreSQL 手册 12.3 Controlling Text Search,但没有什么能帮助我获得我想要的确切输出。

询问:

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('query & similarity'),
  'StartSel = <, StopSel = >');

ts_headline 输出

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

我正在寻找下面提到的唯一字符串:

查询,相似度

标签: postgresqlfull-text-searchtsvector

解决方案


如果您为 StartSel 和 StopSel 选择了您确定在字符串的其他位置不存在的分隔符,那么使用regexp很容易做到这一点。

 SELECT  distinct regexp_matches[1] from
     regexp_matches(
        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('query & similarity'),
            'StartSel = <, StopSel = >'
         ),
         '<(.*?)>','g'
     );

推荐阅读