CREATE FUNCTION language_regconfig(language VARCHAR)
RETURNS regconfig
AS $$
SELECT CASE
WHEN $1 = 'de' THEN 'german'
WHEN $1 = 'en' THEN 'english'
WHEN $1 = 'fr' THEN 'french'
ELSE 'simple'
END :: regconfig
$$ IMMUTABLE LANGUAGE SQL;
CREATE INDEX bookmark_texts_search_idx ON benki.bookmark_texts
USING GIN (to_tsvector(language_regconfig(language), title || ' ' || description));
CREATE INDEX lazychat_message_texts_search_idx ON benki.lazychat_message_texts
USING GIN (to_tsvector(language_regconfig(language), content));
CREATE FUNCTION post_matches_websearch(
post_language TEXT,
post_text TEXT,
websearch_language TEXT,
websearch_text TEXT)
RETURNS BOOLEAN
AS $$
SELECT to_tsvector(language_regconfig($1), $2) @@ websearch_to_tsquery(language_regconfig($3), $4)
$$ LANGUAGE SQL IMMUTABLE;
CREATE FUNCTION post_matches_websearch(
post_tsvector tsvector,
websearch_language TEXT,
websearch_text TEXT)
RETURNS BOOLEAN
AS $$
SELECT $1 @@ websearch_to_tsquery(language_regconfig($2), $3)
$$ LANGUAGE SQL IMMUTABLE;
CREATE INDEX bookmark_texts_search_terms_idx ON benki.bookmark_texts
USING GIN (search_terms);
CREATE INDEX lazychat_message_texts_search_terms_idx ON benki.lazychat_message_texts
USING GIN (search_terms);
CREATE FUNCTION bookmark_search_term_update_trigger()
RETURNS trigger
AS $$
BEGIN
NEW.search_terms :=
setweight(to_tsvector(language_regconfig(NEW.language), coalesce(NEW.title, '')), 'A') ||
setweight(to_tsvector(language_regconfig(NEW.language), coalesce(NEW.description, '')), 'B');
RETURN NEW;
END
$$ LANGUAGE plpgsql;
CREATE FUNCTION lazychat_message_search_term_update_trigger()
RETURNS trigger
AS $$
BEGIN
NEW.search_terms :=
to_tsvector(language_regconfig(NEW.language), coalesce(NEW.content,''));
RETURN NEW;
END
$$ LANGUAGE plpgsql;
CREATE TRIGGER bookmark_search_term_update
BEFORE INSERT OR UPDATE
ON benki.bookmark_texts
FOR EACH ROW
EXECUTE FUNCTION
bookmark_search_term_update_trigger();
CREATE TRIGGER lazychat_message_search_term_update
BEFORE INSERT OR UPDATE
ON benki.lazychat_message_texts
FOR EACH ROW
EXECUTE FUNCTION
lazychat_message_search_term_update_trigger();
UPDATE benki.bookmark_texts
SET search_terms =
setweight(to_tsvector(language_regconfig(language), coalesce(title, '')), 'A') ||
setweight(to_tsvector(language_regconfig(language), coalesce(description, '')), 'B');
UPDATE benki.lazychat_message_texts
SET search_terms =
to_tsvector(language_regconfig(language), coalesce(content,''));