1 -- Get tokens used for searching the given place.
3 -- These are the tokens that will be saved in the search_name table.
4 CREATE OR REPLACE FUNCTION token_get_name_search_tokens(info JSONB)
7 SELECT (info->>'names')::INTEGER[]
8 $$ LANGUAGE SQL IMMUTABLE STRICT;
11 -- Get tokens for matching the place name against others.
13 -- This should usually be restricted to full name tokens.
14 CREATE OR REPLACE FUNCTION token_get_name_match_tokens(info JSONB)
17 SELECT (info->>'names')::INTEGER[]
18 $$ LANGUAGE SQL IMMUTABLE STRICT;
21 -- Return the housenumber tokens applicable for the place.
22 CREATE OR REPLACE FUNCTION token_get_housenumber_search_tokens(info JSONB)
25 SELECT (info->>'hnr_tokens')::INTEGER[]
26 $$ LANGUAGE SQL IMMUTABLE STRICT;
29 -- Return the housenumber in the form that it can be matched during search.
30 CREATE OR REPLACE FUNCTION token_normalized_housenumber(info JSONB)
34 $$ LANGUAGE SQL IMMUTABLE STRICT;
37 CREATE OR REPLACE FUNCTION token_addr_street_match_tokens(info JSONB)
40 SELECT (info->>'street')::INTEGER[]
41 $$ LANGUAGE SQL IMMUTABLE STRICT;
44 CREATE OR REPLACE FUNCTION token_addr_place_match_tokens(info JSONB)
47 SELECT (info->>'place_match')::INTEGER[]
48 $$ LANGUAGE SQL IMMUTABLE STRICT;
51 CREATE OR REPLACE FUNCTION token_addr_place_search_tokens(info JSONB)
54 SELECT (info->>'place_search')::INTEGER[]
55 $$ LANGUAGE SQL IMMUTABLE STRICT;
58 DROP TYPE IF EXISTS token_addresstoken CASCADE;
59 CREATE TYPE token_addresstoken AS (
65 CREATE OR REPLACE FUNCTION token_get_address_tokens(info JSONB)
66 RETURNS SETOF token_addresstoken
68 SELECT key, (value->>1)::int[] as match_tokens,
69 (value->>0)::int[] as search_tokens
70 FROM jsonb_each(info->'addr');
71 $$ LANGUAGE SQL IMMUTABLE STRICT;
74 CREATE OR REPLACE FUNCTION token_normalized_postcode(postcode TEXT)
77 SELECT CASE WHEN postcode SIMILAR TO '%(,|;)%' THEN NULL ELSE upper(trim(postcode))END;
78 $$ LANGUAGE SQL IMMUTABLE STRICT;
81 -- Return token info that should be saved permanently in the database.
82 CREATE OR REPLACE FUNCTION token_strip_info(info JSONB)
86 $$ LANGUAGE SQL IMMUTABLE STRICT;
88 --------------- private functions ----------------------------------------------
90 CREATE OR REPLACE FUNCTION getorcreate_term_id(lookup_term TEXT)
97 SELECT min(word_id), max(search_name_count) INTO return_id, term_count
98 FROM word WHERE word_token = lookup_term and class is null and type is null;
100 IF return_id IS NULL THEN
101 return_id := nextval('seq_word');
102 INSERT INTO word (word_id, word_token, search_name_count)
103 VALUES (return_id, lookup_term, 0);
104 ELSEIF left(lookup_term, 1) = ' ' and term_count > {{ max_word_freq }} THEN
114 CREATE OR REPLACE FUNCTION getorcreate_hnr_id(lookup_term TEXT)
120 SELECT min(word_id) INTO return_id
122 WHERE word_token = ' ' || lookup_term
123 and class = 'place' and type = 'house';
125 IF return_id IS NULL THEN
126 return_id := nextval('seq_word');
127 INSERT INTO word (word_id, word_token, class, type, search_name_count)
128 VALUES (return_id, ' ' || lookup_term, 'place', 'house', 0);