2 Mix-ins that provide the actual commands for the indexer for various indexing
7 from psycopg2 import sql as pysql
9 from nominatim.indexer.place_info import PlaceInfo
11 # pylint: disable=C0111
13 def _mk_valuelist(template, num):
14 return pysql.SQL(',').join([pysql.SQL(template)] * num)
17 class AbstractPlacexRunner:
18 """ Returns SQL commands for indexing of the placex table.
20 SELECT_SQL = pysql.SQL('SELECT place_id FROM placex ')
21 UPDATE_LINE = "(%s, %s::hstore, %s::hstore, %s::int, %s::jsonb)"
23 def __init__(self, rank, analyzer):
25 self.analyzer = analyzer
29 @functools.lru_cache(maxsize=1)
30 def _index_sql(num_places):
33 SET indexed_status = 0, address = v.addr, token_info = v.ti,
34 name = v.name, linked_place_id = v.linked_place_id
35 FROM (VALUES {}) as v(id, name, addr, linked_place_id, ti)
37 """).format(_mk_valuelist(AbstractPlacexRunner.UPDATE_LINE, num_places))
41 def get_place_details(worker, ids):
42 worker.perform("""SELECT place_id, (placex_indexing_prepare(placex)).*
43 FROM placex WHERE place_id IN %s""",
44 (tuple((p[0] for p in ids)), ))
47 def index_places(self, worker, places):
50 for field in ('place_id', 'name', 'address', 'linked_place_id'):
51 values.append(place[field])
52 values.append(PlaceInfo(place).analyze(self.analyzer))
54 worker.perform(self._index_sql(len(places)), values)
57 class RankRunner(AbstractPlacexRunner):
58 """ Returns SQL commands for indexing one rank within the placex table.
62 return "rank {}".format(self.rank)
64 def sql_count_objects(self):
65 return pysql.SQL("""SELECT count(*) FROM placex
66 WHERE rank_address = {} and indexed_status > 0
67 """).format(pysql.Literal(self.rank))
69 def sql_get_objects(self):
70 return self.SELECT_SQL + pysql.SQL(
71 """WHERE indexed_status > 0 and rank_address = {}
72 ORDER BY geometry_sector
73 """).format(pysql.Literal(self.rank))
76 class BoundaryRunner(AbstractPlacexRunner):
77 """ Returns SQL commands for indexing the administrative boundaries
82 return "boundaries rank {}".format(self.rank)
84 def sql_count_objects(self):
85 return pysql.SQL("""SELECT count(*) FROM placex
86 WHERE indexed_status > 0
88 AND class = 'boundary' and type = 'administrative'
89 """).format(pysql.Literal(self.rank))
91 def sql_get_objects(self):
92 return self.SELECT_SQL + pysql.SQL(
93 """WHERE indexed_status > 0 and rank_search = {}
94 and class = 'boundary' and type = 'administrative'
95 ORDER BY partition, admin_level
96 """).format(pysql.Literal(self.rank))
99 class InterpolationRunner:
100 """ Returns SQL commands for indexing the address interpolation table
101 location_property_osmline.
104 def __init__(self, analyzer):
105 self.analyzer = analyzer
110 return "interpolation lines (location_property_osmline)"
113 def sql_count_objects():
114 return """SELECT count(*) FROM location_property_osmline
115 WHERE indexed_status > 0"""
118 def sql_get_objects():
119 return """SELECT place_id
120 FROM location_property_osmline
121 WHERE indexed_status > 0
122 ORDER BY geometry_sector"""
126 def get_place_details(worker, ids):
127 worker.perform("""SELECT place_id, get_interpolation_address(address, osm_id) as address
128 FROM location_property_osmline WHERE place_id IN %s""",
129 (tuple((p[0] for p in ids)), ))
133 @functools.lru_cache(maxsize=1)
134 def _index_sql(num_places):
135 return pysql.SQL("""UPDATE location_property_osmline
136 SET indexed_status = 0, address = v.addr, token_info = v.ti
137 FROM (VALUES {}) as v(id, addr, ti)
138 WHERE place_id = v.id
139 """).format(_mk_valuelist("(%s, %s::hstore, %s::jsonb)", num_places))
142 def index_places(self, worker, places):
145 values.extend((place[x] for x in ('place_id', 'address')))
146 values.append(PlaceInfo(place).analyze(self.analyzer))
148 worker.perform(self._index_sql(len(places)), values)
152 class PostcodeRunner:
153 """ Provides the SQL commands for indexing the location_postcode table.
158 return "postcodes (location_postcode)"
161 def sql_count_objects():
162 return 'SELECT count(*) FROM location_postcode WHERE indexed_status > 0'
165 def sql_get_objects():
166 return """SELECT place_id FROM location_postcode
167 WHERE indexed_status > 0
168 ORDER BY country_code, postcode"""
171 def index_places(worker, ids):
172 worker.perform(pysql.SQL("""UPDATE location_postcode SET indexed_status = 0
173 WHERE place_id IN ({})""")
174 .format(pysql.SQL(',').join((pysql.Literal(i[0]) for i in ids))))