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_has_addr_street(info JSONB)
40 SELECT info->>'street' is not null;
41 $$ LANGUAGE SQL IMMUTABLE;
44 CREATE OR REPLACE FUNCTION token_has_addr_place(info JSONB)
47 SELECT info->>'place' is not null;
48 $$ LANGUAGE SQL IMMUTABLE;
51 CREATE OR REPLACE FUNCTION token_matches_street(info JSONB, street_tokens INTEGER[])
54 SELECT (info->>'street')::INTEGER[] <@ street_tokens
55 $$ LANGUAGE SQL IMMUTABLE STRICT;
58 CREATE OR REPLACE FUNCTION token_matches_place(info JSONB, place_tokens INTEGER[])
61 SELECT (info->>'place')::INTEGER[] <@ place_tokens
62 $$ LANGUAGE SQL IMMUTABLE STRICT;
65 CREATE OR REPLACE FUNCTION token_addr_place_search_tokens(info JSONB)
68 SELECT (info->>'place')::INTEGER[]
69 $$ LANGUAGE SQL IMMUTABLE STRICT;
72 CREATE OR REPLACE FUNCTION token_get_address_keys(info JSONB)
75 SELECT * FROM jsonb_object_keys(info->'addr');
76 $$ LANGUAGE SQL IMMUTABLE STRICT;
79 CREATE OR REPLACE FUNCTION token_get_address_search_tokens(info JSONB, key TEXT)
82 SELECT (info->'addr'->>key)::INTEGER[];
83 $$ LANGUAGE SQL IMMUTABLE STRICT;
86 CREATE OR REPLACE FUNCTION token_matches_address(info JSONB, key TEXT, tokens INTEGER[])
89 SELECT (info->'addr'->>key)::INTEGER[] <@ tokens;
90 $$ LANGUAGE SQL IMMUTABLE STRICT;
93 CREATE OR REPLACE FUNCTION token_normalized_postcode(postcode TEXT)
96 SELECT CASE WHEN postcode SIMILAR TO '%(,|;)%' THEN NULL ELSE upper(trim(postcode))END;
97 $$ LANGUAGE SQL IMMUTABLE STRICT;
100 -- Return token info that should be saved permanently in the database.
101 CREATE OR REPLACE FUNCTION token_strip_info(info JSONB)
105 $$ LANGUAGE SQL IMMUTABLE STRICT;
107 --------------- private functions ----------------------------------------------
109 CREATE OR REPLACE FUNCTION getorcreate_full_word(norm_term TEXT, lookup_terms TEXT[],
111 OUT partial_tokens INT[])
114 partial_terms TEXT[] = '{}'::TEXT[];
119 SELECT min(word_id) INTO full_token
120 FROM word WHERE word = norm_term and type = 'W';
122 IF full_token IS NULL THEN
123 full_token := nextval('seq_word');
124 INSERT INTO word (word_id, word_token, type, word, info)
125 SELECT full_token, lookup_term, 'W', norm_term,
126 json_build_object('count', 0)
127 FROM unnest(lookup_terms) as lookup_term;
130 FOR term IN SELECT unnest(string_to_array(unnest(lookup_terms), ' ')) LOOP
132 IF NOT (ARRAY[term] <@ partial_terms) THEN
133 partial_terms := partial_terms || term;
137 partial_tokens := '{}'::INT[];
138 FOR term IN SELECT unnest(partial_terms) LOOP
139 SELECT min(word_id), max(info->>'count') INTO term_id, term_count
140 FROM word WHERE word_token = term and type = 'w';
142 IF term_id IS NULL THEN
143 term_id := nextval('seq_word');
145 INSERT INTO word (word_id, word_token, type, info)
146 VALUES (term_id, term, 'w', json_build_object('count', term_count));
149 partial_tokens := array_merge(partial_tokens, ARRAY[term_id]);
156 CREATE OR REPLACE FUNCTION getorcreate_partial_word(partial TEXT)
162 SELECT min(word_id) INTO token
163 FROM word WHERE word_token = partial and type = 'w';
165 IF token IS NULL THEN
166 token := nextval('seq_word');
167 INSERT INTO word (word_id, word_token, type, info)
168 VALUES (token, partial, 'w', json_build_object('count', 0));
177 CREATE OR REPLACE FUNCTION getorcreate_hnr_id(lookup_term TEXT)
183 SELECT min(word_id) INTO return_id FROM word
184 WHERE word_token = lookup_term and type = 'H';
186 IF return_id IS NULL THEN
187 return_id := nextval('seq_word');
188 INSERT INTO word (word_id, word_token, type)
189 VALUES (return_id, lookup_term, 'H');