获取比赛开始位置。文字中

问题描述 投票:0回答:1

当我使用 Postgres 的全文搜索 (FTS) 检查匹配项时,我想知道是否有办法找出匹配项在文本中发生的位置?是否可以获得起始位置和结束位置?

例如,这是一个简单的匹配:

select to_tsvector('simple', 'Free text seaRCh is a wONderful Thing') @@ phraseto_tsquery('simple', 'wonderful thing');

使用

ts_headline
我可以突出显示匹配发生的位置:

select ts_headline('Free text seaRCh is a wonderful Thing',
        phraseto_tsquery('simple', 'wonderful thing'));

产生:

                     ts_headline
═════════════════════════════════════════════════════
 Free text seaRCh is a <b>wonderful</b> <b>Thing</b>
(1 row)

所以,这个函数知道比赛的开始和结束位置。有没有办法提取两个位置?

postgresql full-text-search
1个回答
0
投票

使用 FTS 中的 tsvector、tsquery 和 ts_stat 函数,您可以提取文档中匹配术语的位置。试试这个代码;

-- Create a tsvector from the document
SELECT to_tsvector('simple', 'Free text seaRCh is a wonderful Thing') AS doc_vector;

-- Create a tsquery from the query
SELECT phraseto_tsquery('simple', 'wonderful thing') AS query_tsquery;

-- Use the @@ operator to check if the query matches the document
SELECT to_tsvector('simple', 'Free text seaRCh is a wonderful Thing')
    @@ phraseto_tsquery('simple', 'wonderful thing') AS is_match;

-- Use ts_stat to get the positions of the matched terms
SELECT ts_stat('simple',
               to_tsvector('simple', 'Free text seaRCh is a wonderful Thing'),
               phraseto_tsquery('simple', 'wonderful thing')
              ) AS term_positions;

ts_stat 函数为查询中的每个术语返回一行。每行都提供了有关该术语的信息,包括它在文档中出现的位置。

希望它能解决您的问题:)

© www.soinside.com 2019 - 2024. All rights reserved.