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
15 from nominatim.indexer.place_info import PlaceInfo
17 # pylint: disable=C0111
19 def _mk_valuelist(template, num):
20 return pysql.SQL(',').join([pysql.SQL(template)] * num)
23 class AbstractPlacexRunner:
24 """ Returns SQL commands for indexing of the placex table.
26 SELECT_SQL = pysql.SQL('SELECT place_id FROM placex ')
27 UPDATE_LINE = "(%s, %s::hstore, %s::hstore, %s::int, %s::jsonb)"
29 def __init__(self, rank, analyzer):
31 self.analyzer = analyzer
35 @functools.lru_cache(maxsize=1)
36 def _index_sql(num_places):
39 SET indexed_status = 0, address = v.addr, token_info = v.ti,
40 name = v.name, linked_place_id = v.linked_place_id
41 FROM (VALUES {}) as v(id, name, addr, linked_place_id, ti)
43 """).format(_mk_valuelist(AbstractPlacexRunner.UPDATE_LINE, num_places))
47 def get_place_details(worker, ids):
48 worker.perform("""SELECT place_id, (placex_indexing_prepare(placex)).*
49 FROM placex WHERE place_id IN %s""",
50 (tuple((p[0] for p in ids)), ))
53 def index_places(self, worker, places):
56 for field in ('place_id', 'name', 'address', 'linked_place_id'):
57 values.append(place[field])
58 values.append(PlaceInfo(place).analyze(self.analyzer))
60 worker.perform(self._index_sql(len(places)), values)
63 class RankRunner(AbstractPlacexRunner):
64 """ Returns SQL commands for indexing one rank within the placex table.
68 return "rank {}".format(self.rank)
70 def sql_count_objects(self):
71 return pysql.SQL("""SELECT count(*) FROM placex
72 WHERE rank_address = {} and indexed_status > 0
73 """).format(pysql.Literal(self.rank))
75 def sql_get_objects(self):
76 return self.SELECT_SQL + pysql.SQL(
77 """WHERE indexed_status > 0 and rank_address = {}
78 ORDER BY geometry_sector
79 """).format(pysql.Literal(self.rank))
82 class BoundaryRunner(AbstractPlacexRunner):
83 """ Returns SQL commands for indexing the administrative boundaries
88 return "boundaries rank {}".format(self.rank)
90 def sql_count_objects(self):
91 return pysql.SQL("""SELECT count(*) FROM placex
92 WHERE indexed_status > 0
94 AND class = 'boundary' and type = 'administrative'
95 """).format(pysql.Literal(self.rank))
97 def sql_get_objects(self):
98 return self.SELECT_SQL + pysql.SQL(
99 """WHERE indexed_status > 0 and rank_search = {}
100 and class = 'boundary' and type = 'administrative'
101 ORDER BY partition, admin_level
102 """).format(pysql.Literal(self.rank))
105 class InterpolationRunner:
106 """ Returns SQL commands for indexing the address interpolation table
107 location_property_osmline.
110 def __init__(self, analyzer):
111 self.analyzer = analyzer
116 return "interpolation lines (location_property_osmline)"
119 def sql_count_objects():
120 return """SELECT count(*) FROM location_property_osmline
121 WHERE indexed_status > 0"""
124 def sql_get_objects():
125 return """SELECT place_id
126 FROM location_property_osmline
127 WHERE indexed_status > 0
128 ORDER BY geometry_sector"""
132 def get_place_details(worker, ids):
133 worker.perform("""SELECT place_id, get_interpolation_address(address, osm_id) as address
134 FROM location_property_osmline WHERE place_id IN %s""",
135 (tuple((p[0] for p in ids)), ))
139 @functools.lru_cache(maxsize=1)
140 def _index_sql(num_places):
141 return pysql.SQL("""UPDATE location_property_osmline
142 SET indexed_status = 0, address = v.addr, token_info = v.ti
143 FROM (VALUES {}) as v(id, addr, ti)
144 WHERE place_id = v.id
145 """).format(_mk_valuelist("(%s, %s::hstore, %s::jsonb)", num_places))
148 def index_places(self, worker, places):
151 values.extend((place[x] for x in ('place_id', 'address')))
152 values.append(PlaceInfo(place).analyze(self.analyzer))
154 worker.perform(self._index_sql(len(places)), values)
158 class PostcodeRunner:
159 """ Provides the SQL commands for indexing the location_postcode table.
164 return "postcodes (location_postcode)"
167 def sql_count_objects():
168 return 'SELECT count(*) FROM location_postcode WHERE indexed_status > 0'
171 def sql_get_objects():
172 return """SELECT place_id FROM location_postcode
173 WHERE indexed_status > 0
174 ORDER BY country_code, postcode"""
177 def index_places(worker, ids):
178 worker.perform(pysql.SQL("""UPDATE location_postcode SET indexed_status = 0
179 WHERE place_id IN ({})""")
180 .format(pysql.SQL(',').join((pysql.Literal(i[0]) for i in ids))))