postgresql_16:3.9.4.4 Gathering Document Statistics
PostgreSQL 16 > The SQL Language > Full Text Search > Additional Features > Gathering Document Statistics
The function ts_stat is useful for checking your configuration and for finding stop-word candidates.
ts_stat(sqlquery text, weights text,
OUT word text, OUT ndoc integer,
OUT nentry integer) returns setof record
*sqlquery* is a text value containing an SQL query which must return a single tsvector column. ts_stat executes the query and returns statistics about each distinct lexeme (word) contained in the tsvector data. The columns returned are *word* text — the value of a lexeme *ndoc* integer — number of documents (tsvectors) the word occurred in *nentry* integer — total number of occurrences of the word If *weights* is supplied, only occurrences having one of those weights are counted.
For example, to find the ten most frequent words in a document collection: SELECT * FROM ts_stat('SELECT vector FROM apod') ORDER BY nentry DESC, ndoc DESC, word LIMIT 10; The same, but counting only word occurrences with weight A or B: SELECT * FROM ts_stat('SELECT vector FROM apod', 'ab') ORDER BY nentry DESC, ndoc DESC, word LIMIT 10;