1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Mix-ins that provide the actual commands for the indexer for various indexing
13 from psycopg2 import sql as pysql
14 import psycopg2.extras
16 from nominatim.data.place_info import PlaceInfo
18 # pylint: disable=C0111
20 def _mk_valuelist(template, num):
21 return pysql.SQL(',').join([pysql.SQL(template)] * num)
23 def _analyze_place(place, analyzer):
24 return psycopg2.extras.Json(analyzer.process_place(PlaceInfo(place)))
26 class AbstractPlacexRunner:
27 """ Returns SQL commands for indexing of the placex table.
29 SELECT_SQL = pysql.SQL('SELECT place_id FROM placex ')
30 UPDATE_LINE = "(%s, %s::hstore, %s::hstore, %s::int, %s::jsonb)"
32 def __init__(self, rank, analyzer):
34 self.analyzer = analyzer
38 @functools.lru_cache(maxsize=1)
39 def _index_sql(num_places):
42 SET indexed_status = 0, address = v.addr, token_info = v.ti,
43 name = v.name, linked_place_id = v.linked_place_id
44 FROM (VALUES {}) as v(id, name, addr, linked_place_id, ti)
46 """).format(_mk_valuelist(AbstractPlacexRunner.UPDATE_LINE, num_places))
50 def get_place_details(worker, ids):
51 worker.perform("""SELECT place_id, extra.*
52 FROM placex, LATERAL placex_indexing_prepare(placex) as extra
53 WHERE place_id IN %s""",
54 (tuple((p[0] for p in ids)), ))
57 def index_places(self, worker, places):
60 for field in ('place_id', 'name', 'address', 'linked_place_id'):
61 values.append(place[field])
62 values.append(_analyze_place(place, self.analyzer))
64 worker.perform(self._index_sql(len(places)), values)
67 class RankRunner(AbstractPlacexRunner):
68 """ Returns SQL commands for indexing one rank within the placex table.
72 return f"rank {self.rank}"
74 def sql_count_objects(self):
75 return pysql.SQL("""SELECT count(*) FROM placex
76 WHERE rank_address = {} and indexed_status > 0
77 """).format(pysql.Literal(self.rank))
79 def sql_get_objects(self):
80 return self.SELECT_SQL + pysql.SQL(
81 """WHERE indexed_status > 0 and rank_address = {}
82 ORDER BY geometry_sector
83 """).format(pysql.Literal(self.rank))
86 class BoundaryRunner(AbstractPlacexRunner):
87 """ Returns SQL commands for indexing the administrative boundaries
92 return f"boundaries rank {self.rank}"
94 def sql_count_objects(self):
95 return pysql.SQL("""SELECT count(*) FROM placex
96 WHERE indexed_status > 0
98 AND class = 'boundary' and type = 'administrative'
99 """).format(pysql.Literal(self.rank))
101 def sql_get_objects(self):
102 return self.SELECT_SQL + pysql.SQL(
103 """WHERE indexed_status > 0 and rank_search = {}
104 and class = 'boundary' and type = 'administrative'
105 ORDER BY partition, admin_level
106 """).format(pysql.Literal(self.rank))
109 class InterpolationRunner:
110 """ Returns SQL commands for indexing the address interpolation table
111 location_property_osmline.
114 def __init__(self, analyzer):
115 self.analyzer = analyzer
120 return "interpolation lines (location_property_osmline)"
123 def sql_count_objects():
124 return """SELECT count(*) FROM location_property_osmline
125 WHERE indexed_status > 0"""
128 def sql_get_objects():
129 return """SELECT place_id
130 FROM location_property_osmline
131 WHERE indexed_status > 0
132 ORDER BY geometry_sector"""
136 def get_place_details(worker, ids):
137 worker.perform("""SELECT place_id, get_interpolation_address(address, osm_id) as address
138 FROM location_property_osmline WHERE place_id IN %s""",
139 (tuple((p[0] for p in ids)), ))
143 @functools.lru_cache(maxsize=1)
144 def _index_sql(num_places):
145 return pysql.SQL("""UPDATE location_property_osmline
146 SET indexed_status = 0, address = v.addr, token_info = v.ti
147 FROM (VALUES {}) as v(id, addr, ti)
148 WHERE place_id = v.id
149 """).format(_mk_valuelist("(%s, %s::hstore, %s::jsonb)", num_places))
152 def index_places(self, worker, places):
155 values.extend((place[x] for x in ('place_id', 'address')))
156 values.append(_analyze_place(place, self.analyzer))
158 worker.perform(self._index_sql(len(places)), values)
162 class PostcodeRunner:
163 """ Provides the SQL commands for indexing the location_postcode table.
168 return "postcodes (location_postcode)"
171 def sql_count_objects():
172 return 'SELECT count(*) FROM location_postcode WHERE indexed_status > 0'
175 def sql_get_objects():
176 return """SELECT place_id FROM location_postcode
177 WHERE indexed_status > 0
178 ORDER BY country_code, postcode"""
181 def index_places(worker, ids):
182 worker.perform(pysql.SQL("""UPDATE location_postcode SET indexed_status = 0
183 WHERE place_id IN ({})""")
184 .format(pysql.SQL(',').join((pysql.Literal(i[0]) for i in ids))))