Skip to content

Instantly share code, notes, and snippets.

@lenolib
Created December 16, 2020 09:50
Show Gist options
  • Save lenolib/cf2a65bc99ad639eb207edc73cd0bac6 to your computer and use it in GitHub Desktop.
Save lenolib/cf2a65bc99ad639eb207edc73cd0bac6 to your computer and use it in GitHub Desktop.
postgresql full text search schema
ALTER TABLE my_table ADD COLUMN tsdoc tsvector;
CREATE INDEX my_table_tsvec_idx ON my_table USING GIN (customer_id, tsdoc);
-- Full text search for datafield specs
CREATE FUNCTION my_table_tsvec_trigger() RETURNS trigger AS $$
BEGIN
NEW.tsdoc :=
setweight(to_tsvector('simple', coalesce(NEW.name,'')), 'A') ||
setweight(to_tsvector('simple', coalesce(NEW.description,'')), 'B');
return NEW;
END
$$ LANGUAGE plpgsql;
CREATE TRIGGER my_table_tsvec_update BEFORE INSERT OR UPDATE
ON my_table FOR EACH ROW EXECUTE PROCEDURE my_table_tsvec_trigger();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment