Skip to content

Instantly share code, notes, and snippets.

@requaos
Created March 28, 2019 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save requaos/fd3340acc6eaa52d9ffe1c10f42853a3 to your computer and use it in GitHub Desktop.
Save requaos/fd3340acc6eaa52d9ffe1c10f42853a3 to your computer and use it in GitHub Desktop.
START TRANSACTION;
ALTER TABLE public.stories
ADD COLUMN text_search tsvector;
UPDATE public.stories SET text_search =
setweight(to_tsvector(author), 'A') ||
setweight(to_tsvector(title), 'A') ||
setweight(to_tsvector(description), 'B');
CREATE INDEX index_stories_on_text_search ON public.stories USING GIN(text_search);
CREATE FUNCTION stories_search_trigger() RETURNS trigger AS $$
begin
new.text_search :=
setweight(to_tsvector(new.author), 'A') ||
setweight(to_tsvector(new.title), 'A') ||
setweight(to_tsvector(new.description), 'B');
return new;
end
$$ LANGUAGE plpgsql;
CREATE TRIGGER tsvectorupdate BEFORE INSERT OR UPDATE
ON public.stories FOR EACH ROW EXECUTE PROCEDURE
stories_search_trigger();
COMMIT TRANSACTION;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment