Igo*_*rek 6 sql postgresql full-text-search tsvector
有没有办法获得有关句子中词汇位置和tsvector出现次数的信息?
像这样的东西
SELECT *
FROM get_position(to_tsvector('english', 'The Fat Rats'), to_tsquery('Rats'));
Run Code Online (Sandbox Code Playgroud)
将返回3
和
SELECT *
FROM get_occurrences(to_tsvector('english', 'The Fat Rats'), to_tsquery('Rats'));
Run Code Online (Sandbox Code Playgroud)
将返回1.
tsvector的文本表示包含特定词位的出现列表:
test=# select to_tsvector ( 'english', 'new bar in New York' );
to_tsvector
----------------------------
'bar':2 'new':1,4 'york':5
Run Code Online (Sandbox Code Playgroud)
以下是依赖于此的示例性功能.它接受文本参数并在内部将它们转换为ts_vector,但可以轻松地重写以接受ts_vector.
CREATE OR REPLACE FUNCTION lexeme_occurrences (
IN _document text
, IN _word text
, IN _config regconfig
, OUT lexeme_count int
, OUT lexeme_positions int[]
) RETURNS RECORD
AS $$
DECLARE
_lexemes tsvector := to_tsvector ( _config, _document );
_searched_lexeme tsvector := strip ( to_tsvector ( _config, _word ) );
_occurences_pattern text := _searched_lexeme::text || ':([0-9,]+)';
_occurences_list text := substring ( _lexemes::text, _occurences_pattern );
BEGIN
SELECT
count ( a )
, array_agg ( a::int )
FROM regexp_split_to_table ( _occurences_list, ',' ) a
WHERE _searched_lexeme::text != '' -- preventing false positives
INTO
lexeme_count
, lexeme_positions;
RETURN;
END $$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
用法示例:
select * from lexeme_occurrences ( 'The Fat Rats', 'rat', 'english' );
lexeme_count | lexeme_positions
--------------+-----------------
1 | {3}
(1 row)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
641 次 |
| 最近记录: |