Commit 6e7ecd4c authored by Eduardo L. Buratti's avatar Eduardo L. Buratti

database: Add search_school query function

Signed-off-by: Eduardo L. Buratti's avatarEduardo L. Buratti <elb09@c3sl.ufpr.br>
parent c2f3abc7
CREATE OR REPLACE FUNCTION remove_accents(text) RETURNS text AS
$$
SELECT translate($1, 'áàâãäéèêëíìïóòôõöúùûüÁÀÂÃÄÉÈÊËÍÌÏÓÒÔÕÖÚÙÛÜçÇ',
'aaaaaeeeeiiiooooouuuuAAAAAEEEEIIIOOOOOUUUUcC');
$$
LANGUAGE SQL;
CREATE OR REPLACE FUNCTION search_school(query text) RETURNS
TABLE (id integer, name text, city text, region text, state text, city_raw text) AS $$
BEGIN
RETURN QUERY SELECT
sch.id,
INITCAP(ts_headline(sch.name, q)),
INITCAP(ts_headline(cit.name, q)),
cit.region::text,
cit.state::text,
cit.name::text
FROM search_school s, dim_school sch, dim_city cit,
to_tsquery(regexp_replace(remove_accents(query), E'[ \+]+', '|', 'g')) q
WHERE
s.contacted_once = true AND
sch.id = s.sch_id AND
cit.id = sch.cit_id AND
s.ts_vector @@ q
ORDER BY ts_rank(s.ts_vector, q) DESC;
END;
$$ language plpgsql;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment