我是 PostgreSql 的新手,我有一個如下表,

我需要使用 ts_vector 函式將輸出作為 text_col 中給定單詞(測驗)的計數,如下表所示,請給我一些建議,謝謝!

uj5u.com熱心網友回復:
計算空間呢?
下面取原始字串的長度,計算去掉空格的字串長度,然后求和 1
with first_sel as (
select 'Ciao io sono Ugo' mytext union all
select 'Ciao io non sono Ugo'
)
select mytext, length(mytext) - length(replace(mytext, ' ','')) 1 nr_words from first_sel;
結果
mytext | nr_words
---------------------- ----------
Ciao io sono Ugo | 4
Ciao io non sono Ugo | 5
(2 rows)
如果不僅僅是空格,您可以嘗試使用regex_replace洗掉所需的字符
uj5u.com熱心網友回復:
SELECT filename, cardinality(string_to_array(text_col,'test'))-1
FROM documents;
小提琴手
uj5u.com熱心網友回復:
使用 tsvector 就像(全域)
select * from ts_stat('select to_tsvector(text_col) from document_table')
where word='test';
其中 ndoc 是行數(檔案)和 nentry 單詞的出現次數
或按行
select file_name, text_col, to_tsvector(text_col),
substring(to_tsvector(text_col)::text, '''test'':(\d (,\d )*)') positions_in_text,
length(regexp_replace(substring(to_tsvector(text_col)::text, '''test'':(\d (,\d )*)'), '[^,]', '', 'g')) 1 occurrences
from document_table
where text_col @@ to_tsquery('test');
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/504032.html
標籤:数据库 PostgreSQL
