1 -- SPDX-License-Identifier: GPL-2.0-only
3 -- This file is part of Nominatim. (https://nominatim.org)
5 -- Copyright (C) 2025 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 -- Return an approximate search radius according to the search rank.
11 CREATE OR REPLACE FUNCTION reverse_place_diameter(rank_search SMALLINT)
15 IF rank_search <= 4 THEN
17 ELSIF rank_search <= 8 THEN
19 ELSIF rank_search <= 12 THEN
21 ELSIF rank_search <= 17 THEN
23 ELSIF rank_search <= 18 THEN
25 ELSIF rank_search <= 19 THEN
32 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
35 -- Return an approximate update radius according to the search rank.
36 CREATE OR REPLACE FUNCTION update_place_diameter(rank_search SMALLINT)
41 IF rank_search = 11 or rank_search = 5 THEN
43 -- anything higher than city is effectively ignored (polygon required)
44 ELSIF rank_search < 16 THEN
46 ELSIF rank_search < 18 THEN
48 ELSIF rank_search < 20 THEN
50 ELSIF rank_search = 21 THEN
52 ELSIF rank_search < 24 THEN
54 ELSIF rank_search < 26 THEN
56 ELSIF rank_search < 28 THEN
63 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
65 -- Compute a base address rank from the extent of the given geometry.
67 -- This is all simple guess work. We don't need particularly good estimates
68 -- here. This just avoids to have very high ranked address parts in features
69 -- that span very large areas (or vice versa).
70 CREATE OR REPLACE FUNCTION geometry_to_rank(search_rank SMALLINT, geometry GEOMETRY, country_code TEXT)
76 IF ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') THEN
77 area := ST_Area(geometry);
78 ELSIF ST_GeometryType(geometry) in ('ST_LineString','ST_MultiLineString') THEN
79 area := (ST_Length(geometry)^2) * 0.1;
84 -- adjust for the fact that countries come in different sizes
85 IF country_code IN ('ca', 'au', 'ru') THEN
87 ELSIF country_code IN ('br', 'kz', 'cn', 'us', 'ne', 'gb', 'za', 'sa', 'id', 'eh', 'ml', 'tm') THEN
89 ELSIF country_code IN ('bo', 'ar', 'sd', 'mn', 'in', 'et', 'cd', 'mz', 'ly', 'cl', 'zm') THEN
91 ELSIF country_code IN ('sg', 'ws', 'st', 'kn') THEN
93 ELSIF country_code IN ('dm', 'mt', 'lc', 'gg', 'sc', 'nr') THEN
101 ELSIF area > 0.01 THEN
103 ELSIF area > 0.001 THEN
105 ELSIF area > 0.0001 THEN
107 ELSIF area > 0.000005 THEN
114 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
117 -- Get standard search and address rank for an object.
119 -- \param country Two-letter country code where the object is in.
120 -- \param extended_type OSM type (N, W, R) or area type (A).
121 -- \param place_class Class (or tag key) of object.
122 -- \param place_type Type (or tag value) of object.
123 -- \param admin_level Value of admin_level tag.
124 -- \param is_major If true, boost search rank by one.
125 -- \param postcode Value of addr:postcode tag.
126 -- \param[out] search_rank Computed search rank.
127 -- \param[out] address_rank Computed address rank.
129 CREATE OR REPLACE FUNCTION compute_place_rank(country VARCHAR(2),
130 extended_type VARCHAR(1),
131 place_class TEXT, place_type TEXT,
132 admin_level SMALLINT,
135 OUT search_rank SMALLINT,
136 OUT address_rank SMALLINT)
141 IF extended_type = 'N' AND place_class = 'highway' THEN
144 ELSEIF place_class = 'landuse' AND extended_type != 'A' THEN
148 IF place_class = 'boundary' and place_type = 'administrative' THEN
149 classtype = place_type || admin_level::TEXT;
151 classtype = place_type;
154 SELECT l.rank_search, l.rank_address INTO search_rank, address_rank
155 FROM address_levels l
156 WHERE (l.country_code = country or l.country_code is NULL)
157 AND l.class = place_class AND (l.type = classtype or l.type is NULL)
158 ORDER BY l.country_code, l.class, l.type LIMIT 1;
160 IF search_rank is NULL OR address_rank is NULL THEN
165 -- some postcorrections
166 IF place_class = 'waterway' AND extended_type = 'R' THEN
167 -- Slightly promote waterway relations so that they are processed
168 -- before their members.
169 search_rank := search_rank - 1;
173 search_rank := search_rank - 1;
178 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
180 CREATE OR REPLACE FUNCTION get_addr_tag_rank(key TEXT, country TEXT,
181 OUT from_rank SMALLINT,
182 OUT to_rank SMALLINT,
192 (SELECT l.rank_search, l.rank_address FROM address_levels l
193 WHERE (l.country_code = country or l.country_code is NULL)
194 AND l.class = 'place' AND l.type = key
195 ORDER BY l.country_code LIMIT 1) r
196 WHERE rank_address > 0
198 extent := reverse_place_diameter(ranks.rank_search);
200 IF ranks.rank_address <= 4 THEN
203 ELSEIF ranks.rank_address <= 9 THEN
206 ELSEIF ranks.rank_address <= 12 THEN
209 ELSEIF ranks.rank_address <= 16 THEN
212 ELSEIF ranks.rank_address <= 21 THEN
215 ELSEIF ranks.rank_address <= 24 THEN
225 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
228 CREATE OR REPLACE FUNCTION weigh_search(search_vector INT[],
237 SELECT * FROM json_array_elements(rankings::JSON)
239 IF true = ALL(SELECT x::int = ANY(search_vector) FROM json_array_elements_text(rank->1) as x) THEN
240 RETURN (rank->>0)::float;
246 LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;