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, class TEXT,
13 admin_level SMALLINT, name HSTORE,
14 extratags HSTORE, is_area BOOLEAN)
18 IF class = 'highway' AND is_area AND name is null
19 AND extratags ? 'area' AND extratags->'area' = 'yes'
24 IF class = 'boundary' THEN
26 OR (admin_level <= 4 AND osm_type = 'W')
35 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
38 -- Return an approximate search radius according to the search rank.
39 CREATE OR REPLACE FUNCTION reverse_place_diameter(rank_search SMALLINT)
43 IF rank_search <= 4 THEN
45 ELSIF rank_search <= 8 THEN
47 ELSIF rank_search <= 12 THEN
49 ELSIF rank_search <= 17 THEN
51 ELSIF rank_search <= 18 THEN
53 ELSIF rank_search <= 19 THEN
60 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
63 -- Return an approximate update radius according to the search rank.
64 CREATE OR REPLACE FUNCTION update_place_diameter(rank_search SMALLINT)
69 IF rank_search = 11 or rank_search = 5 THEN
71 -- anything higher than city is effectively ignored (polygon required)
72 ELSIF rank_search < 16 THEN
74 ELSIF rank_search < 18 THEN
76 ELSIF rank_search < 20 THEN
78 ELSIF rank_search = 21 THEN
80 ELSIF rank_search < 24 THEN
82 ELSIF rank_search < 26 THEN
84 ELSIF rank_search < 28 THEN
91 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
93 -- Compute a base address rank from the extent of the given geometry.
95 -- This is all simple guess work. We don't need particularly good estimates
96 -- here. This just avoids to have very high ranked address parts in features
97 -- that span very large areas (or vice versa).
98 CREATE OR REPLACE FUNCTION geometry_to_rank(search_rank SMALLINT, geometry GEOMETRY, country_code TEXT)
104 IF ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') THEN
105 area := ST_Area(geometry);
106 ELSIF ST_GeometryType(geometry) in ('ST_LineString','ST_MultiLineString') THEN
107 area := (ST_Length(geometry)^2) * 0.1;
112 -- adjust for the fact that countries come in different sizes
113 IF country_code IN ('ca', 'au', 'ru') THEN
115 ELSIF country_code IN ('br', 'kz', 'cn', 'us', 'ne', 'gb', 'za', 'sa', 'id', 'eh', 'ml', 'tm') THEN
117 ELSIF country_code IN ('bo', 'ar', 'sd', 'mn', 'in', 'et', 'cd', 'mz', 'ly', 'cl', 'zm') THEN
119 ELSIF country_code IN ('sg', 'ws', 'st', 'kn') THEN
121 ELSIF country_code IN ('dm', 'mt', 'lc', 'gg', 'sc', 'nr') THEN
127 ELSIF area > 0.1 THEN
129 ELSIF area > 0.01 THEN
131 ELSIF area > 0.001 THEN
133 ELSIF area > 0.0001 THEN
135 ELSIF area > 0.000005 THEN
142 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
145 -- Get standard search and address rank for an object.
147 -- \param country Two-letter country code where the object is in.
148 -- \param extended_type OSM type (N, W, R) or area type (A).
149 -- \param place_class Class (or tag key) of object.
150 -- \param place_type Type (or tag value) of object.
151 -- \param admin_level Value of admin_level tag.
152 -- \param is_major If true, boost search rank by one.
153 -- \param postcode Value of addr:postcode tag.
154 -- \param[out] search_rank Computed search rank.
155 -- \param[out] address_rank Computed address rank.
157 CREATE OR REPLACE FUNCTION compute_place_rank(country VARCHAR(2),
158 extended_type VARCHAR(1),
159 place_class TEXT, place_type TEXT,
160 admin_level SMALLINT,
163 OUT search_rank SMALLINT,
164 OUT address_rank SMALLINT)
169 IF extended_type = 'N' AND place_class = 'highway' THEN
172 ELSEIF place_class = 'landuse' AND extended_type != 'A' THEN
176 IF place_class = 'boundary' and place_type = 'administrative' THEN
177 classtype = place_type || admin_level::TEXT;
179 classtype = place_type;
182 SELECT l.rank_search, l.rank_address INTO search_rank, address_rank
183 FROM address_levels l
184 WHERE (l.country_code = country or l.country_code is NULL)
185 AND l.class = place_class AND (l.type = classtype or l.type is NULL)
186 ORDER BY l.country_code, l.class, l.type LIMIT 1;
188 IF search_rank is NULL OR address_rank is NULL THEN
193 -- some postcorrections
194 IF place_class = 'waterway' AND extended_type = 'R' THEN
195 -- Slightly promote waterway relations so that they are processed
196 -- before their members.
197 search_rank := search_rank - 1;
201 search_rank := search_rank - 1;
206 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
208 CREATE OR REPLACE FUNCTION get_addr_tag_rank(key TEXT, country TEXT,
209 OUT from_rank SMALLINT,
210 OUT to_rank SMALLINT,
220 (SELECT l.rank_search, l.rank_address FROM address_levels l
221 WHERE (l.country_code = country or l.country_code is NULL)
222 AND l.class = 'place' AND l.type = key
223 ORDER BY l.country_code LIMIT 1) r
224 WHERE rank_address > 0
226 extent := reverse_place_diameter(ranks.rank_search);
228 IF ranks.rank_address <= 4 THEN
231 ELSEIF ranks.rank_address <= 9 THEN
234 ELSEIF ranks.rank_address <= 12 THEN
237 ELSEIF ranks.rank_address <= 16 THEN
240 ELSEIF ranks.rank_address <= 21 THEN
243 ELSEIF ranks.rank_address <= 24 THEN
253 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
256 CREATE OR REPLACE FUNCTION weigh_search(search_vector INT[],
265 SELECT * FROM json_array_elements(rankings::JSON)
267 IF true = ALL(SELECT x::int = ANY(search_vector) FROM json_array_elements_text(rank->1) as x) THEN
268 RETURN (rank->>0)::float;
274 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;