James R
James R

Reputation: 4656

postgres full text search word count

I've been messing around with full text search in postgres, and I was wondering, is it possible to return total word counts of all rows?

So, let's say you have

 text_col
 _______
 'dog'
 'dog cat'
 'dog bird dog'

the count of 'dog' should be four, the count of 'cat' should be one and bird should also be one.

Right now I have all the tsvectors saved to a gin indexed column as well.

Of course this would be across all rows where you could say something like

select max(ts_count(text_col_tsvector)) from mytable;

(I made that up, but I hope you get the general idea)

is it only possible to return the count of the lexeme, and if so, how does one return the dictionary (or array) of the lexeme returned.

Upvotes: 2

Views: 3522

Answers (1)

maniek
maniek

Reputation: 7307

how about:

select * from ts_stat('select text_col_tsvector from mytable')

Edit: You mean:

with words as (
select regexp_split_to_table(text_column , E'\\W+') as word
from mytable
)
select word, count(*) as cnt from words group by 1 order by 2 desc 

?

Upvotes: 3

Related Questions