1 -- Functions for term normalisation and access to the 'word' table.
3 CREATE OR REPLACE FUNCTION transliteration(text) RETURNS text
4 AS '{modulepath}/nominatim.so', 'transliteration'
5 LANGUAGE c IMMUTABLE STRICT;
8 CREATE OR REPLACE FUNCTION gettokenstring(text) RETURNS text
9 AS '{modulepath}/nominatim.so', 'gettokenstring'
10 LANGUAGE c IMMUTABLE STRICT;
13 CREATE OR REPLACE FUNCTION make_standard_name(name TEXT) RETURNS TEXT
18 o := public.gettokenstring(public.transliteration(name));
19 RETURN trim(substr(o,1,length(o)));
22 LANGUAGE plpgsql IMMUTABLE;
24 -- returns NULL if the word is too common
25 CREATE OR REPLACE FUNCTION getorcreate_word_id(lookup_word TEXT)
30 return_word_id INTEGER;
33 lookup_token := trim(lookup_word);
34 SELECT min(word_id), max(search_name_count) FROM word
35 WHERE word_token = lookup_token and class is null and type is null
36 INTO return_word_id, count;
37 IF return_word_id IS NULL THEN
38 return_word_id := nextval('seq_word');
39 INSERT INTO word VALUES (return_word_id, lookup_token, null, null, null, null, 0);
41 IF count > get_maxwordfreq() THEN
42 return_word_id := NULL;
45 RETURN return_word_id;
51 CREATE OR REPLACE FUNCTION getorcreate_housenumber_id(lookup_word TEXT)
56 return_word_id INTEGER;
58 lookup_token := ' ' || trim(lookup_word);
59 SELECT min(word_id) FROM word
60 WHERE word_token = lookup_token and class='place' and type='house'
62 IF return_word_id IS NULL THEN
63 return_word_id := nextval('seq_word');
64 INSERT INTO word VALUES (return_word_id, lookup_token, null,
65 'place', 'house', null, 0);
67 RETURN return_word_id;
73 CREATE OR REPLACE FUNCTION getorcreate_postcode_id(postcode TEXT)
79 return_word_id INTEGER;
81 lookup_word := upper(trim(postcode));
82 lookup_token := ' ' || make_standard_name(lookup_word);
83 SELECT min(word_id) FROM word
84 WHERE word_token = lookup_token and word = lookup_word
85 and class='place' and type='postcode'
87 IF return_word_id IS NULL THEN
88 return_word_id := nextval('seq_word');
89 INSERT INTO word VALUES (return_word_id, lookup_token, lookup_word,
90 'place', 'postcode', null, 0);
92 RETURN return_word_id;
98 CREATE OR REPLACE FUNCTION getorcreate_country(lookup_word TEXT,
99 lookup_country_code varchar(2))
104 return_word_id INTEGER;
106 lookup_token := ' '||trim(lookup_word);
107 SELECT min(word_id) FROM word
108 WHERE word_token = lookup_token and country_code=lookup_country_code
110 IF return_word_id IS NULL THEN
111 return_word_id := nextval('seq_word');
112 INSERT INTO word VALUES (return_word_id, lookup_token, null,
113 null, null, lookup_country_code, 0);
115 RETURN return_word_id;
121 CREATE OR REPLACE FUNCTION getorcreate_amenity(lookup_word TEXT, normalized_word TEXT,
122 lookup_class text, lookup_type text)
127 return_word_id INTEGER;
129 lookup_token := ' '||trim(lookup_word);
130 SELECT min(word_id) FROM word
131 WHERE word_token = lookup_token and word = normalized_word
132 and class = lookup_class and type = lookup_type
134 IF return_word_id IS NULL THEN
135 return_word_id := nextval('seq_word');
136 INSERT INTO word VALUES (return_word_id, lookup_token, normalized_word,
137 lookup_class, lookup_type, null, 0);
139 RETURN return_word_id;
145 CREATE OR REPLACE FUNCTION getorcreate_amenityoperator(lookup_word TEXT,
146 normalized_word TEXT,
154 return_word_id INTEGER;
156 lookup_token := ' '||trim(lookup_word);
157 SELECT min(word_id) FROM word
158 WHERE word_token = lookup_token and word = normalized_word
159 and class = lookup_class and type = lookup_type and operator = op
161 IF return_word_id IS NULL THEN
162 return_word_id := nextval('seq_word');
163 INSERT INTO word VALUES (return_word_id, lookup_token, normalized_word,
164 lookup_class, lookup_type, null, 0, op);
166 RETURN return_word_id;
172 CREATE OR REPLACE FUNCTION getorcreate_name_id(lookup_word TEXT, src_word TEXT)
177 nospace_lookup_token TEXT;
178 return_word_id INTEGER;
180 lookup_token := ' '||trim(lookup_word);
181 SELECT min(word_id) FROM word
182 WHERE word_token = lookup_token and class is null and type is null
184 IF return_word_id IS NULL THEN
185 return_word_id := nextval('seq_word');
186 INSERT INTO word VALUES (return_word_id, lookup_token, src_word,
187 null, null, null, 0);
189 RETURN return_word_id;
195 CREATE OR REPLACE FUNCTION getorcreate_name_id(lookup_word TEXT)
200 RETURN getorcreate_name_id(lookup_word, '');
205 -- Normalize a string and lookup its word ids (partial words).
206 CREATE OR REPLACE FUNCTION addr_ids_from_name(lookup_word TEXT)
212 return_word_id INTEGER[];
214 lookup_token := make_standard_name(lookup_word);
215 SELECT array_agg(word_id) FROM word
216 WHERE word_token = lookup_token and class is null and type is null
218 IF return_word_id IS NULL THEN
219 id := nextval('seq_word');
220 INSERT INTO word VALUES (id, lookup_token, null, null, null, null, 0);
221 return_word_id = ARRAY[id];
223 RETURN return_word_id;
229 -- Normalize a string and look up its name ids (full words).
230 CREATE OR REPLACE FUNCTION word_ids_from_name(lookup_word TEXT)
235 return_word_ids INTEGER[];
237 lookup_token := ' '|| make_standard_name(lookup_word);
238 SELECT array_agg(word_id) FROM word
239 WHERE word_token = lookup_token and class is null and type is null
240 INTO return_word_ids;
241 RETURN return_word_ids;
244 LANGUAGE plpgsql STABLE STRICT;
247 CREATE OR REPLACE FUNCTION create_country(src HSTORE, country_code varchar(2))
257 FOR item IN SELECT (each(src)).* LOOP
259 s := make_standard_name(item.value);
260 w := getorcreate_country(s, country_code);
262 words := regexp_split_to_array(item.value, E'[,;()]');
263 IF array_upper(words, 1) != 1 THEN
264 FOR j IN 1..array_upper(words, 1) LOOP
265 s := make_standard_name(words[j]);
267 w := getorcreate_country(s, country_code);
277 CREATE OR REPLACE FUNCTION make_keywords(src HSTORE)
288 result := '{}'::INTEGER[];
290 FOR item IN SELECT (each(src)).* LOOP
292 s := make_standard_name(item.value);
293 w := getorcreate_name_id(s, item.value);
295 IF not(ARRAY[w] <@ result) THEN
296 result := result || w;
299 w := getorcreate_word_id(s);
301 IF w IS NOT NULL AND NOT (ARRAY[w] <@ result) THEN
302 result := result || w;
305 words := string_to_array(s, ' ');
306 IF array_upper(words, 1) IS NOT NULL THEN
307 FOR j IN 1..array_upper(words, 1) LOOP
308 IF (words[j] != '') THEN
309 w = getorcreate_word_id(words[j]);
310 IF w IS NOT NULL AND NOT (ARRAY[w] <@ result) THEN
311 result := result || w;
317 words := regexp_split_to_array(item.value, E'[,;()]');
318 IF array_upper(words, 1) != 1 THEN
319 FOR j IN 1..array_upper(words, 1) LOOP
320 s := make_standard_name(words[j]);
322 w := getorcreate_word_id(s);
323 IF w IS NOT NULL AND NOT (ARRAY[w] <@ result) THEN
324 result := result || w;
330 s := regexp_replace(item.value, '市$', '');
331 IF s != item.value THEN
332 s := make_standard_name(s);
334 w := getorcreate_name_id(s, item.value);
335 IF NOT (ARRAY[w] <@ result) THEN
336 result := result || w;
349 CREATE OR REPLACE FUNCTION make_keywords(src TEXT)
360 result := '{}'::INTEGER[];
362 s := make_standard_name(src);
363 w := getorcreate_name_id(s, src);
365 IF NOT (ARRAY[w] <@ result) THEN
366 result := result || w;
369 w := getorcreate_word_id(s);
371 IF w IS NOT NULL AND NOT (ARRAY[w] <@ result) THEN
372 result := result || w;
375 words := string_to_array(s, ' ');
376 IF array_upper(words, 1) IS NOT NULL THEN
377 FOR j IN 1..array_upper(words, 1) LOOP
378 IF (words[j] != '') THEN
379 w = getorcreate_word_id(words[j]);
380 IF w IS NOT NULL AND NOT (ARRAY[w] <@ result) THEN
381 result := result || w;
387 words := regexp_split_to_array(src, E'[,;()]');
388 IF array_upper(words, 1) != 1 THEN
389 FOR j IN 1..array_upper(words, 1) LOOP
390 s := make_standard_name(words[j]);
392 w := getorcreate_word_id(s);
393 IF w IS NOT NULL AND NOT (ARRAY[w] <@ result) THEN
394 result := result || w;
400 s := regexp_replace(src, '市$', '');
402 s := make_standard_name(s);
404 w := getorcreate_name_id(s, src);
405 IF NOT (ARRAY[w] <@ result) THEN
406 result := result || w;
417 CREATE OR REPLACE FUNCTION create_poi_search_terms(obj_place_id BIGINT,
418 in_partition SMALLINT,
419 parent_place_id BIGINT,
423 initial_name_vector INTEGER[],
425 OUT name_vector INTEGER[],
426 OUT nameaddress_vector INTEGER[])
429 parent_name_vector INTEGER[];
430 parent_address_vector INTEGER[];
431 addr_place_ids INTEGER[];
434 parent_address_place_ids BIGINT[];
435 filtered_address HSTORE;
437 nameaddress_vector := '{}'::INTEGER[];
439 SELECT s.name_vector, s.nameaddress_vector
440 INTO parent_name_vector, parent_address_vector
442 WHERE s.place_id = parent_place_id;
444 -- Find all address tags that don't appear in the parent search names.
445 SELECT hstore(array_agg(ARRAY[k, v])) INTO filtered_address
446 FROM (SELECT skeys(address) as k, svals(address) as v) a
447 WHERE not addr_ids_from_name(v) && parent_address_vector
448 AND k not in ('country', 'street', 'place', 'postcode',
449 'housenumber', 'streetnumber', 'conscriptionnumber');
451 -- Compute all search terms from the addr: tags.
452 IF filtered_address IS NOT NULL THEN
455 get_places_for_addr_tags(in_partition, geometry, filtered_address, country)
457 IF addr_item.place_id is null THEN
458 nameaddress_vector := array_merge(nameaddress_vector,
463 IF parent_address_place_ids is null THEN
464 SELECT array_agg(parent_place_id) INTO parent_address_place_ids
465 FROM place_addressline
466 WHERE place_id = parent_place_id;
469 IF not parent_address_place_ids @> ARRAY[addr_item.place_id] THEN
470 nameaddress_vector := array_merge(nameaddress_vector,
473 INSERT INTO place_addressline (place_id, address_place_id, fromarea,
474 isaddress, distance, cached_rank_address)
475 VALUES (obj_place_id, addr_item.place_id, not addr_item.isguess,
476 true, addr_item.distance, addr_item.rank_address);
482 -- If the POI is named, simply mix in all address terms and be done.
483 IF array_length(initial_name_vector, 1) is not NULL THEN
484 -- Cheating here by not recomputing all terms but simply using the ones
485 -- from the parent object.
486 name_vector := initial_name_vector;
487 nameaddress_vector := array_merge(nameaddress_vector, parent_name_vector);
488 nameaddress_vector := array_merge(nameaddress_vector, parent_address_vector);
490 IF not address ? 'street' and address ? 'place' THEN
491 -- make sure addr:place terms are always searchable
492 nameaddress_vector := array_merge(nameaddress_vector,
493 addr_ids_from_name(address->'place'));
501 IF (array_length(nameaddress_vector, 1) is null
502 and (address ? 'street'or not address ? 'place'))
503 or housenumber is null
508 -- Check if the parent covers all address terms.
509 -- If not, create a search name entry with the house number as the name.
510 -- This is unusual for the search_name table but prevents that the place
511 -- is returned when we only search for the street/place.
513 IF not nameaddress_vector <@ parent_address_vector THEN
514 name_vector := ARRAY[getorcreate_name_id(housenumber)];
517 IF not address ? 'street' and address ? 'place' THEN
518 addr_place_ids := addr_ids_from_name(address->'place');
519 IF not addr_place_ids <@ parent_name_vector THEN
520 -- addr:place tag exists without a corresponding place. Mix in addr:place
521 -- in the address and drop the name from the parent. This would only be
522 -- the street name of the nearest street.
523 nameaddress_vector := array_merge(nameaddress_vector, addr_place_ids);
524 name_vector := ARRAY[getorcreate_name_id(housenumber)];
527 nameaddress_vector := array_merge(nameaddress_vector, parent_name_vector);
530 -- The address vector always gets merged in.
531 nameaddress_vector := array_merge(nameaddress_vector, parent_address_vector);