Skip to content

Instantly share code, notes, and snippets.

@leafo
Last active December 28, 2015 02:19
Show Gist options
  • Save leafo/7427317 to your computer and use it in GitHub Desktop.
Save leafo/7427317 to your computer and use it in GitHub Desktop.
postgres function to update game full text search table
create or replace function update_game_search(_game_id integer) returns text as $$
declare
game record;
game_words tsvector;
game_facets tsvector;
index_lang regconfig := 'english';
begin
select title, short_text, p_windows, p_linux, p_osx, deleted, published,
min_price, genre, "type"
into game
from games where id = _game_id;
if game.deleted or not game.published then
delete from games_search where game_id = _game_id;
return 'delete';
end if;
game_words := to_tsvector(index_lang, game.title || ' ' || COALESCE(game.short_text, ''));
game_facets := to_tsvector('simple', (case
when game.p_windows then 'p.windows '
else '' end) ||
(case
when game.p_linux then 'p.linux '
else '' end) ||
(case
when game.p_osx then 'p.osx '
else '' end) ||
(case
when game.min_price > 0 then 'm.paid '
else 'm.free ' end) ||
(case
when game."type" = 1 then 't.download '
else 't.web ' end) ||
(case
when game.genre is not null then ('g.' || game.genre)
else '' end));
if exists(select 1 from games_search where game_id = _game_id) then
update games_search set words = game_words, facets = game_facets
where game_id = _game_id;
return 'update';
else
insert into games_search (game_id, words, facets)
values (_game_id, game_words, game_facets);
return 'insert';
end if;
end;
$$ language plpgsql;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment