X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/2735ea768aa812998a9498cf411563f118bd6ad6..157414a053cde0133a001964264254a62f231be5:/src/nominatim_api/core.py diff --git a/src/nominatim_api/core.py b/src/nominatim_api/core.py index c45b24de..a71bca6e 100644 --- a/src/nominatim_api/core.py +++ b/src/nominatim_api/core.py @@ -26,7 +26,7 @@ from .connection import SearchConnection from .status import get_status, StatusResult from .lookup import get_places, get_detailed_place from .reverse import ReverseGeocoder -from .search import ForwardGeocoder, Phrase, PhraseType, make_query_analyzer +from . import search as nsearch from . import types as ntyp from .results import DetailedResult, ReverseResult, SearchResults @@ -138,19 +138,17 @@ class NominatimAPIAsync: async with engine.begin() as conn: result = await conn.scalar(sa.text('SHOW server_version_num')) server_version = int(result) - if server_version >= 110000: - await conn.execute(sa.text("SET jit_above_cost TO '-1'")) - await conn.execute(sa.text( - "SET max_parallel_workers_per_gather TO '0'")) + await conn.execute(sa.text("SET jit_above_cost TO '-1'")) + await conn.execute(sa.text( + "SET max_parallel_workers_per_gather TO '0'")) except (PGCORE_ERROR, sa.exc.OperationalError): server_version = 0 - if server_version >= 110000: - @sa.event.listens_for(engine.sync_engine, "connect") - def _on_connect(dbapi_con: Any, _: Any) -> None: - cursor = dbapi_con.cursor() - cursor.execute("SET jit_above_cost TO '-1'") - cursor.execute("SET max_parallel_workers_per_gather TO '0'") + @sa.event.listens_for(engine.sync_engine, "connect") + def _on_connect(dbapi_con: Any, _: Any) -> None: + cursor = dbapi_con.cursor() + cursor.execute("SET jit_above_cost TO '-1'") + cursor.execute("SET max_parallel_workers_per_gather TO '0'") self._property_cache['DB:server_version'] = server_version @@ -186,7 +184,7 @@ class NominatimAPIAsync: assert self._tables is not None async with self._engine.begin() as conn: - yield SearchConnection(conn, self._tables, self._property_cache) + yield SearchConnection(conn, self._tables, self._property_cache, self.config) async def status(self) -> StatusResult: """ Return the status of the database. @@ -209,7 +207,7 @@ class NominatimAPIAsync: async with self.begin() as conn: conn.set_query_timeout(self.query_timeout) if details.keywords: - await make_query_analyzer(conn) + await nsearch.make_query_analyzer(conn) return await get_detailed_place(conn, place, details) async def lookup(self, places: Sequence[ntyp.PlaceRef], **params: Any) -> SearchResults: @@ -221,7 +219,7 @@ class NominatimAPIAsync: async with self.begin() as conn: conn.set_query_timeout(self.query_timeout) if details.keywords: - await make_query_analyzer(conn) + await nsearch.make_query_analyzer(conn) return await get_places(conn, places, details) async def reverse(self, coord: ntyp.AnyPoint, **params: Any) -> Optional[ReverseResult]: @@ -239,7 +237,7 @@ class NominatimAPIAsync: async with self.begin() as conn: conn.set_query_timeout(self.query_timeout) if details.keywords: - await make_query_analyzer(conn) + await nsearch.make_query_analyzer(conn) geocoder = ReverseGeocoder(conn, details, self.reverse_restrict_to_country_area) return await geocoder.lookup(coord) @@ -253,10 +251,10 @@ class NominatimAPIAsync: async with self.begin() as conn: conn.set_query_timeout(self.query_timeout) - geocoder = ForwardGeocoder(conn, ntyp.SearchDetails.from_kwargs(params), - self.config.get_int('REQUEST_TIMEOUT') - if self.config.REQUEST_TIMEOUT else None) - phrases = [Phrase(PhraseType.NONE, p.strip()) for p in query.split(',')] + geocoder = nsearch.ForwardGeocoder(conn, ntyp.SearchDetails.from_kwargs(params), + self.config.get_int('REQUEST_TIMEOUT') + if self.config.REQUEST_TIMEOUT else None) + phrases = [nsearch.Phrase(nsearch.PHRASE_ANY, p.strip()) for p in query.split(',')] return await geocoder.lookup(phrases) async def search_address(self, amenity: Optional[str] = None, @@ -273,22 +271,22 @@ class NominatimAPIAsync: conn.set_query_timeout(self.query_timeout) details = ntyp.SearchDetails.from_kwargs(params) - phrases: List[Phrase] = [] + phrases: List[nsearch.Phrase] = [] if amenity: - phrases.append(Phrase(PhraseType.AMENITY, amenity)) + phrases.append(nsearch.Phrase(nsearch.PHRASE_AMENITY, amenity)) if street: - phrases.append(Phrase(PhraseType.STREET, street)) + phrases.append(nsearch.Phrase(nsearch.PHRASE_STREET, street)) if city: - phrases.append(Phrase(PhraseType.CITY, city)) + phrases.append(nsearch.Phrase(nsearch.PHRASE_CITY, city)) if county: - phrases.append(Phrase(PhraseType.COUNTY, county)) + phrases.append(nsearch.Phrase(nsearch.PHRASE_COUNTY, county)) if state: - phrases.append(Phrase(PhraseType.STATE, state)) + phrases.append(nsearch.Phrase(nsearch.PHRASE_STATE, state)) if postalcode: - phrases.append(Phrase(PhraseType.POSTCODE, postalcode)) + phrases.append(nsearch.Phrase(nsearch.PHRASE_POSTCODE, postalcode)) if country: - phrases.append(Phrase(PhraseType.COUNTRY, country)) + phrases.append(nsearch.Phrase(nsearch.PHRASE_COUNTRY, country)) if not phrases: raise UsageError('Nothing to search for.') @@ -311,9 +309,9 @@ class NominatimAPIAsync: if amenity: details.layers |= ntyp.DataLayer.POI - geocoder = ForwardGeocoder(conn, details, - self.config.get_int('REQUEST_TIMEOUT') - if self.config.REQUEST_TIMEOUT else None) + geocoder = nsearch.ForwardGeocoder(conn, details, + self.config.get_int('REQUEST_TIMEOUT') + if self.config.REQUEST_TIMEOUT else None) return await geocoder.lookup(phrases) async def search_category(self, categories: List[Tuple[str, str]], @@ -330,15 +328,15 @@ class NominatimAPIAsync: async with self.begin() as conn: conn.set_query_timeout(self.query_timeout) if near_query: - phrases = [Phrase(PhraseType.NONE, p) for p in near_query.split(',')] + phrases = [nsearch.Phrase(nsearch.PHRASE_ANY, p) for p in near_query.split(',')] else: phrases = [] if details.keywords: - await make_query_analyzer(conn) + await nsearch.make_query_analyzer(conn) - geocoder = ForwardGeocoder(conn, details, - self.config.get_int('REQUEST_TIMEOUT') - if self.config.REQUEST_TIMEOUT else None) + geocoder = nsearch.ForwardGeocoder(conn, details, + self.config.get_int('REQUEST_TIMEOUT') + if self.config.REQUEST_TIMEOUT else None) return await geocoder.lookup_pois(categories, phrases)