1 -- SPDX-License-Identifier: GPL-2.0-only
3 -- This file is part of Nominatim. (https://nominatim.org)
5 -- Copyright (C) 2022 by the Nominatim developer community.
6 -- For a full list of authors see the git log.
8 -- Get tokens used for searching the given place.
10 -- These are the tokens that will be saved in the search_name table.
11 CREATE OR REPLACE FUNCTION token_get_name_search_tokens(info JSONB)
14 SELECT (info->>'names')::INTEGER[]
15 $$ LANGUAGE SQL IMMUTABLE STRICT;
18 -- Get tokens for matching the place name against others.
20 -- This should usually be restricted to full name tokens.
21 CREATE OR REPLACE FUNCTION token_get_name_match_tokens(info JSONB)
24 SELECT (info->>'names')::INTEGER[]
25 $$ LANGUAGE SQL IMMUTABLE STRICT;
28 -- Return the housenumber tokens applicable for the place.
29 CREATE OR REPLACE FUNCTION token_get_housenumber_search_tokens(info JSONB)
32 SELECT (info->>'hnr_tokens')::INTEGER[]
33 $$ LANGUAGE SQL IMMUTABLE STRICT;
36 -- Return the housenumber in the form that it can be matched during search.
37 CREATE OR REPLACE FUNCTION token_normalized_housenumber(info JSONB)
41 $$ LANGUAGE SQL IMMUTABLE STRICT;
44 CREATE OR REPLACE FUNCTION token_is_street_address(info JSONB)
47 SELECT info->>'street' is not null or info->>'place' is null;
48 $$ LANGUAGE SQL IMMUTABLE;
51 CREATE OR REPLACE FUNCTION token_has_addr_street(info JSONB)
54 SELECT info->>'street' is not null and info->>'street' != '{}';
55 $$ LANGUAGE SQL IMMUTABLE;
58 CREATE OR REPLACE FUNCTION token_has_addr_place(info JSONB)
61 SELECT info->>'place' is not null;
62 $$ LANGUAGE SQL IMMUTABLE;
65 CREATE OR REPLACE FUNCTION token_matches_street(info JSONB, street_tokens INTEGER[])
68 SELECT (info->>'street')::INTEGER[] && street_tokens
69 $$ LANGUAGE SQL IMMUTABLE STRICT;
72 CREATE OR REPLACE FUNCTION token_matches_place(info JSONB, place_tokens INTEGER[])
75 SELECT (info->>'place')::INTEGER[] <@ place_tokens
76 $$ LANGUAGE SQL IMMUTABLE STRICT;
79 CREATE OR REPLACE FUNCTION token_addr_place_search_tokens(info JSONB)
82 SELECT (info->>'place')::INTEGER[]
83 $$ LANGUAGE SQL IMMUTABLE STRICT;
86 CREATE OR REPLACE FUNCTION token_get_address_keys(info JSONB)
89 SELECT * FROM jsonb_object_keys(info->'addr');
90 $$ LANGUAGE SQL IMMUTABLE STRICT;
93 CREATE OR REPLACE FUNCTION token_get_address_search_tokens(info JSONB, key TEXT)
96 SELECT (info->'addr'->>key)::INTEGER[];
97 $$ LANGUAGE SQL IMMUTABLE STRICT;
100 CREATE OR REPLACE FUNCTION token_matches_address(info JSONB, key TEXT, tokens INTEGER[])
103 SELECT (info->'addr'->>key)::INTEGER[] <@ tokens;
104 $$ LANGUAGE SQL IMMUTABLE STRICT;
107 CREATE OR REPLACE FUNCTION token_get_postcode(info JSONB)
110 SELECT info->>'postcode';
111 $$ LANGUAGE SQL IMMUTABLE STRICT;
114 -- Return token info that should be saved permanently in the database.
115 CREATE OR REPLACE FUNCTION token_strip_info(info JSONB)
119 $$ LANGUAGE SQL IMMUTABLE STRICT;
121 --------------- private functions ----------------------------------------------
123 CREATE OR REPLACE FUNCTION getorcreate_full_word(norm_term TEXT, lookup_terms TEXT[],
125 OUT partial_tokens INT[])
128 partial_terms TEXT[] = '{}'::TEXT[];
133 SELECT min(word_id) INTO full_token
134 FROM word WHERE word = norm_term and type = 'W';
136 IF full_token IS NULL THEN
137 full_token := nextval('seq_word');
138 INSERT INTO word (word_id, word_token, type, word, info)
139 SELECT full_token, lookup_term, 'W', norm_term,
140 json_build_object('count', 0)
141 FROM unnest(lookup_terms) as lookup_term;
144 FOR term IN SELECT unnest(string_to_array(unnest(lookup_terms), ' ')) LOOP
146 IF NOT (ARRAY[term] <@ partial_terms) THEN
147 partial_terms := partial_terms || term;
151 partial_tokens := '{}'::INT[];
152 FOR term IN SELECT unnest(partial_terms) LOOP
153 SELECT min(word_id), max(info->>'count') INTO term_id, term_count
154 FROM word WHERE word_token = term and type = 'w';
156 IF term_id IS NULL THEN
157 term_id := nextval('seq_word');
159 INSERT INTO word (word_id, word_token, type, info)
160 VALUES (term_id, term, 'w', json_build_object('count', term_count));
163 partial_tokens := array_merge(partial_tokens, ARRAY[term_id]);
170 CREATE OR REPLACE FUNCTION getorcreate_partial_word(partial TEXT)
176 SELECT min(word_id) INTO token
177 FROM word WHERE word_token = partial and type = 'w';
179 IF token IS NULL THEN
180 token := nextval('seq_word');
181 INSERT INTO word (word_id, word_token, type, info)
182 VALUES (token, partial, 'w', json_build_object('count', 0));
191 CREATE OR REPLACE FUNCTION getorcreate_hnr_id(lookup_term TEXT)
197 SELECT min(word_id) INTO return_id FROM word
198 WHERE word_token = lookup_term and type = 'H';
200 IF return_id IS NULL THEN
201 return_id := nextval('seq_word');
202 INSERT INTO word (word_id, word_token, type)
203 VALUES (return_id, lookup_term, 'H');
212 CREATE OR REPLACE FUNCTION create_analyzed_hnr_id(norm_term TEXT, lookup_terms TEXT[])
218 SELECT min(word_id) INTO return_id
219 FROM word WHERE word = norm_term and type = 'H';
221 IF return_id IS NULL THEN
222 return_id := nextval('seq_word');
223 INSERT INTO word (word_id, word_token, type, word, info)
224 SELECT return_id, lookup_term, 'H', norm_term,
225 json_build_object('lookup', lookup_terms[1])
226 FROM unnest(lookup_terms) as lookup_term;
234 CREATE OR REPLACE FUNCTION create_postcode_word(postcode TEXT, lookup_terms TEXT[])
240 SELECT count(*) INTO existing
241 FROM word WHERE word = postcode and type = 'P';
247 -- postcodes don't need word ids
248 INSERT INTO word (word_token, type, word)
249 SELECT lookup_term, 'P', postcode FROM unnest(lookup_terms) as lookup_term;