Skip to content

Instantly share code, notes, and snippets.

@phillip-haydon
Created June 19, 2016 05:05
Show Gist options
  • Save phillip-haydon/167e490778b653ea1a2349d83f33daad to your computer and use it in GitHub Desktop.
Save phillip-haydon/167e490778b653ea1a2349d83f33daad to your computer and use it in GitHub Desktop.
Example trigger to update doc into tsvestor
drop table if exists searchable_table_test;
create table if not exists searchable_table_test (
id serial primary key not null,
data jsonb not null,
searchable tsvector null
);
create or replace function make_doc_searchable() returns trigger as $make_doc_searchable$
begin
NEW.searchable := to_tsvector(NEW.data ->> 'Name') ||
to_tsvector(NEW.data ->> 'Address' ||
to_tsvector(NEW.data ->> 'Tags'));
return NEW;
end;
$make_doc_searchable$ language plpgsql;
drop trigger if exists searchable_doc_trigger on searchable_table_test;
create trigger searchable_doc_trigger before insert or update on searchable_table_test
for each row execute procedure make_doc_searchable();
insert into searchable_table_test (data) values ('{"Name": "Phillip Haydon", "Address": "Somewhere in Singapore", "Tags": ["One", "Two", "Three"]}');
select * from searchable_table_test;
update searchable_table_test set data = '{"Name": "Prentice Porter", "Address": "Somewhere in Singapore", "Tags": ["One", "Two", "Three"]}' where id = 1;
select * from searchable_table_test;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment