1 CREATE OR REPLACE FUNCTION public.create_plpgsql_language ()
4 CREATE LANGUAGE plpgsql;
5 SELECT 'language plpgsql created'::TEXT;
12 WHERE lanname='plpgsql')
14 (SELECT 'language already installed'::TEXT)
16 (SELECT public.create_plpgsql_language())
19 DROP FUNCTION public.create_plpgsql_language ();
21 CREATE OR REPLACE FUNCTION node_ranking(node_id int, srch text) RETURNS float AS $$
25 SELECT tsv INTO v FROM forum_node WHERE id = node_id;
26 RETURN ts_rank_cd(v || children_tsv(node_id), plainto_tsquery(srch), 32);
30 CREATE OR REPLACE FUNCTION children_tsv(id int) RETURNS tsvector AS $$
32 v tsvector := ''::tsvector;
35 FOR r IN SELECT * FROM forum_node WHERE parent_id = id LOOP
36 v := v || r.tsv || children_tsv(r.id);
42 CREATE OR REPLACE FUNCTION set_node_tsv() RETURNS TRIGGER AS $$
44 IF (tg_op = 'INSERT') THEN
46 setweight(to_tsvector('english', coalesce(new.tagnames,'')), 'A') ||
47 setweight(to_tsvector('english', coalesce(new.title,'')), 'B') ||
48 setweight(to_tsvector('english', coalesce(new.body,'')), 'C');
49 ELSIF (new.active_revision_id <> old.active_revision_id) OR (new.tsv IS NULL) THEN
51 setweight(to_tsvector('english', coalesce(new.tagnames,'')), 'A') ||
52 setweight(to_tsvector('english', coalesce(new.title,'')), 'B') ||
53 setweight(to_tsvector('english', coalesce(new.body,'')), 'C');
59 CREATE OR REPLACE FUNCTION public.create_tsv_node_column ()
62 ALTER TABLE forum_node ADD COLUMN tsv tsvector;
64 CREATE TRIGGER tsvectorupdate BEFORE INSERT OR UPDATE
65 ON forum_node FOR EACH ROW EXECUTE PROCEDURE set_node_tsv();
67 CREATE INDEX node_tsv ON forum_node USING gin(tsv);
69 SELECT 'tsv column created'::TEXT;
74 (SELECT true::BOOLEAN FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = 'forum_node') AND attname = 'tsv')
76 (SELECT 'Tsv column already exists'::TEXT)
78 (SELECT public.create_tsv_node_column())
82 DROP FUNCTION public.create_tsv_node_column ();
84 UPDATE forum_node SET id=id WHERE TRUE;