Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fLKEg-0000iU-Qi for pgsql-general@arkaria.postgresql.org; Wed, 23 May 2018 03:14:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fLKEd-00012p-9U for pgsql-general@arkaria.postgresql.org; Wed, 23 May 2018 03:14:43 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fLKEc-00012d-Tu for pgsql-general@lists.postgresql.org; Wed, 23 May 2018 03:14:43 +0000 Received: from smtp.emerald.synacor.com ([208.47.184.7] helo=smtp01.emerald.cmh.synacor.com) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fLKET-00049w-4n for pgsql-general@postgresql.org; Wed, 23 May 2018 03:14:39 +0000 X_CMAE_Category: , , X-CNFS-Analysis: v=2.2 cv=cfeQlTLM c=1 sm=1 tr=0 a=TO0uZPKBNpYfnMTqd/+/fg==:117 a=TO0uZPKBNpYfnMTqd/+/fg==:17 a=KGjhK52YXX0A:10 a=IkcTkHD0fZMA:10 a=d0d0vjhDbBEA:10 a=VUJBJC2UJ8kA:10 a=qzyW-89uDWQA:10 a=m7mnwo8xotJJKrGeF-cA:9 a=QEXdDO2ut3YA:10 X-CM-Score: 0 X-Scanned-by: Cloudmark Authority Engine X-Authed-Username: YnRvYmVyQGJyb2Fkc3RyaXBlLm5ldA== X_CMAE_Category: , , X-CNFS-Analysis: X-CM-Score: X-Scanned-by: Cloudmark Authority Engine Authentication-Results: smtp01.emerald.cmh.synacor.com smtp.mail=btober@computer.org; spf=softfail; sender-id=softfail Authentication-Results: smtp01.emerald.cmh.synacor.com header.from=btober@computer.org; sender-id=softfail Authentication-Results: smtp01.emerald.cmh.synacor.com smtp.user=btober; auth=pass (LOGIN) Received-SPF: softfail (smtp01.emerald.cmh.synacor.com: transitional domain computer.org does not designate 24.35.114.223 as permitted sender) Received: from [24.35.114.223] ([24.35.114.223:7004] helo=laptop.btober.net) by smtp.broadstripe.emerald.synacor.com (envelope-from ) (ecelerity 3.6.25.56547 r(Core:3.6.25.0)) with ESMTPSA (cipher=DHE-RSA-AES128-SHA) id 61/5C-32692-69CD40B5; Tue, 22 May 2018 23:14:30 -0400 To: Postgres General From: Berend Tober Subject: Per-document document statistics Message-ID: Date: Tue, 22 May 2018 23:14:20 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:49.0) Gecko/20100101 Firefox/49.0 SeaMonkey/2.46 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk I'm trying to figure out how to compile text search statistics on a per-document basis. While I successfully compute text search statistics for the entire corpus with a call to ts_stat after having inserted all documents, what I also want is to run ts_stat on the tsvector for each row so as to get the term frequency per document. Sample code and comments follow. -- Dumped from database version 9.5.7 CREATE DATABASE nlp; \connect nlp CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog; SET search_path = public, pg_catalog; -- This table stores one document per row CREATE TABLE document ( document_id serial primary key, content text NOT NULL, document_vector tsvector ); -- This is the table I need help with how to to populate -- with term frequency per document CREATE TABLE document_statistics ( document_id integer primary key, word text, ndoc bigint, /* this will be one, since there is only one document */ nentry bigint /* this is the number of interest */ ); ALTER TABLE ONLY document_statistics ADD CONSTRAINT document_statistics_document_id_fkey FOREIGN KEY (document_id) REFERENCES document(document_id); CREATE FUNCTION document_bit() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN -- Compile document statistics for each document upon insert SELECT to_tsvector('simple', new.content) INTO new.document_vector; RETURN new; END; $$; CREATE TRIGGER document_bit BEFORE INSERT OR UPDATE ON document FOR EACH ROW EXECUTE PROCEDURE document_bit(); -- Sample data INSERT INTO document (content) VALUES ('Hello World!'); INSERT INTO document (content) VALUES ('The quick brown dog jumped over the lazy dog'); INSERT INTO document (content) VALUES ('One flew over the coo coo''s nest',); -- Once all the individual documents are inserted, then -- calculate overall corpus statistics insert into corpus_statistics select * from ts_stat('select document_vector from document'); -- I'm thinking something like this proposed after insert trigger -- is where I want to compute document statistics, but can't -- figure out how to make it work CREATE FUNCTION document_ait() RETURNS trigger LANGUAGE plpgsql AS $$ DECLARE word_stat record; BEGIN /* --Here's one pathetic try FOR word_stat IN select * from ts_stat('select * from ' || (new.document_vector)) LOOP RAISE NOTICE '%' , word_stat; INSERT INTO public.document_statistics( document_id, word, ndoc, nentry) VALUES (new.document_id, word_stat.word, word_stat.ndoc, word_stat.nentry); END LOOP; */ RETURN new; END; $$; CREATE TRIGGER document_ait AFTER INSERT ON document FOR EACH ROW EXECUTE PROCEDURE document_ait();