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 -- Check if a place is rankable at all.
11 -- These really should be dropped at the lua level eventually.
12 CREATE OR REPLACE FUNCTION is_rankable_place(osm_type TEXT, categories ltree[],
13 admin_level SMALLINT, name HSTORE,
14 extratags HSTORE, is_area BOOLEAN)
21 IF categories IS NULL THEN
24 FOREACH cat IN ARRAY categories LOOP
25 -- Only check osm.* categories
26 IF cat ~ 'osm.*'::lquery THEN
27 cat_class := split_part(cat::text, '.', 2);
29 -- Check unnamed highway area
30 IF cat_class = 'highway' AND is_area AND name IS NULL
31 AND extratags ? 'area' AND extratags->'area' = 'yes'
36 -- Check non-area boundary
37 IF cat_class = 'boundary' THEN
39 OR (admin_level <= 4 AND osm_type = 'W')
52 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
55 -- Return an approximate search radius according to the search rank.
56 CREATE OR REPLACE FUNCTION reverse_place_diameter(rank_search SMALLINT)
60 IF rank_search <= 4 THEN
62 ELSIF rank_search <= 8 THEN
64 ELSIF rank_search <= 12 THEN
66 ELSIF rank_search <= 17 THEN
68 ELSIF rank_search <= 18 THEN
70 ELSIF rank_search <= 19 THEN
77 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
80 -- Return an approximate update radius according to the search rank.
81 CREATE OR REPLACE FUNCTION update_place_diameter(rank_search SMALLINT)
86 IF rank_search = 11 or rank_search = 5 THEN
88 -- anything higher than city is effectively ignored (polygon required)
89 ELSIF rank_search < 16 THEN
91 ELSIF rank_search < 18 THEN
93 ELSIF rank_search < 20 THEN
95 ELSIF rank_search = 21 THEN
97 ELSIF rank_search < 24 THEN
99 ELSIF rank_search < 26 THEN
101 ELSIF rank_search < 28 THEN
108 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
110 -- Compute a base address rank from the extent of the given geometry.
112 -- This is all simple guess work. We don't need particularly good estimates
113 -- here. This just avoids to have very high ranked address parts in features
114 -- that span very large areas (or vice versa).
115 CREATE OR REPLACE FUNCTION geometry_to_rank(search_rank SMALLINT, geometry GEOMETRY, country_code TEXT)
121 IF ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') THEN
122 area := ST_Area(geometry);
123 ELSIF ST_GeometryType(geometry) in ('ST_LineString','ST_MultiLineString') THEN
124 area := (ST_Length(geometry)^2) * 0.1;
129 -- adjust for the fact that countries come in different sizes
130 IF country_code IN ('ca', 'au', 'ru') THEN
132 ELSIF country_code IN ('br', 'kz', 'cn', 'us', 'ne', 'gb', 'za', 'sa', 'id', 'eh', 'ml', 'tm') THEN
134 ELSIF country_code IN ('bo', 'ar', 'sd', 'mn', 'in', 'et', 'cd', 'mz', 'ly', 'cl', 'zm') THEN
136 ELSIF country_code IN ('sg', 'ws', 'st', 'kn') THEN
138 ELSIF country_code IN ('dm', 'mt', 'lc', 'gg', 'sc', 'nr') THEN
144 ELSIF area > 0.1 THEN
146 ELSIF area > 0.01 THEN
148 ELSIF area > 0.001 THEN
150 ELSIF area > 0.0001 THEN
152 ELSIF area > 0.000005 THEN
159 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
162 -- Get standard search and address rank for an object.
163 -- Iterates all osm.* categories and finds the one with the lowest positive
164 -- address rank. Address rank of 0 has the lowest priority. Ties broken by
165 -- lower search rank.
167 -- \param country Two-letter country code where the object is in.
168 -- \param extended_type OSM type (N, W, R) or area type (A).
169 -- \param categories ltree[] of osm.<class>.<type> categories.
170 -- \param admin_level Value of admin_level tag.
171 -- \param is_major If true, boost search rank by one.
172 -- \param postcode Value of addr:postcode tag.
173 -- \param[out] search_rank Computed search rank.
174 -- \param[out] address_rank Computed address rank.
176 CREATE OR REPLACE FUNCTION compute_place_rank(country VARCHAR(2),
177 extended_type VARCHAR(1),
179 admin_level SMALLINT,
182 OUT search_rank SMALLINT,
183 OUT address_rank SMALLINT)
190 best_search SMALLINT := 99;
191 best_address SMALLINT := 99;
192 cand_search SMALLINT;
193 cand_address SMALLINT;
194 has_boundary_admin BOOLEAN;
196 IF categories IS NULL THEN
202 -- Hoist: skip place categories when boundary/administrative is also present.
203 has_boundary_admin := categories <@ 'osm.boundary.administrative';
205 FOREACH cat IN ARRAY categories LOOP
206 -- Only consider osm.* categories
207 IF NOT (cat ~ 'osm.*'::lquery) THEN
211 -- Place ranks are handled by the post-hoc adjustment in placex_update.
212 IF has_boundary_admin AND cat ~ 'osm.place.*'::lquery THEN
216 cat_class := split_part(cat::text, '.', 2);
217 cat_type := split_part(cat::text, '.', 3);
218 -- Short-circuits for special cases
219 IF extended_type = 'N' AND cat_class = 'highway' THEN
222 ELSIF cat_class = 'landuse' AND extended_type != 'A' THEN
226 -- Build classtype for boundary/administrative
227 IF cat_class = 'boundary' AND cat_type = 'administrative' THEN
228 classtype := cat_type || admin_level::TEXT;
230 classtype := cat_type;
233 SELECT l.rank_search, l.rank_address INTO cand_search, cand_address
234 FROM address_levels l
235 WHERE (l.country_code = country OR l.country_code IS NULL)
236 AND l.class = cat_class AND (l.type = classtype OR l.type IS NULL)
237 ORDER BY l.country_code, l.class, l.type LIMIT 1;
239 IF cand_search IS NULL OR cand_address IS NULL THEN
244 -- Waterway relation boost
245 IF cat_class = 'waterway' AND extended_type = 'R' THEN
246 cand_search := cand_search - 1;
250 -- Selection: pick the candidate with lowest positive address rank.
251 -- Address rank of 0 has lowest priority (fallback only). Tiebreak: lower search rank.
252 -- A positive address rank always overrides a zero-address fallback (best_address = 0).
253 IF cand_address > 0 AND (best_address = 0 OR cand_address < best_address) THEN
254 best_search := cand_search;
255 best_address := cand_address;
256 ELSIF cand_address > 0 AND cand_address = best_address
257 AND cand_search < best_search THEN
258 best_search := cand_search;
259 best_address := cand_address;
260 ELSIF cand_address = 0 AND (best_address = 99 OR best_address = 0)
261 AND cand_search < best_search THEN
262 -- Fallback: when no addressable category found, pick best search rank
263 best_search := cand_search;
264 best_address := cand_address;
268 -- Apply is_major boost to the winner
270 best_search := best_search - 1;
273 search_rank := best_search;
274 address_rank := best_address;
277 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
279 CREATE OR REPLACE FUNCTION get_addr_tag_rank(key TEXT, country TEXT,
280 OUT from_rank SMALLINT,
281 OUT to_rank SMALLINT,
291 (SELECT l.rank_search, l.rank_address FROM address_levels l
292 WHERE (l.country_code = country or l.country_code is NULL)
293 AND l.class = 'place' AND l.type = key
294 ORDER BY l.country_code LIMIT 1) r
295 WHERE rank_address > 0
297 extent := reverse_place_diameter(ranks.rank_search);
299 IF ranks.rank_address <= 4 THEN
302 ELSEIF ranks.rank_address <= 9 THEN
305 ELSEIF ranks.rank_address <= 12 THEN
308 ELSEIF ranks.rank_address <= 16 THEN
311 ELSEIF ranks.rank_address <= 21 THEN
314 ELSEIF ranks.rank_address <= 24 THEN
324 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
327 CREATE OR REPLACE FUNCTION weigh_search(search_vector INT[],
336 SELECT * FROM json_array_elements(rankings::JSON)
338 IF true = ALL(SELECT x::int = ANY(search_vector) FROM json_array_elements_text(rank->1) as x) THEN
339 RETURN (rank->>0)::float;
345 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;