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_full_word(norm_term TEXT, lookup_terms TEXT[],
92 OUT partial_tokens INT[])
95 partial_terms TEXT[] = '{}'::TEXT[];
100 SELECT min(word_id) INTO full_token
101 FROM word WHERE word = norm_term and class is null and country_code is null;
103 IF full_token IS NULL THEN
104 full_token := nextval('seq_word');
105 INSERT INTO word (word_id, word_token, word, search_name_count)
106 SELECT full_token, ' ' || lookup_term, norm_term, 0 FROM unnest(lookup_terms) as lookup_term;
109 FOR term IN SELECT unnest(string_to_array(unnest(lookup_terms), ' ')) LOOP
111 IF NOT (ARRAY[term] <@ partial_terms) THEN
112 partial_terms := partial_terms || term;
116 partial_tokens := '{}'::INT[];
117 FOR term IN SELECT unnest(partial_terms) LOOP
118 SELECT min(word_id), max(search_name_count) INTO term_id, term_count
119 FROM word WHERE word_token = term and class is null and country_code is null;
121 IF term_id IS NULL THEN
122 term_id := nextval('seq_word');
124 INSERT INTO word (word_id, word_token, search_name_count)
125 VALUES (term_id, term, 0);
128 IF term_count < {{ max_word_freq }} THEN
129 partial_tokens := array_merge(partial_tokens, ARRAY[term_id]);
137 CREATE OR REPLACE FUNCTION getorcreate_hnr_id(lookup_term TEXT)
143 SELECT min(word_id) INTO return_id FROM word
144 WHERE word_token = lookup_term and type = 'H';
146 IF return_id IS NULL THEN
147 return_id := nextval('seq_word');
148 INSERT INTO word (word_id, word_token, type)
149 VALUES (return_id, lookup_term, 'H');