I am new to PostgreSql, I am having a table like below,
I need to get output as count of the given word (test) in text_col like below table using ts_vector function,
Please give me some suggestion, Thanks !.
The following takes the length of the original string, calculates the length of the string removing the spaces and then sums 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;
result
mytext | nr_words
----------------------+----------
Ciao io sono Ugo | 4
Ciao io non sono Ugo | 5
(2 rows)
If it’s not only about spaces you can try removing the needed characters with regex_replace
3
Answers
What about counting spaces?
The following takes the length of the original string, calculates the length of the string removing the spaces and then sums 1
result
If it’s not only about spaces you can try removing the needed characters with regex_replace
dbfiddle
Using tsvector is like (globally)
where ndoc is number of rows (documents) and nentry number of occurrences of the word
Or by row