1 -- SPDX-License-Identifier: GPL-2.0-only
3 -- This file is part of Nominatim. (https://nominatim.org)
5 -- Copyright (C) 2026 by the Nominatim developer community.
6 -- For a full list of authors see the git log.
8 -- Functions related to search and address ranks
10 -- Function for removing the categories that shouldn't be taken into account while ranking.
11 -- Categories outside the osm.* tree are always kept.
12 CREATE OR REPLACE FUNCTION drop_unwanted_categories(categories ltree[], osm_type TEXT,
13 admin_level SMALLINT, name HSTORE,
14 extratags HSTORE, is_area BOOLEAN)
20 result ltree[] := ARRAY[]::ltree[];
22 IF categories IS NULL THEN
26 FOREACH cat IN ARRAY categories LOOP
27 IF cat::TEXT LIKE 'osm.%' THEN
28 cat_class := subpath(cat, 1, 1)::TEXT;
30 -- Check unnamed highway area
31 IF cat_class = 'highway'
32 AND is_area AND name IS NULL
33 AND extratags IS NOT NULL AND extratags ? 'area'
34 AND extratags->'area' = 'yes'
39 -- Check non-area boundary
40 IF cat_class = 'boundary' THEN
41 IF NOT is_area OR (admin_level <= 4 AND osm_type = 'W')
48 result := array_append(result, cat);
54 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
56 -- Check if a place is rankable at all.
57 -- These really should be dropped at the lua level eventually.
58 CREATE OR REPLACE FUNCTION is_rankable_place(osm_type TEXT, categories ltree[],
59 admin_level SMALLINT, name HSTORE,
60 extratags HSTORE, is_area BOOLEAN)
64 IF categories IS NULL THEN
67 RETURN array_length(drop_unwanted_categories(categories, osm_type, admin_level,
68 name, extratags, is_area), 1)
72 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
76 -- Return an approximate search radius according to the search rank.
77 CREATE OR REPLACE FUNCTION reverse_place_diameter(rank_search SMALLINT)
81 IF rank_search <= 4 THEN
83 ELSIF rank_search <= 8 THEN
85 ELSIF rank_search <= 12 THEN
87 ELSIF rank_search <= 17 THEN
89 ELSIF rank_search <= 18 THEN
91 ELSIF rank_search <= 19 THEN
98 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
101 -- Return an approximate update radius according to the search rank.
102 CREATE OR REPLACE FUNCTION update_place_diameter(rank_search SMALLINT)
107 IF rank_search = 11 or rank_search = 5 THEN
109 -- anything higher than city is effectively ignored (polygon required)
110 ELSIF rank_search < 16 THEN
112 ELSIF rank_search < 18 THEN
114 ELSIF rank_search < 20 THEN
116 ELSIF rank_search = 21 THEN
118 ELSIF rank_search < 24 THEN
120 ELSIF rank_search < 26 THEN
122 ELSIF rank_search < 28 THEN
129 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
131 -- Compute a base address rank from the extent of the given geometry.
133 -- This is all simple guess work. We don't need particularly good estimates
134 -- here. This just avoids to have very high ranked address parts in features
135 -- that span very large areas (or vice versa).
136 CREATE OR REPLACE FUNCTION geometry_to_rank(search_rank SMALLINT, geometry GEOMETRY, country_code TEXT)
142 IF ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') THEN
143 area := ST_Area(geometry);
144 ELSIF ST_GeometryType(geometry) in ('ST_LineString','ST_MultiLineString') THEN
145 area := (ST_Length(geometry)^2) * 0.1;
150 -- adjust for the fact that countries come in different sizes
151 IF country_code IN ('ca', 'au', 'ru') THEN
153 ELSIF country_code IN ('br', 'kz', 'cn', 'us', 'ne', 'gb', 'za', 'sa', 'id', 'eh', 'ml', 'tm') THEN
155 ELSIF country_code IN ('bo', 'ar', 'sd', 'mn', 'in', 'et', 'cd', 'mz', 'ly', 'cl', 'zm') THEN
157 ELSIF country_code IN ('sg', 'ws', 'st', 'kn') THEN
159 ELSIF country_code IN ('dm', 'mt', 'lc', 'gg', 'sc', 'nr') THEN
165 ELSIF area > 0.1 THEN
167 ELSIF area > 0.01 THEN
169 ELSIF area > 0.001 THEN
171 ELSIF area > 0.0001 THEN
173 ELSIF area > 0.000005 THEN
180 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
183 -- Get standard search and address rank for an object.
184 -- Iterates all osm.* categories and finds the one with the lowest positive
185 -- address rank. Address rank of 0 has the lowest priority. Ties broken by
186 -- lower search rank.
188 -- \param country Two-letter country code where the object is in.
189 -- \param extended_type OSM type (N, W, R) or area type (A).
190 -- \param categories ltree[] of osm.<class>.<type> categories.
191 -- \param admin_level Value of admin_level tag.
192 -- \param is_major If true, boost search rank by one.
193 -- \param postcode Value of addr:postcode tag.
194 -- \param[out] search_rank Computed search rank.
195 -- \param[out] address_rank Computed address rank.
197 CREATE OR REPLACE FUNCTION compute_place_rank(country VARCHAR(2),
198 extended_type VARCHAR(1),
200 admin_level SMALLINT,
203 OUT search_rank SMALLINT,
204 OUT address_rank SMALLINT)
211 best_search SMALLINT := 99;
212 best_address SMALLINT := 99;
213 cand_search SMALLINT;
214 cand_address SMALLINT;
215 has_boundary_admin BOOLEAN;
217 IF categories IS NULL THEN
223 -- Hoist: skip place categories when boundary/administrative is also present.
224 has_boundary_admin := categories <@ 'osm.boundary.administrative';
226 FOREACH cat IN ARRAY categories LOOP
227 -- Only consider osm.* categories
228 IF NOT (cat ~ 'osm.*'::lquery) THEN
232 -- Place ranks are handled by the post-hoc adjustment in placex_update.
233 IF has_boundary_admin AND cat ~ 'osm.place.*'::lquery THEN
237 cat_class := split_part(cat::text, '.', 2);
238 cat_type := split_part(cat::text, '.', 3);
239 -- Short-circuits for special cases
240 IF extended_type = 'N' AND cat_class = 'highway' THEN
243 ELSIF cat_class = 'landuse' AND extended_type != 'A' THEN
247 -- Build classtype for boundary/administrative
248 IF cat_class = 'boundary' AND cat_type = 'administrative' THEN
249 classtype := cat_type || admin_level::TEXT;
251 classtype := cat_type;
254 SELECT l.rank_search, l.rank_address INTO cand_search, cand_address
255 FROM address_levels l
256 WHERE (l.country_code = country OR l.country_code IS NULL)
257 AND l.class = cat_class AND (l.type = classtype OR l.type IS NULL)
258 ORDER BY l.country_code, l.class, l.type LIMIT 1;
260 IF cand_search IS NULL OR cand_address IS NULL THEN
265 -- Waterway relation boost
266 IF cat_class = 'waterway' AND extended_type = 'R' THEN
267 cand_search := cand_search - 1;
271 -- Selection: pick the candidate with lowest positive address rank.
272 -- Address rank of 0 has lowest priority (fallback only). Tiebreak: lower search rank.
273 -- A positive address rank always overrides a zero-address fallback (best_address = 0).
274 IF cand_address > 0 AND (best_address = 0 OR cand_address < best_address) THEN
275 best_search := cand_search;
276 best_address := cand_address;
277 ELSIF cand_address > 0 AND cand_address = best_address
278 AND cand_search < best_search THEN
279 best_search := cand_search;
280 best_address := cand_address;
281 ELSIF cand_address = 0 AND (best_address = 99 OR best_address = 0)
282 AND cand_search < best_search THEN
283 -- Fallback: when no addressable category found, pick best search rank
284 best_search := cand_search;
285 best_address := cand_address;
289 -- Apply is_major boost to the winner
291 best_search := best_search - 1;
294 search_rank := best_search;
295 address_rank := best_address;
298 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
300 CREATE OR REPLACE FUNCTION get_addr_tag_rank(key TEXT, country TEXT,
301 OUT from_rank SMALLINT,
302 OUT to_rank SMALLINT,
312 (SELECT l.rank_search, l.rank_address FROM address_levels l
313 WHERE (l.country_code = country or l.country_code is NULL)
314 AND l.class = 'place' AND l.type = key
315 ORDER BY l.country_code LIMIT 1) r
316 WHERE rank_address > 0
318 extent := reverse_place_diameter(ranks.rank_search);
320 IF ranks.rank_address <= 4 THEN
323 ELSEIF ranks.rank_address <= 9 THEN
326 ELSEIF ranks.rank_address <= 12 THEN
329 ELSEIF ranks.rank_address <= 16 THEN
332 ELSEIF ranks.rank_address <= 21 THEN
335 ELSEIF ranks.rank_address <= 24 THEN
345 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
348 CREATE OR REPLACE FUNCTION weigh_search(search_vector INT[],
357 SELECT * FROM json_array_elements(rankings::JSON)
359 IF true = ALL(SELECT x::int = ANY(search_vector) FROM json_array_elements_text(rank->1) as x) THEN
360 RETURN (rank->>0)::float;
366 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;